caveman-crummy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ rdoc
2
+ pkg
3
+ crummy.gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,118 @@
1
+ h1. Crummy
2
+
3
+ h2. Introduction
4
+
5
+ Crummy is a simple and tasty way to add breadcrumbs to your Rails applications.
6
+
7
+ h2. Install
8
+
9
+ The gem is hosted on gemcutter, so if you haven’t already, add it as a gem source:
10
+
11
+ <pre>
12
+ <code>
13
+ gem sources -a http://gemcutter.org/
14
+ </code>
15
+ </pre>
16
+
17
+ Then install the Crummy gem:
18
+
19
+ <pre>
20
+ <code>
21
+ gem install crummy
22
+ </code>
23
+ </pre>
24
+
25
+ You can also install it as a Rails plugin:
26
+
27
+ <pre>
28
+ <code>
29
+ script/plugin install git://github.com/zachinglis/crummy.git
30
+ </code>
31
+ </pre>
32
+
33
+ h2. Example
34
+
35
+ In your controllers you may add_crumb either like a before_filter or within a method (It is also available to views).
36
+
37
+ <pre>
38
+ <code>
39
+ class ApplicationController
40
+ add_crumb "Home", '/'
41
+ end
42
+
43
+ class BusinessController < ApplicationController
44
+ add_crumb("Businesses") { |instance| instance.send :businesses_path }
45
+ add_crumb("Comments", :only => "comments") { |instance| instance.send :businesses_comments_path }
46
+ before_filter :load_comment, :only => "show"
47
+ add_crumb :comment, :only => "show"
48
+
49
+ def show
50
+ add_crumb @business.display_name, @business
51
+ end
52
+
53
+ def load_comment
54
+ @comment = Comment.find(params[:id])
55
+ end
56
+ end
57
+ </code>
58
+ </pre>
59
+
60
+ Then in your view:
61
+
62
+ <pre>
63
+ <code>
64
+ <%= render_crumbs %>
65
+ </code>
66
+ </pre>
67
+
68
+ h2. Options for render_crumb_
69
+
70
+ render_crumbs renders the list of crumbs as either html or xml
71
+
72
+ It takes 3 options
73
+
74
+ The output format. Can either be :xml or :html. Defaults to :html
75
+
76
+ <code>:format => (:html|:xml|:html_with_list)</code>
77
+
78
+ 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
79
+
80
+ <code>:seperator => string</code>
81
+
82
+ Render links in the output. Defaults to +true+
83
+
84
+ <code>:link => boolean</code>
85
+
86
+ h3. Examples
87
+
88
+ <pre>
89
+ <code>
90
+ render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
91
+ render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
92
+ render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
93
+ render_crumbs :seperator => ' > ', :format => :html_with_list
94
+ #=> <ul class="breadcrumb"><li><a href="/">Home</a></li><li><a href="/businesses">Businesses</a></li></ul>
95
+ </code>
96
+ </pre>
97
+ A crumb with a nil link will just output plain text.
98
+
99
+ h2. Notes
100
+
101
+ The variable set is set to @_crumbs as to not conflict with your code.
102
+
103
+ h2. Todo
104
+
105
+ * Port over rspecs from project to plugin (Fully tested in a project)
106
+ * Accept instances of models as a single argument
107
+ * Allow for variables in names. (The workaround is to do your own before_filter for that currently)
108
+
109
+ h2. Credits
110
+
111
+ * "Zach Inglis":http://zachinglis.com
112
+ * "Rein Henrichs":http://reinh.com
113
+ * "Les Hill":http://blog.leshill.org/
114
+ * "Sandro Turriate":http://turriate.com/
115
+ * "Przemysław Kowalczyk":http://szeryf.wordpress.com/2008/06/13/easy-and-flexible-breadcrumbs-for-rails/ - feature ideas
116
+ * "Sharad Jain":http://github.com/sjain
117
+
118
+ *Copyright (c) 2008 Zach Inglis, released under the MIT license*
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = "caveman-crummy"
11
+ gem.summary = %Q{Tasty breadcrumbs!}
12
+ gem.description = %Q{Crummy is a simple and tasty way to add breadcrumbs to your Rails applications.}
13
+ gem.email = "zach@lt3media.com"
14
+ gem.homepage = "http://github.com/caveman/crummy"
15
+ gem.authors = ["Zach Inglis", "Randall Chin"]
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ desc 'Test the crummy plugin.'
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = true
28
+ end
29
+
30
+ begin
31
+ gem 'hanna'
32
+ require 'hanna/rdoctask'
33
+ rescue LoadError
34
+ require 'rake/rdoctask'
35
+ end
36
+ desc 'Generate documentation for the crummy plugin.'
37
+ Rake::RDocTask.new(:rdoc) do |rdoc|
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = 'Crummy: Tasty Breadcrumbs'
40
+ rdoc.options << '--line-numbers' << '--inline-source'
41
+ rdoc.rdoc_files.include('README.textile')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
Binary file
@@ -0,0 +1,47 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{caveman-crummy}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Zach Inglis", "Randall Chin"]
12
+ s.date = %q{2010-08-31}
13
+ s.description = %q{Crummy is a simple and tasty way to add breadcrumbs to your Rails applications.}
14
+ s.email = %q{zach@lt3media.com}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.textile",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "caveman-crummy-0.1.0.gem",
25
+ "caveman-crummy.gemspec",
26
+ "init.rb",
27
+ "lib/crummy.rb",
28
+ "rails/init.rb",
29
+ "tasks/crummy_tasks.rake"
30
+ ]
31
+ s.homepage = %q{http://github.com/caveman/crummy}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Tasty breadcrumbs!}
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ else
43
+ end
44
+ else
45
+ end
46
+ end
47
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ ActionController::Base.send :include, Crummy::ControllerMethods
2
+ ActionView::Base.send :include, Crummy::ViewMethods
data/lib/crummy.rb ADDED
@@ -0,0 +1,130 @@
1
+ module Crummy
2
+ module ControllerMethods
3
+ module ClassMethods
4
+ # Add a crumb to the crumbs array.
5
+ #
6
+ # add_crumb("Home", "/")
7
+ # add_crumb("Business") { |instance| instance.business_path }
8
+ #
9
+ # Works like a before_filter so +:only+ and +except+ both work.
10
+ def add_crumb(name, *args)
11
+ options = args.extract_options!
12
+ url = args.first
13
+ raise ArgumentError, "Need more arguments" unless name or options[:record] or block_given?
14
+ raise ArgumentError, "Cannot pass url and use block" if url && block_given?
15
+ before_filter(options) do |instance|
16
+ url = yield instance if block_given?
17
+ url = instance.send url if url.is_a? Symbol
18
+ record = instance.instance_variable_get("@#{name}") unless url or block_given?
19
+ if record and record.respond_to? :to_param
20
+ name, url = record.to_s, instance.url_for(record)
21
+ end
22
+
23
+ # FIXME: url = instance.url_for(name) if name.respond_to?("to_param") && url.nil?
24
+ # FIXME: Add ||= for the name, url above
25
+ instance.add_crumb(name, url)
26
+ end
27
+ end
28
+ end
29
+
30
+ module InstanceMethods
31
+ # Add a crumb to the crumbs array.
32
+ #
33
+ # add_crumb("Home", "/")
34
+ # add_crumb("Business") { |instance| instance.business_path }
35
+ #
36
+ def add_crumb(name, url=nil)
37
+ crumbs.push [name, url]
38
+ end
39
+
40
+ # Lists the crumbs as an array
41
+ def crumbs
42
+ get_or_set_ivar "@_crumbs", []
43
+ end
44
+
45
+ def get_or_set_ivar(var, value) # :nodoc:
46
+ instance_variable_set var, instance_variable_get(var) || value
47
+ end
48
+ private :get_or_set_ivar
49
+ end
50
+
51
+ def self.included(receiver) # :nodoc:
52
+ receiver.extend ClassMethods
53
+ receiver.send :include, InstanceMethods
54
+ end
55
+ end
56
+
57
+ module ViewMethods
58
+ # List the crumbs as an array
59
+ def crumbs
60
+ @_crumbs ||= [] # Give me something to push to
61
+ end
62
+
63
+ # Add a crumb to the +crumbs+ array
64
+ def add_crumb(name, url=nil)
65
+ crumbs.push [name, url]
66
+ end
67
+
68
+ # Render the list of crumbs as either html or xml
69
+ #
70
+ # Takes 3 options:
71
+ # The output format. Can either be xml or html. Default :html
72
+ # :format => (:html|:xml)
73
+ # 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
74
+ # :seperator => string
75
+ # Render links in the output. Default +true+
76
+ # :link => boolean
77
+ #
78
+ # Examples:
79
+ # render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
80
+ # render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
81
+ # render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
82
+ #
83
+ # 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;+
84
+ #
85
+ # render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
86
+ #
87
+ def render_crumbs(options = {})
88
+ options[:format] = :html if options[:format] == nil
89
+ if options[:seperator] == nil
90
+ options[:seperator] = " &raquo; " if options[:format] == :html
91
+ options[:seperator] = "crumb" if options[:format] == :xml
92
+ end
93
+ options[:links] = true if options[:links] == nil
94
+ case options[:format]
95
+ when :html
96
+ crumb_string = crumbs.collect do |crumb|
97
+ crumb_to_html crumb, options[:links]
98
+ end * options[:seperator]
99
+
100
+ crumb_string = crumb_string.html_safe if crumb_string.respond_to?(:html_safe)
101
+ crumb_string
102
+ when :html_with_list
103
+ list_items = ''
104
+ crumbs.each_with_index do |crumb, pos|
105
+ #append separator unless it is the last item in the list
106
+ list_item_content = ( pos == crumbs.length-1 ? crumb_to_html(crumb, options[:links]) : crumb_to_html(crumb, options[:links]) + options[:seperator] )
107
+ list_items << content_tag(:li, list_item_content )
108
+ end
109
+ content_tag :ul, list_items, :class => 'breadcrumb'
110
+ when :xml
111
+ crumbs.collect do |crumb|
112
+ crumb_to_xml crumb, options[:links], options[:seperator]
113
+ end * ''
114
+ else
115
+ raise "Unknown breadcrumb output format"
116
+ end
117
+ end
118
+
119
+ def crumb_to_html(crumb, links)
120
+ name, url = crumb
121
+ url && links ? link_to(name, url) : name
122
+ end
123
+
124
+ def crumb_to_xml(crumb, links, seperator)
125
+ name, url = crumb
126
+ url && links ? "<#{seperator} href=\"#{url}\">#{name}</#{seperator}>" : "<#{seperator}>#{name}</#{seperator}>"
127
+ end
128
+
129
+ end
130
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib crummy])
2
+ ActionController::Base.send :include, Crummy::ControllerMethods
3
+ ActionView::Base.send :include, Crummy::ViewMethods
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :crummy do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caveman-crummy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Zach Inglis
14
+ - Randall Chin
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-08-31 00:00:00 -04:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description: Crummy is a simple and tasty way to add breadcrumbs to your Rails applications.
24
+ email: zach@lt3media.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.textile
31
+ files:
32
+ - .gitignore
33
+ - MIT-LICENSE
34
+ - README.textile
35
+ - Rakefile
36
+ - VERSION
37
+ - caveman-crummy-0.1.0.gem
38
+ - caveman-crummy.gemspec
39
+ - init.rb
40
+ - lib/crummy.rb
41
+ - rails/init.rb
42
+ - tasks/crummy_tasks.rake
43
+ has_rdoc: true
44
+ homepage: http://github.com/caveman/crummy
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Tasty breadcrumbs!
77
+ test_files: []
78
+