bsm-breadcrumbs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_DISABLE_SHARED_GEMS: "1"
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ doc
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "actionpack", '~> 3.0.0'
4
+
5
+ group :test do
6
+ gem "test-unit"
7
+ end
8
+
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionpack (3.0.1)
6
+ activemodel (= 3.0.1)
7
+ activesupport (= 3.0.1)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.4.1)
11
+ rack (~> 1.2.1)
12
+ rack-mount (~> 0.6.12)
13
+ rack-test (~> 0.5.4)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.1)
16
+ activesupport (= 3.0.1)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.4.1)
19
+ activesupport (3.0.1)
20
+ builder (2.1.2)
21
+ erubis (2.6.6)
22
+ abstract (>= 1.0.0)
23
+ i18n (0.4.2)
24
+ rack (1.2.1)
25
+ rack-mount (0.6.13)
26
+ rack (>= 1.0.0)
27
+ rack-test (0.5.6)
28
+ rack (>= 1.0)
29
+ test-unit (2.1.1)
30
+ tzinfo (0.3.23)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ actionpack (~> 3.0.0)
37
+ test-unit
data/README.rdoc ADDED
@@ -0,0 +1,118 @@
1
+ = Breadcrumbs
2
+
3
+ Breadcrumbs is a simple plugin that adds a +breadcrumbs+ object to controllers and views.
4
+
5
+ == Instalation
6
+
7
+ Just run <tt>sudo gem install breadcrumbs</tt>
8
+
9
+ == Usage
10
+
11
+ On your controller (optional):
12
+
13
+ class ApplicationController < ActionController::Base
14
+ before_filter :add_initial_breadcrumbs
15
+
16
+ private
17
+ def add_initial_breadcrumbs
18
+ breadcrumbs.add 'Home', root_path
19
+ end
20
+ end
21
+
22
+ class ThingsController < ApplicationController
23
+ def index
24
+ breadcrumbs.add 'Things', things_path
25
+ end
26
+ end
27
+
28
+ You don't need to provide an URL; in that case, a span will be generated
29
+ instead of a link:
30
+
31
+ breadcrumbs.add 'Some page'
32
+
33
+ You can set additional HTML attributes if you need to:
34
+
35
+ breadcrumbs.add 'Home', root_path, :id => 'home', :title => 'Go to the home page'
36
+
37
+ You can also use polymorphic routes. Example:
38
+
39
+ breadcrumbs.add 'Manage User', [:admin, @user] # => e.g. /admin/users/123
40
+
41
+ On your view (possibly application.html.erb):
42
+
43
+ <%= breadcrumbs.render %>
44
+
45
+ You can render as ordered list.
46
+
47
+ <%= breadcrumbs.render(:format => :ordered_list) %>
48
+
49
+ You can render as inline links.
50
+
51
+ <%= breadcrumbs.render(:format => :inline) %>
52
+
53
+ You can set your own separator:
54
+
55
+ <p>
56
+ You are here: <%= breadcrumbs.render(:format => :inline, :separator => '|') %>
57
+ </p>
58
+
59
+ You can also define your own formatter. Just create a class that implements a +render+ instance
60
+ method and you're good to go.
61
+
62
+ class Breadcrumbs::Render::Dl
63
+ def render
64
+ # return breadcrumbs wrapped in a <DL> tag
65
+ end
66
+ end
67
+
68
+ To use your new format, just provide the <tt>:format</tt> option.
69
+
70
+ breadcrumbs.render(:format => :dl)
71
+
72
+ === I18n
73
+
74
+ Breadcrumbs is integrated with I18n. You can set translations like:
75
+
76
+ en:
77
+ breadcrumbs:
78
+ home: "Home"
79
+
80
+ And then you just call
81
+
82
+ breadcrumbs.add :home
83
+
84
+ In fact, you can provide any scope you want.
85
+
86
+ breadcrumbs.add "titles.home"
87
+
88
+ If you don't want to translate a label, just pass the option <tt>:i18n</tt> as <tt>false</tt>.
89
+
90
+ breadcrumbs.add :home, nil, :i18n => false
91
+
92
+ == Maintainer
93
+
94
+ * Nando Vieira - http://simplesideias.com.br
95
+ * Dimitrij Denissenko
96
+
97
+ License
98
+
99
+ (The MIT License)
100
+
101
+ Permission is hereby granted, free of charge, to any person obtaining
102
+ a copy of this software and associated documentation files (the
103
+ 'Software'), to deal in the Software without restriction, including
104
+ without limitation the rights to use, copy, modify, merge, publish,
105
+ distribute, sublicense, and/or sell copies of the Software, and to
106
+ permit persons to whom the Software is furnished to do so, subject to
107
+ the following conditions:
108
+
109
+ The above copyright notice and this permission notice shall be
110
+ included in all copies or substantial portions of the Software.
111
+
112
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
113
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
114
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
115
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
116
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
117
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
118
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require "jeweler"
2
+ require "rake/testtask"
3
+ require "rake/rdoctask"
4
+ require "lib/breadcrumbs/version"
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.libs << "lib"
11
+ t.test_files = FileList["test/**/*_test.rb"]
12
+ t.verbose = true
13
+ end
14
+
15
+ Rake::RDocTask.new do |rdoc|
16
+ rdoc.main = "README.rdoc"
17
+ rdoc.rdoc_dir = "doc"
18
+ rdoc.title = "Breadcrumbs"
19
+ rdoc.options += %w[ --line-numbers --inline-source --charset utf-8 ]
20
+ rdoc.rdoc_files.include("README.rdoc")
21
+ rdoc.rdoc_files.include("lib/**/*.rb")
22
+ end
23
+
24
+ begin
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gem|
27
+ gem.name = "bsm-breadcrumbs"
28
+ gem.email = "dimitrij@blacksquaremedia.com"
29
+ gem.homepage = "http://github.com/bsm/breadcrumbs"
30
+ gem.authors = ["Nando Vieira", "Dimitrij Denissenko"]
31
+ gem.version = Breadcrumbs::Version::STRING
32
+ gem.summary = "Breadcrumbs is a simple plugin that adds a `breadcrumbs` object to controllers and views."
33
+ gem.description = "Breadcrumbs is a simple plugin that adds a `breadcrumbs` object to controllers and views."
34
+ gem.add_runtime_dependency "actionpack", ">= 3.0.0"
35
+ end
36
+ Jeweler::GemcutterTasks.new
37
+ rescue LoadError
38
+ puts "Jeweler not available. Install it with: gem install jeweler"
39
+ end
@@ -0,0 +1,61 @@
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{bsm-breadcrumbs}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nando Vieira", "Dimitrij Denissenko"]
12
+ s.date = %q{2010-11-11}
13
+ s.description = %q{Breadcrumbs is a simple plugin that adds a `breadcrumbs` object to controllers and views.}
14
+ s.email = %q{dimitrij@blacksquaremedia.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".bundle/config",
20
+ ".gitignore",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "bsm-breadcrumbs.gemspec",
26
+ "lib/breadcrumbs.rb",
27
+ "lib/breadcrumbs/action_controller_ext.rb",
28
+ "lib/breadcrumbs/render.rb",
29
+ "lib/breadcrumbs/render/base.rb",
30
+ "lib/breadcrumbs/render/inline.rb",
31
+ "lib/breadcrumbs/render/list.rb",
32
+ "lib/breadcrumbs/render/ordered_list.rb",
33
+ "lib/breadcrumbs/version.rb",
34
+ "test/breadcrumbs_test.rb",
35
+ "test/resources/pt.yml",
36
+ "test/test_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/bsm/breadcrumbs}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.7}
42
+ s.summary = %q{Breadcrumbs is a simple plugin that adds a `breadcrumbs` object to controllers and views.}
43
+ s.test_files = [
44
+ "test/breadcrumbs_test.rb",
45
+ "test/test_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<actionpack>, [">= 3.0.0"])
54
+ else
55
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<actionpack>, [">= 3.0.0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,71 @@
1
+ require "action_controller"
2
+ require "breadcrumbs/render"
3
+ require "breadcrumbs/action_controller_ext"
4
+ require "active_support/inflector"
5
+
6
+ class Breadcrumbs
7
+ attr_accessor :controller, :items
8
+
9
+ def initialize(controller) # :nodoc:
10
+ self.controller = controller
11
+ self.items = []
12
+ end
13
+
14
+ # Add a new breadcrumbs.
15
+ #
16
+ # breadcrumbs.add "Home"
17
+ # breadcrumbs.add "Home", "/"
18
+ # breadcrumbs.add "Home", "/", :class => "home"
19
+ #
20
+ # If you provide a symbol as text, it will try to
21
+ # find it as I18n scope.
22
+ #
23
+ def add(text, url = nil, options = {})
24
+ options.reverse_merge!(:i18n => true)
25
+ text = translate(text) if options.delete(:i18n)
26
+ url = controller.__send__(:url_for, url) if url
27
+ items << [text.to_s, url, options]
28
+ end
29
+
30
+ alias :<< :add
31
+
32
+ # Render breadcrumbs using the specified format.
33
+ # Use HTML lists by default, but can be plain links.
34
+ #
35
+ # breadcrumbs.render
36
+ # breadcrumbs.render(:format => :inline)
37
+ # breadcrumbs.render(:format => :inline, :separator => "|")
38
+ # breadcrumbs.render(:format => :list)
39
+ # breadcrumbs.render(:format => :ordered_list)
40
+ # breadcrumbs.render(:id => "breadcrumbs")
41
+ # breadcrumbs.render(:class => "breadcrumbs")
42
+ #
43
+ # You can also define your own formatter. Just create a class that implements a +render+ instance
44
+ # method and you're good to go.
45
+ #
46
+ # class Breadcrumbs::Render::Dl
47
+ # def render
48
+ # # return breadcrumbs wrapped in a <DL> tag
49
+ # end
50
+ # end
51
+ #
52
+ # To use your new format, just provide the <tt>:format</tt> option.
53
+ #
54
+ # breadcrumbs.render(:format => :dl)
55
+ #
56
+ def render(options = {})
57
+ options[:format] ||= :list
58
+
59
+ klass_name = options[:format].to_s.classify
60
+ klass = Breadcrumbs::Render.const_get(klass_name)
61
+ html = klass.new(self, options).render
62
+
63
+ html.respond_to?(:html_safe) ? html.html_safe : html
64
+ end
65
+
66
+ def translate(scope) # :nodoc:
67
+ text = I18n.t(scope, :scope => :breadcrumbs, :raise => true) rescue nil
68
+ text ||= I18n.t(scope, :default => scope.to_s)
69
+ text
70
+ end
71
+ end
@@ -0,0 +1,16 @@
1
+ class Breadcrumbs
2
+ module ActionController # :nodoc: all
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :breadcrumbs
7
+ end
8
+
9
+ def breadcrumbs
10
+ @breadcrumbs ||= Breadcrumbs.new(self)
11
+ end
12
+
13
+ end
14
+ end
15
+
16
+ ActionController::Base.send :include, Breadcrumbs::ActionController
@@ -0,0 +1,8 @@
1
+ class Breadcrumbs
2
+ module Render
3
+ autoload :Base, "breadcrumbs/render/base"
4
+ autoload :Inline, "breadcrumbs/render/inline"
5
+ autoload :List, "breadcrumbs/render/list"
6
+ autoload :OrderedList, "breadcrumbs/render/ordered_list"
7
+ end
8
+ end
@@ -0,0 +1,44 @@
1
+ class Breadcrumbs
2
+ module Render
3
+ class Base # :nodoc: all
4
+ attr_accessor :breadcrumbs
5
+ attr_accessor :default_options
6
+
7
+ def initialize(breadcrumbs, default_options = {})
8
+ @breadcrumbs = breadcrumbs
9
+ @default_options = default_options
10
+ end
11
+
12
+ # Build a HTML tag.
13
+ #
14
+ # tag(:p, "Hello!")
15
+ # tag(:p, "Hello!", :class => "hello")
16
+ # tag(:p, :class => "phrase") { "Hello" }
17
+ #
18
+ def tag(name, *args, &block)
19
+ options = args.pop if args.last.kind_of?(Hash)
20
+ options ||= {}
21
+
22
+ content = args.first
23
+ content = self.instance_eval(&block) if block_given?
24
+
25
+ attrs = " " + options.collect {|n, v| %[%s="%s"] % [n, v] }.join(" ") unless options.empty?
26
+
27
+ %[<#{name}#{attrs}>#{content}</#{name}>]
28
+ end
29
+
30
+ protected
31
+ def wrap_item(url, text, options)
32
+ if url
33
+ tag(:a, text, options.merge(:href => url))
34
+ else
35
+ tag(:span, text, options)
36
+ end
37
+ end
38
+
39
+ def escape(text)
40
+ text.respond_to?(:html_safe?) && text.html_safe? ? text : CGI.escapeHTML(text)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ class Breadcrumbs
2
+ module Render
3
+ class Inline < Base # :nodoc: all
4
+ def render
5
+ options = {:class => "breadcrumbs", :separator => "&#187;"}.merge(default_options)
6
+
7
+ html = []
8
+ items = breadcrumbs.items
9
+ size = items.size
10
+
11
+ items.each_with_index do |item, i|
12
+ html << render_item(item, i, size)
13
+ end
14
+
15
+ separator = tag(:span, options[:separator], :class => "separator")
16
+
17
+ html.join(" #{separator} ")
18
+ end
19
+
20
+ def render_item(item, i, size)
21
+ text, url, options = *item
22
+ options[:class] ||= ""
23
+
24
+ css = []
25
+ css << "first" if i == 0
26
+ css << "last" if i == size - 1
27
+ css << "item-#{i}"
28
+
29
+ options[:class] << " #{css.join(" ")}"
30
+ options[:class].gsub!(/^ *(.*?)$/, '\\1')
31
+
32
+ wrap_item(url, escape(text), options)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ class Breadcrumbs
2
+ module Render
3
+ class List < Base # :nodoc: all
4
+ def render
5
+ options = {
6
+ :class => "breadcrumbs"
7
+ }.merge(default_options)
8
+
9
+ tag(list_style, options) do
10
+ html = ""
11
+ items = breadcrumbs.items
12
+ size = items.size
13
+
14
+ items.each_with_index do |item, i|
15
+ html << render_item(item, i, size)
16
+ end
17
+
18
+ html
19
+ end
20
+ end
21
+
22
+ def list_style
23
+ :ul
24
+ end
25
+
26
+ def render_item(item, i, size)
27
+ css = []
28
+ css << "first" if i == 0
29
+ css << "last" if i == size - 1
30
+ css << "item-#{i}"
31
+
32
+ text, url, options = *item
33
+ text = wrap_item(url, escape(text), options)
34
+ tag(:li, text, :class => css.join(" "))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ class Breadcrumbs
2
+ module Render
3
+ class OrderedList < List # :nodoc: all
4
+ def list_style
5
+ :ol
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ class Breadcrumbs
2
+ module Version # :nodoc: all
3
+ MAJOR = 0
4
+ MINOR = 2
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,220 @@
1
+ require "test_helper"
2
+
3
+ class BreadcrumbsTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @controller = TestsController.new
7
+ @controller.request = ActionController::TestRequest.new
8
+ @breadcrumbs = Breadcrumbs.new(@controller)
9
+ @inline = Breadcrumbs::Render::Inline.new(@breadcrumbs)
10
+ end
11
+
12
+ def test_return_safe_html
13
+ @breadcrumbs.add "Terms & Conditions"
14
+ assert_equal %(<span class="first last item-0">Terms &amp; Conditions</span>), @breadcrumbs.render(:format => :inline)
15
+ end
16
+
17
+ def test_allow_custom_text_escaping
18
+ @breadcrumbs.add "<em>Home</em>".html_safe
19
+ html = @breadcrumbs.render(:format => :inline)
20
+ assert_equal %(<span class="first last item-0"><em>Home</em></span>), html
21
+ end
22
+
23
+ def test_add_item
24
+ @breadcrumbs.add "Home"
25
+ assert_equal 1, @breadcrumbs.items.count
26
+
27
+ @breadcrumbs << "Home"
28
+ assert_equal 2, @breadcrumbs.items.count
29
+ end
30
+
31
+ def test_tag
32
+ assert_equal "<span>Hi!</span>", @inline.tag(:span, "Hi!")
33
+ end
34
+
35
+ def test_tag_with_attributes
36
+ expected = %[<span class="greetings" id="hi">Hi!</span>]
37
+ assert_equal expected, @inline.tag(:span, "Hi!", :class => "greetings", :id => "hi")
38
+ end
39
+
40
+ def test_tag_with_block
41
+ assert_equal "<span>Hi!</span>", @inline.tag(:span) { "Hi!" }
42
+ end
43
+
44
+ def test_tag_with_block_and_attributes
45
+ expected = %[<span class="greetings" id="hi">Hi!</span>]
46
+ assert_equal expected, @inline.tag(:span, :class => "greetings", :id => "hi") { "Hi!" }
47
+ end
48
+
49
+ def test_nested_tags
50
+ expected = %[<span class="greetings"><strong id="hi">Hi!</strong></span>]
51
+ actual = @inline.tag(:span, :class => "greetings") { tag(:strong, "Hi!", :id => "hi") }
52
+ assert_equal expected, actual
53
+ end
54
+
55
+ def test_render_as_list
56
+ @breadcrumbs.add "Home", "/", :class => "home"
57
+ list = parse_tag(@breadcrumbs.render)
58
+ assert_equal "ul", list.name
59
+ assert_equal "breadcrumbs", list['class']
60
+ end
61
+
62
+ def test_render_as_ordered_list
63
+ @breadcrumbs.add "Home", "/"
64
+ list = parse_tag(@breadcrumbs.render(:format => :ordered_list))
65
+ assert_equal "ol", list.name
66
+ assert_equal "breadcrumbs", list['class']
67
+ end
68
+
69
+ def test_render_as_list_with_custom_attributes
70
+ @breadcrumbs.add "Home", "/", :class => "home"
71
+ ul = parse_tag(@breadcrumbs.render(:id => "breadcrumbs", :class => "top"))
72
+ assert_equal "ul", ul.name
73
+ assert_equal "top", ul['class']
74
+ assert_equal "breadcrumbs", ul['id']
75
+ end
76
+
77
+ def test_render_as_list_add_items
78
+ @breadcrumbs.add "Home", "/", :class => "home"
79
+ @breadcrumbs.add "About", "/about", :class => "about"
80
+ @breadcrumbs.add "People"
81
+
82
+ ul = parse_tag(@breadcrumbs.render)
83
+ items = ul.children
84
+
85
+ assert_equal 3, items.count
86
+
87
+ assert_equal "first item-0", items[0]["class"]
88
+ assert_equal %(<a href="/" class="home">Home</a>), items[0].children.join
89
+
90
+ assert_equal "item-1", items[1]["class"]
91
+ assert_equal %(<a href="/about" class="about">About</a>), items[1].children.join
92
+
93
+ assert_equal "last item-2", items[2]["class"]
94
+ assert_equal %(<span>People</span>), items[2].children.join
95
+ end
96
+
97
+ def test_render_inline
98
+ @breadcrumbs.add "Home", "/", :class => "home"
99
+ item = parse_tag(@breadcrumbs.render(:format => :inline))
100
+ assert_not_equal 'ul', item.name
101
+ end
102
+
103
+ def test_render_inline_add_items
104
+ @breadcrumbs.add "Home", "/", :class => "home"
105
+ @breadcrumbs.add "About", "/about", :class => "about"
106
+ @breadcrumbs.add "People"
107
+
108
+ items = parse_tags(@breadcrumbs.render(:format => :inline))
109
+ assert_equal 5, items.count
110
+
111
+ assert_equal "a", items[0].name
112
+ assert_equal "home first item-0", items[0]["class"]
113
+ assert_equal "Home", items[0].children.join
114
+ assert_equal "/", items[0]["href"]
115
+
116
+ assert_equal "span", items[1].name
117
+ assert_equal "separator", items[1]["class"]
118
+ assert_equal "&#187;", items[1].children.join
119
+
120
+ assert_equal "a", items[2].name
121
+ assert_equal "about item-1", items[2]["class"]
122
+ assert_equal "About", items[2].children.join
123
+ assert_equal "/about", items[2]["href"]
124
+
125
+ assert_equal "span", items[3].name
126
+ assert_equal "separator", items[3]["class"]
127
+ assert_equal "&#187;", items[3].children.join
128
+
129
+ assert_equal "span", items[4].name
130
+ assert_equal "last item-2", items[4]["class"]
131
+ assert_equal "People", items[4].children.join
132
+ end
133
+
134
+ def test_render_inline_with_custom_separator
135
+ @breadcrumbs.add "Home", "/", :class => "home"
136
+ @breadcrumbs.add "People"
137
+
138
+ tags = parse_tags(@breadcrumbs.render(:format => :inline, :separator => "|"))
139
+ assert_equal [
140
+ %(<a href="/" class="home first item-0">Home</a>),
141
+ %(<span class="separator">|</span>),
142
+ %(<span class="last item-1">People</span>)
143
+ ], tags.map(&:to_s)
144
+ end
145
+
146
+ def test_render_original_text_when_disabling_translation
147
+ @breadcrumbs.add :home, nil, :i18n => false
148
+ @breadcrumbs.add :people
149
+
150
+ items = parse_tag(@breadcrumbs.render).children
151
+ assert_equal "<span>home</span>", items[0].children.join
152
+ assert_equal "<span>Nosso time</span>", items[1].children.join
153
+ end
154
+
155
+ def test_render_internationalized_text_using_default_scope
156
+ @breadcrumbs.add :home
157
+ @breadcrumbs.add :people
158
+
159
+ items = parse_tag(@breadcrumbs.render).children
160
+ assert_equal "<span>Página inicial</span>", items[0].children.join
161
+ assert_equal "<span>Nosso time</span>", items[1].children.join
162
+ end
163
+
164
+ def test_render_scope_as_text_for_missing_scope
165
+ @breadcrumbs.add :contact
166
+ @breadcrumbs.add "Help"
167
+
168
+ items = parse_tag(@breadcrumbs.render).children
169
+ assert_equal "<span>contact</span>", items[0].children.join
170
+ assert_equal "<span>Help</span>", items[1].children.join
171
+ end
172
+
173
+ def test_pimp_action_controller
174
+ assert @controller.respond_to?(:breadcrumbs)
175
+ assert_equal @controller.breadcrumbs, @controller.breadcrumbs
176
+ end
177
+
178
+ def test_escape_text_when_rendering_inline
179
+ @breadcrumbs.add "<script>alert(1)</script>"
180
+ html = @breadcrumbs.render(:format => :inline)
181
+
182
+ assert_equal %[<span class="first last item-0">&lt;script&gt;alert(1)&lt;/script&gt;</span>], html
183
+ end
184
+
185
+ def test_escape_text_when_rendering_list
186
+ @breadcrumbs.add "<script>alert(1)</script>"
187
+ html = @breadcrumbs.render
188
+
189
+ assert_match /&lt;script&gt;alert\(1\)&lt;\/script&gt;/, html
190
+ end
191
+
192
+ def test_with_polymorphic_urls
193
+ @breadcrumbs.add "Resources", [:tests]
194
+ prefix = "#{@controller.request.scheme}://#{@controller.request.host}"
195
+ tag = parse_tag(@breadcrumbs.render(:format => :inline))
196
+
197
+ assert_equal "a", tag.name
198
+ assert_equal "first last item-0", tag['class']
199
+ assert_equal "#{prefix}/tests", tag['href']
200
+ assert_equal "Resources", tag.children.join
201
+ end
202
+
203
+ private
204
+
205
+ def reject_blanks!(tag)
206
+ tag.children.reject! do |child|
207
+ child.tag? ? (reject_blanks!(child) && false) : child.to_s.blank?
208
+ end
209
+ end
210
+
211
+ def parse_tags(html)
212
+ root = HTML::Document.new(html, true, true).root
213
+ reject_blanks!(root)
214
+ root.children
215
+ end
216
+
217
+ def parse_tag(html)
218
+ parse_tags(html).first
219
+ end
220
+ end
@@ -0,0 +1,6 @@
1
+ pt:
2
+ people: "Nosso time"
3
+
4
+ breadcrumbs:
5
+ home: "Página inicial"
6
+ about: "Sobre"
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ Bundler.require :default, :test
5
+
6
+ require 'test/unit'
7
+ require "breadcrumbs"
8
+ require 'action_controller'
9
+ require 'action_controller/test_case'
10
+
11
+ I18n.load_path << File.dirname(__FILE__) + "/resources/pt.yml"
12
+ I18n.locale = :pt
13
+
14
+ class TestsController < ActionController::Base
15
+ end
16
+
17
+ if ActionPack::VERSION::MAJOR == 3
18
+ routes = ActionDispatch::Routing::RouteSet.new
19
+ routes.draw do
20
+ resources :tests
21
+ end
22
+ TestsController.send(:include, routes.url_helpers)
23
+ else
24
+ ActionController::Routing::Routes.draw do |map|
25
+ map.resources :tests
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bsm-breadcrumbs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Nando Vieira
14
+ - Dimitrij Denissenko
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-11-11 00:00:00 +00:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: actionpack
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 7
31
+ segments:
32
+ - 3
33
+ - 0
34
+ - 0
35
+ version: 3.0.0
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: Breadcrumbs is a simple plugin that adds a `breadcrumbs` object to controllers and views.
39
+ email: dimitrij@blacksquaremedia.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.rdoc
46
+ files:
47
+ - .bundle/config
48
+ - .gitignore
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - README.rdoc
52
+ - Rakefile
53
+ - bsm-breadcrumbs.gemspec
54
+ - lib/breadcrumbs.rb
55
+ - lib/breadcrumbs/action_controller_ext.rb
56
+ - lib/breadcrumbs/render.rb
57
+ - lib/breadcrumbs/render/base.rb
58
+ - lib/breadcrumbs/render/inline.rb
59
+ - lib/breadcrumbs/render/list.rb
60
+ - lib/breadcrumbs/render/ordered_list.rb
61
+ - lib/breadcrumbs/version.rb
62
+ - test/breadcrumbs_test.rb
63
+ - test/resources/pt.yml
64
+ - test/test_helper.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/bsm/breadcrumbs
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Breadcrumbs is a simple plugin that adds a `breadcrumbs` object to controllers and views.
99
+ test_files:
100
+ - test/breadcrumbs_test.rb
101
+ - test/test_helper.rb