Permalink
Browse files

Breadcrumbs.

Basic links.
  • Loading branch information...
1 parent 536cf18 commit 022dd5c7cf5a9623d140e552052fff60b7771eb7 @jdwyah committed May 22, 2009
@@ -1,4 +1,6 @@
class TopicsController < ApplicationController
+ add_crumb("Topics") { |instance| instance.send :topics_path }
+
before_filter :require_user
before_filter :find_topic, :only => [:show, :edit, :update]
@@ -22,6 +24,10 @@ def create
end
end
+ def show
+ add_crumb @topic.name, @topic
+ end
+
def update
if @topic.update_attributes(params[:topic])
flash[:notice] = "Topic updated!"
@@ -34,7 +40,7 @@ def update
protected
def find_topic
- @topic = Topic.find(params[:id])
+ @topic = current_user.topics.find(params[:id])
end
end
@@ -1,5 +1,4 @@
%dl.topic
- %dd.date topic.created_at.to_s(:us_date)
- %dt.title topic.name
- %dd.description truncate(topic.text, 150)
+ %dd.date= topic.created_at.to_s(:us_date)
+ %dt.title= topic.name
=partial 'property', :collection => topic.properties
@@ -1,4 +1,6 @@
%h1 My Topics
#topics
- =partial @topics
+ =partial @topics
+
+= link_to 'Add Topic', new_topic_path
@@ -1,3 +1,4 @@
+= render_crumbs
%h1= @topic.name
#properties
@@ -0,0 +1,20 @@
+Copyright (c) 2008 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,93 @@
+ h1. Crummy
+
+h2. Introduction
+
+Crummy is a simple and tasty way to add breadcrumbs to your Rails applications.
+
+h2. Install
+
+To install, type <code>script/plugin install git://github.com/zachinglis/crummy.git</code>
+
+h2. Example
+
+In your controllers you may add_crumb either like a before_filter or within a method (It is also available to views).
+
+<pre>
+ <code>
+ class ApplicationController
+ add_crumb "Home", '/'
+ end
+
+ class BusinessController < ApplicationController
+ add_crumb("Businesses") { |instance| instance.send :businesses_path }
+ add_crumb("Comments", :only => "comments") { |instance| instance.send :businesses_comments_path }
+ before_filter :load_comment, :only => "show"
+ add_crumb :comment, :only => "show"
+
+ def show
+ add_crumb @business.display_name, @business
+ end
+
+ def load_comment
+ @comment = Comment.find(params[:id])
+ end
+ end
+ </code>
+</pre>
+
+Then in your view:
+
+<pre>
+ <code>
+ <%= render_crumbs %>
+ </code>
+</pre>
+
+h2. Options for render_crumb_
+
+render_crumbs renders the list of crumbs as either html or xml
+
+It takes 3 options
+
+The output format. Can either be :xml or :html. Defaults to :html
+
+<code>:format => (:html|:xml)</code>
+
+The seperator text. It does not assume you want spaces on either side so you must specify. Defaults to <code>&raquo;</code> for :html and <code><crumb></code> for :xml
+
+<code>:seperator => string</code>
+
+Render links in the output. Defaults to +true+
+
+<code>:link => boolean</code>
+
+h3. Examples
+
+<pre>
+ <code>
+ render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
+ render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
+ render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
+ </code>
+</pre>
+A crumb with a nil link will just output plain text.
+
+h2. Notes
+
+The variable set is set to @_crumbs as to not conflict with your code.
+
+h2. Todo
+
+* Port over rspecs from project to plugin (Fully tested in a project)
+* Accept instances of models as a single argument
+* Allow for variables in names. (The workaround is to do your own before_filter for that currently)
+
+h2. Credits
+
+* "Zach Inglis":http://zachinglis.com
+* "Rein Henrichs":http://reinh.com
+* "Les Hill":http://blog.leshill.org/
+* "Sandro Turriate":http://turriate.com/
+* "Przemysław Kowalczyk":http://szeryf.wordpress.com/2008/06/13/easy-and-flexible-breadcrumbs-for-rails/ - feature ideas
+
+*Copyright (c) 2008 Zach Inglis, released under the MIT license*
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'hanna/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the crummy plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the crummy plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Crummy: Tasty Breadcrumbs'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.textile')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+ActionController::Base.send :include, Crummy::ControllerMethods
+ActionView::Base.send :include, Crummy::ViewMethods
@@ -0,0 +1,119 @@
+module Crummy
+ module ControllerMethods
+ module ClassMethods
+ # Add a crumb to the crumbs array.
+ #
+ # add_crumb("Home", "/")
+ # add_crumb("Business") { |instance| instance.business_path }
+ #
+ # Works like a before_filter so +:only+ and +except+ both work.
+ def add_crumb(name, *args)
+ options = args.extract_options!
+ url = args.first
+ raise ArgumentError, "Need more arguments" unless name or options[:record] or block_given?
+ raise ArgumentError, "Cannot pass url and use block" if url && block_given?
+ before_filter(options) do |instance|
+ url = yield instance if block_given?
+ url = instance.send url if url.is_a? Symbol
+ record = instance.instance_variable_get("@#{name}") unless url or block_given?
+ if record and record.respond_to? :to_param
+ name, url = record.to_s, instance.url_for(record)
+ end
+
+ # FIXME: url = instance.url_for(name) if name.respond_to?("to_param") && url.nil?
+ # FIXME: Add ||= for the name, url above
+ instance.add_crumb(name, url)
+ end
+ end
+ end
+
+ module InstanceMethods
+ # Add a crumb to the crumbs array.
+ #
+ # add_crumb("Home", "/")
+ # add_crumb("Business") { |instance| instance.business_path }
+ #
+ def add_crumb(name, url=nil)
+ crumbs.push [name, url]
+ end
+
+ # Lists the crumbs as an array
+ def crumbs
+ get_or_set_ivar "@_crumbs", []
+ end
+
+ def get_or_set_ivar(var, value) # :nodoc:
+ instance_variable_set var, instance_variable_get(var) || value
+ end
+ private :get_or_set_ivar
+ end
+
+ def self.included(receiver) # :nodoc:
+ receiver.extend ClassMethods
+ receiver.send :include, InstanceMethods
+ end
+ end
+
+ module ViewMethods
+ # List the crumbs as an array
+ def crumbs
+ @_crumbs ||= [] # Give me something to push to
+ end
+
+ # Add a crumb to the +crumbs+ array
+ def add_crumb(name, url=nil)
+ crumbs.push [name, url]
+ end
+
+ # Render the list of crumbs as either html or xml
+ #
+ # Takes 3 options:
+ # The output format. Can either be xml or html. Default :html
+ # :format => (:html|:xml)
+ # The seperator text. It does not assume you want spaces on either side so you must specify. Default +&raquo;+ for :html and +crumb+ for xml
+ # :seperator => string
+ # Render links in the output. Default +true+
+ # :link => boolean
+ #
+ # Examples:
+ # render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
+ # render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
+ # render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
+ #
+ # The only argument is for the seperator text. It does not assume you want spaces on either side so you must specify. Defaults to +&raquo;+
+ #
+ # render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
+ #
+ def render_crumbs(options = {})
+ options[:format] = :html if options[:format] == nil
+ if options[:seperator] == nil
+ options[:seperator] = " &raquo; " if options[:format] == :html
+ options[:seperator] = "crumb" if options[:format] == :xml
+ end
+ options[:links] = true if options[:links] == nil
+ case options[:format]
+ when :html
+ crumbs.collect do |crumb|
+ crumb_to_html crumb, options[:links]
+ end * options[:seperator]
+ when :xml
+ crumbs.collect do |crumb|
+ crumb_to_xml crumb, options[:links], options[:seperator]
+ end * ''
+ else
+ raise "Unknown breadcrumb output format"
+ end
+ end
+
+ def crumb_to_html(crumb, links)
+ name, url = crumb
+ url && links ? link_to(name, url) : name
+ end
+
+ def crumb_to_xml(crumb, links, seperator)
+ name, url = crumb
+ url && links ? "<#{seperator} href=\"#{url}\">#{name}</#{seperator}>" : "<#{seperator}>#{name}</#{seperator}>"
+ end
+
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :crummy do
+# # Task goes here
+# end

0 comments on commit 022dd5c

Please sign in to comment.