vinted-crummy 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rvmrc +48 -0
  4. data/.travis.yml +7 -0
  5. data/Appraisals +9 -0
  6. data/CHANGELOG +47 -0
  7. data/Gemfile +3 -0
  8. data/MIT-LICENSE +20 -0
  9. data/README.md +220 -0
  10. data/Rakefile +25 -0
  11. data/crummy.gemspec +30 -0
  12. data/example/.gitignore +5 -0
  13. data/example/.rspec +1 -0
  14. data/example/.rvmrc +48 -0
  15. data/example/Gemfile +30 -0
  16. data/example/README.md +40 -0
  17. data/example/Rakefile +7 -0
  18. data/example/app/assets/images/rails.png +0 -0
  19. data/example/app/assets/javascripts/application.js +9 -0
  20. data/example/app/assets/javascripts/pages.js.coffee +3 -0
  21. data/example/app/assets/javascripts/post.js.coffee +3 -0
  22. data/example/app/assets/stylesheets/application.css +7 -0
  23. data/example/app/assets/stylesheets/layout.css.sass +26 -0
  24. data/example/app/assets/stylesheets/post.css.scss +3 -0
  25. data/example/app/controllers/application_controller.rb +6 -0
  26. data/example/app/controllers/pages_controller.rb +5 -0
  27. data/example/app/controllers/posts_controller.rb +19 -0
  28. data/example/app/helpers/application_helper.rb +9 -0
  29. data/example/app/helpers/pages_helper.rb +2 -0
  30. data/example/app/helpers/post_helper.rb +2 -0
  31. data/example/app/mailers/.gitkeep +0 -0
  32. data/example/app/models/.gitkeep +0 -0
  33. data/example/app/models/category.rb +6 -0
  34. data/example/app/models/post.rb +19 -0
  35. data/example/app/views/layouts/application.html.haml +16 -0
  36. data/example/app/views/pages/index.html.haml +4 -0
  37. data/example/app/views/posts/index.html.haml +5 -0
  38. data/example/app/views/posts/new.html.haml +1 -0
  39. data/example/app/views/posts/show.html.haml +3 -0
  40. data/example/config.ru +4 -0
  41. data/example/config/application.rb +48 -0
  42. data/example/config/boot.rb +6 -0
  43. data/example/config/database.yml +25 -0
  44. data/example/config/environment.rb +5 -0
  45. data/example/config/environments/development.rb +30 -0
  46. data/example/config/environments/production.rb +60 -0
  47. data/example/config/environments/test.rb +39 -0
  48. data/example/config/initializers/backtrace_silencers.rb +7 -0
  49. data/example/config/initializers/inflections.rb +10 -0
  50. data/example/config/initializers/mime_types.rb +5 -0
  51. data/example/config/initializers/secret_token.rb +7 -0
  52. data/example/config/initializers/session_store.rb +8 -0
  53. data/example/config/initializers/wrap_parameters.rb +14 -0
  54. data/example/config/locales/en.yml +5 -0
  55. data/example/config/routes.rb +5 -0
  56. data/example/db/migrate/20111104103150_create_posts.rb +10 -0
  57. data/example/db/migrate/20111104103738_create_categories.rb +12 -0
  58. data/example/db/migrate/20111104104040_add_category_id_to_posts.rb +5 -0
  59. data/example/db/schema.rb +32 -0
  60. data/example/db/seeds.rb +15 -0
  61. data/example/doc/README_FOR_APP +2 -0
  62. data/example/lib/assets/.gitkeep +0 -0
  63. data/example/lib/tasks/.gitkeep +0 -0
  64. data/example/log/.gitkeep +0 -0
  65. data/example/public/404.html +26 -0
  66. data/example/public/422.html +26 -0
  67. data/example/public/500.html +26 -0
  68. data/example/public/favicon.ico +0 -0
  69. data/example/public/robots.txt +5 -0
  70. data/example/script/rails +6 -0
  71. data/example/spec/controllers/posts_controller_spec.rb +7 -0
  72. data/example/spec/spec_helper.rb +38 -0
  73. data/example/vendor/assets/stylesheets/.gitkeep +0 -0
  74. data/example/vendor/plugins/.gitkeep +0 -0
  75. data/gemfiles/rails3_2.gemfile +8 -0
  76. data/gemfiles/rails4_0.gemfile +8 -0
  77. data/init.rb +1 -0
  78. data/lib/crummy.rb +69 -0
  79. data/lib/crummy/action_controller.rb +83 -0
  80. data/lib/crummy/action_view.rb +29 -0
  81. data/lib/crummy/railtie.rb +20 -0
  82. data/lib/crummy/standard_renderer.rb +170 -0
  83. data/lib/crummy/version.rb +6 -0
  84. data/test/standard_renderer_test.rb +182 -0
  85. metadata +200 -0
@@ -0,0 +1,83 @@
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(lambda { |instance| instance.business_name }, "/")
8
+ # add_crumb("Business") { |instance| instance.business_path }
9
+ #
10
+ # Works like a before_filter so +:only+ and +except+ both work.
11
+ def add_crumb(name, *args)
12
+ options = args.extract_options!
13
+ url = args.first
14
+ raise ArgumentError, "Need more arguments" unless name or options[:record] or block_given?
15
+ raise ArgumentError, "Cannot pass url and use block" if url && block_given?
16
+ before_filter(options) do |instance|
17
+ url = yield instance if block_given?
18
+ url = instance.send url if url.is_a? Symbol
19
+
20
+ if url.present?
21
+ if url.kind_of? Array
22
+ url.map! do |name|
23
+ name.is_a?(Symbol) ? instance.instance_variable_get("@#{name}") : name
24
+ end
25
+ end
26
+ if not url.kind_of? String
27
+ url = instance.send :url_for, url
28
+ end
29
+ end
30
+
31
+ # Get the return value of the name if its a proc.
32
+ name = name.call(instance) if name.is_a?(Proc)
33
+
34
+ _record = instance.instance_variable_get("@#{name}") unless name.kind_of?(String)
35
+ if _record and _record.respond_to? :to_param
36
+ instance.add_crumb(_record.to_s, url || instance.url_for(_record), options)
37
+ else
38
+ instance.add_crumb(name, url, options)
39
+ end
40
+
41
+ # FIXME: url = instance.url_for(name) if name.respond_to?("to_param") && url.nil?
42
+ # FIXME: Add ||= for the name, url above
43
+ end
44
+ end
45
+
46
+ def clear_crumbs
47
+ before_filter do |instance|
48
+ instance.clear_crumbs
49
+ end
50
+ end
51
+ end
52
+
53
+ module InstanceMethods
54
+ # Add a crumb to the crumbs array.
55
+ #
56
+ # add_crumb("Home", "/")
57
+ # add_crumb("Business") { |instance| instance.business_path }
58
+ #
59
+ def add_crumb(name, url=nil, options={})
60
+ crumbs.push [name, url, options]
61
+ end
62
+
63
+ def clear_crumbs
64
+ crumbs.clear
65
+ end
66
+
67
+ # Lists the crumbs as an array
68
+ def crumbs
69
+ get_or_set_ivar "@_crumbs", []
70
+ end
71
+
72
+ def get_or_set_ivar(var, value) # :nodoc:
73
+ instance_variable_set var, instance_variable_get(var) || value
74
+ end
75
+ private :get_or_set_ivar
76
+ end
77
+
78
+ def self.included(receiver) # :nodoc:
79
+ receiver.extend ClassMethods
80
+ receiver.send :include, InstanceMethods
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,29 @@
1
+ module Crummy
2
+ module ViewMethods
3
+ # List the crumbs as an array
4
+ def crumbs
5
+ @_crumbs ||= [] # Give me something to push to
6
+ end
7
+
8
+ # Add a crumb to the +crumbs+ array
9
+ def add_crumb(name, url=nil, options={})
10
+ crumbs.push [name, url, options]
11
+ end
12
+
13
+ # Render the list of crumbs using renderer
14
+ #
15
+ def render_crumbs(options = {})
16
+ raise ArgumentError, "Renderer and block given" if options.has_key?(:renderer) && block_given?
17
+ return yield(crumbs, options) if block_given?
18
+
19
+ @_renderer ||= if options.has_key?(:renderer)
20
+ options.delete(:renderer)
21
+ else
22
+ require 'crummy/standard_renderer'
23
+ Crummy::StandardRenderer.new
24
+ end
25
+
26
+ @_renderer.render_crumbs(crumbs, options)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'crummy'
2
+ require 'rails'
3
+
4
+ module Crummy
5
+ class Railtie < Rails::Railtie
6
+ initializer "crummy.action_controller" do |app|
7
+ ActiveSupport.on_load :action_controller do
8
+ require 'crummy/action_controller'
9
+ include Crummy::ControllerMethods
10
+ end
11
+ end
12
+
13
+ initializer "crummy.action_view" do |app|
14
+ ActiveSupport.on_load :action_view do
15
+ require 'crummy/action_view'
16
+ include Crummy::ViewMethods
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,170 @@
1
+ # encoding: utf-8
2
+
3
+ module Crummy
4
+ class StandardRenderer
5
+ include ActionView::Helpers::UrlHelper
6
+ include ActionView::Helpers::TagHelper unless self.included_modules.include?(ActionView::Helpers::TagHelper)
7
+ ActionView::Helpers::TagHelper::BOOLEAN_ATTRIBUTES.merge([:itemscope].to_set)
8
+
9
+ # Render the list of crumbs as either html or xml
10
+ #
11
+ # Takes 3 options:
12
+ # The output format. Can either be xml or html. Default :html
13
+ # :format => (:html|:xml)
14
+ # The separator text. It does not assume you want spaces on either side so you must specify. Default +&raquo;+ for :html and +crumb+ for xml
15
+ # :separator => string
16
+ # Render links in the output. Default +true+
17
+ # :link => boolean
18
+ #
19
+ # Examples:
20
+ # render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
21
+ # render_crumbs :separator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
22
+ # render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
23
+ # render_crumbs :format => :html_list #=> <ol class="" id=""><li class=""><a href="/">Home</a></li><li class=""><a href="/">Businesses</a></li></ol>
24
+ #
25
+ # With :format => :html_list you can specify additional params: li_class, ol_class, ol_id
26
+ # The only argument is for the separator text. It does not assume you want spaces on either side so you must specify. Defaults to +&raquo;+
27
+ #
28
+ # render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
29
+ #
30
+ def render_crumbs(crumbs, options = {})
31
+
32
+ options[:skip_if_blank] ||= Crummy.configuration.skip_if_blank
33
+ return '' if options[:skip_if_blank] && crumbs.count < 1
34
+ options[:format] ||= Crummy.configuration.format
35
+ options[:separator] ||= Crummy.configuration.send(:"#{options[:format]}_separator")
36
+ options[:right_separator] ||= Crummy.configuration.send(:"#{options[:format]}_right_separator")
37
+ options[:links] ||= Crummy.configuration.links
38
+ options[:first_class] ||= Crummy.configuration.first_class
39
+ options[:last_class] ||= Crummy.configuration.last_class
40
+ options[:microdata] ||= Crummy.configuration.microdata if options[:microdata].nil?
41
+ options[:truncate] ||= Crummy.configuration.truncate if options[:truncate]
42
+ options[:last_crumb_linked] = Crummy.configuration.last_crumb_linked if options[:last_crumb_linked].nil?
43
+ options[:right_side] ||= Crummy.configuration.right_side
44
+
45
+ last_hash = lambda {|o|k=o.map{|c|
46
+ c.is_a?(Hash) ? (c.empty? ? nil: c) : nil}.compact
47
+ k.empty? ? {} : k.last
48
+ }
49
+ local_global = lambda {|crumb, global_options, param_name| last_hash.call(crumb).has_key?(param_name.to_sym) ? last_hash.call(crumb)[param_name.to_sym] : global_options[param_name.to_sym]}
50
+
51
+ case options[:format]
52
+ when :html
53
+ crumb_string = crumbs.map{|crumb|local_global.call(crumb, options, :right_side) ? nil :
54
+ crumb_to_html(crumb,
55
+ local_global.call(crumb, options, :links),
56
+ local_global.call(crumb, options, :first_class),
57
+ local_global.call(crumb, options, :last_class),
58
+ (crumb == crumbs.first),
59
+ (crumb == crumbs.last),
60
+ local_global.call(crumb, options, :microdata),
61
+ local_global.call(crumb, options, :last_crumb_linked),
62
+ local_global.call(crumb, options, :truncate))}.compact.join(options[:separator]).html_safe
63
+ crumb_string
64
+ when :html_list
65
+ # Let's set values for special options of html_list format
66
+ options[:li_class] ||= Crummy.configuration.li_class
67
+ options[:ol_class] ||= Crummy.configuration.ol_class
68
+ options[:ol_id] ||= Crummy.configuration.ol_id
69
+ options[:ol_id] = nil if options[:ol_id].blank?
70
+
71
+ crumb_string = crumbs.map{|crumb|local_global.call(crumb, options, :right_side) ? nil :
72
+ crumb_to_html_list(crumb,
73
+ local_global.call(crumb, options, :links),
74
+ local_global.call(crumb, options, :li_class),
75
+ local_global.call(crumb, options, :first_class),
76
+ local_global.call(crumb, options, :last_class),
77
+ (crumb == crumbs.first),
78
+ (crumb == crumbs.find_all{|crumb|
79
+ !last_hash.call(crumb).fetch(:right_side,false)}.compact.last),
80
+ local_global.call(crumb, options, :microdata),
81
+ local_global.call(crumb, options, :last_crumb_linked),
82
+ local_global.call(crumb, options, :truncate),
83
+ local_global.call(crumb, options, :separator))}.compact.join.html_safe
84
+ crumb_right_string = crumbs.reverse.map{|crumb|!local_global.call(crumb, options, :right_side) ? nil :
85
+
86
+ crumb_to_html_list(crumb,
87
+ local_global.call(crumb, options, :links),
88
+ local_global.call(crumb, options, :li_right_class),
89
+ local_global.call(crumb, options, :first_class),
90
+ local_global.call(crumb, options, :last_class),
91
+ (crumb == crumbs.first),
92
+ (crumb == crumbs.find_all{|crumb|!local_global.call(crumb, options, :right_side)}.compact.last),
93
+ local_global.call(crumb, options, :microdata),
94
+ local_global.call(crumb, options, :last_crumb_linked),
95
+ local_global.call(crumb, options, :truncate),
96
+ local_global.call(crumb, options, :right_separator))}.compact.join.html_safe
97
+ crumb_string = content_tag(:ol,
98
+ crumb_string+crumb_right_string,
99
+ :class => options[:ol_class],
100
+ :id => options[:ol_id])
101
+ crumb_string
102
+ when :xml
103
+ crumbs.collect do |crumb|
104
+ crumb_to_xml(crumb,
105
+ local_global.call(crumb, options, :links),
106
+ local_global.call(crumb, options, :separator),
107
+ (crumb == crumbs.first),
108
+ (crumb == crumbs.last))
109
+ end * ''
110
+ else
111
+ raise ArgumentError, "Unknown breadcrumb output format"
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def crumb_to_html(crumb, links, first_class, last_class, is_first, is_last, with_microdata, last_crumb_linked, truncate)
118
+ html_classes = []
119
+ html_classes << first_class if is_first
120
+ html_classes << last_class if is_last
121
+ name, url, options = crumb
122
+ options = {} unless options.is_a?(Hash)
123
+ can_link = url && links && (!is_last || last_crumb_linked)
124
+ link_html_options = options[:link_html_options] || {}
125
+ link_html_options[:class] = html_classes
126
+ if with_microdata
127
+ item_title = content_tag(:span, (truncate.present? ? name.truncate(truncate) : name), :itemprop => "title")
128
+ html_options = {:itemscope => true, :itemtype => data_definition_url("Breadcrumb")}
129
+ link_html_options[:itemprop] = "url"
130
+ html_content = can_link ? link_to(item_title, url, link_html_options) : item_title
131
+ content_tag(:div, html_content, html_options)
132
+ else
133
+ can_link ? link_to((truncate.present? ? name.truncate(truncate) : name), url, link_html_options) : (truncate.present? ? name.truncate(truncate) : name)
134
+ end
135
+ end
136
+
137
+ def crumb_to_html_list(crumb, links, li_class, first_class, last_class, is_first, is_last, with_microdata, last_crumb_linked, truncate, separator='')
138
+ name, url, options = crumb
139
+ options = {} unless options.is_a?(Hash)
140
+ can_link = url && links && (!is_last || last_crumb_linked) && !(/<\/a/ =~ name)
141
+ html_classes = []
142
+ html_classes << first_class if is_first && !first_class.empty?
143
+ html_classes << last_class if is_last && !last_class.empty?
144
+ html_classes << li_class unless li_class.empty?
145
+ html_options = html_classes.size > 0 ? {:class => html_classes.join(' ').strip} : {}
146
+
147
+ if with_microdata
148
+ html_options[:itemscope] = true
149
+ html_options[:itemtype] = data_definition_url("Breadcrumb")
150
+ item_title = content_tag(:span, (truncate.present? ? name.truncate(truncate) : name), :itemprop => "title")
151
+ link_html_options = options[:link_html_options] || {}
152
+ link_html_options[:itemprop] = "url"
153
+ html_content = can_link ? link_to(item_title, url, link_html_options) : item_title
154
+ else
155
+ html_content = can_link ? link_to((truncate.present? ? name.truncate(truncate) : name), url, options[:link_html_options]) : content_tag(:span, (truncate.present? ? name.truncate(truncate) : name))
156
+ end
157
+ content_tag(:li, html_content, html_options)+(/<\/li/ =~ separator ?
158
+ separator : content_tag(:li, separator) unless separator.blank? || is_last)
159
+ end
160
+
161
+ def crumb_to_xml(crumb, links, separator, is_first, is_last)
162
+ name, url = crumb
163
+ content_tag(separator, name, :href => (url && links ? url : nil))
164
+ end
165
+
166
+ def data_definition_url(type)
167
+ "http://data-vocabulary.org/#{type}"
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,6 @@
1
+ module Crummy
2
+ MAJOR = 1
3
+ MINOR = 9
4
+ PATCH = 0
5
+ VERSION = [MAJOR, MINOR, PATCH].join('.')
6
+ end
@@ -0,0 +1,182 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require(:test)
4
+ require 'test/unit'
5
+
6
+ require 'action_controller'
7
+ require 'active_support/core_ext/string/output_safety'
8
+ require 'action_dispatch/testing/assertions'
9
+ require 'crummy'
10
+ require 'crummy/standard_renderer'
11
+
12
+ class StandardRendererTest < Test::Unit::TestCase
13
+ include ActionDispatch::Assertions
14
+ include Crummy
15
+
16
+ def test_classes
17
+ renderer = StandardRenderer.new
18
+ assert_dom_equal('<a href="url" class="first last">name</a>',
19
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html))
20
+ assert_equal('<ol class=""><li class="first last"><a href="url">name</a></li></ol>',
21
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html_list))
22
+ assert_equal('<crumb href="url">name</crumb>',
23
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :xml))
24
+
25
+ assert_dom_equal('<a href="url1" class="first">name1</a> &raquo; <a href="url2" class="last">name2</a>',
26
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :html))
27
+ assert_equal('<ol class=""><li class="first li_class"><a href="url1">name1</a></li><li class="li_class"><a href="url2">name2</a></li><li class="last li_class"><a href="url3">name3</a></li></ol>',
28
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2'], ['name3', 'url3']], :li_class => "li_class", :first_class => 'first', :last_class => 'last', :format => :html_list))
29
+ assert_equal('<ol class=""><li class="first li_class"><a href="url1">name1</a></li><li> / </li><li class="li_class"><a href="url2">name2</a></li><li> / </li><li class="last li_class"><a href="url3">name3</a></li></ol>',
30
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2'], ['name3', 'url3']], :li_class => "li_class", :first_class => 'first', :last_class => 'last', :format => :html_list, :separator => " / "))
31
+ assert_equal('<crumb href="url1">name1</crumb><crumb href="url2">name2</crumb>',
32
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :xml))
33
+
34
+ assert_dom_equal('<div itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="url" class="first last" itemprop="url"><span itemprop="title">name</span></a></div>',
35
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html, :microdata => true))
36
+ assert_equal('<ol class=""><li class="first last" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><a itemprop="url" href="url"><span itemprop="title">name</span></a></li></ol>',
37
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html_list, :microdata => true))
38
+ assert_equal('<ol class="crumbclass" id="crumbid"><li class="liclass"><a href="url">name</a></li></ol>',
39
+ renderer.render_crumbs([['name', 'url']], :format => :html_list, :ol_id => "crumbid", :ol_class => "crumbclass", :li_class => "liclass"))
40
+ end
41
+
42
+ def test_classes_last_crumb_not_linked
43
+ renderer = StandardRenderer.new
44
+ assert_equal('name',
45
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html, :last_crumb_linked => false))
46
+ assert_equal('<ol class=""><li class="first last"><span>name</span></li></ol>',
47
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html_list, :last_crumb_linked => false))
48
+ assert_equal('<crumb href="url">name</crumb>',
49
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :xml, :last_crumb_linked => false))
50
+
51
+ assert_dom_equal('<a href="url1" class="first">name1</a> &raquo; name2',
52
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :html, :last_crumb_linked => false))
53
+ assert_equal('<ol class=""><li class="first li_class"><a href="url1">name1</a></li><li class="li_class"><a href="url2">name2</a></li><li class="last li_class"><span>name3</span></li></ol>',
54
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2'], ['name3', 'url3']], :li_class => "li_class", :first_class => 'first', :last_class => 'last', :format => :html_list, :last_crumb_linked => false))
55
+ assert_equal('<ol class=""><li class="first li_class"><a href="url1">name1</a></li><li> / </li><li class="li_class"><a href="url2">name2</a></li><li> / </li><li class="last li_class"><span>name3</span></li></ol>',
56
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2'], ['name3', 'url3']], :li_class => "li_class", :first_class => 'first', :last_class => 'last', :format => :html_list, :separator => " / ", :last_crumb_linked => false))
57
+ assert_equal('<crumb href="url1">name1</crumb><crumb href="url2">name2</crumb>',
58
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :first_class => 'first', :last_class => 'last', :format => :xml, :last_crumb_linked => false))
59
+
60
+ assert_equal('<div itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">name</span></div>',
61
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html, :microdata => true, :last_crumb_linked => false))
62
+ assert_equal('<ol class=""><li class="first last" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">name</span></li></ol>',
63
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html_list, :microdata => true, :last_crumb_linked => false))
64
+ assert_equal('<ol class="crumbclass" id="crumbid"><li class="liclass"><span>name</span></li></ol>',
65
+ renderer.render_crumbs([['name', 'url']], :format => :html_list, :ol_id => "crumbid", :ol_class => "crumbclass", :li_class => "liclass", :last_crumb_linked => false))
66
+ end
67
+
68
+ def test_input_object_mutation
69
+ renderer = StandardRenderer.new
70
+ Crummy.configure do |config|
71
+ config.microdata = false
72
+ end
73
+
74
+ name1 = 'name1'
75
+ url1 = nil
76
+ name2 = 'name2'
77
+ url2 = nil
78
+
79
+ renderer.render_crumbs([[name1, url1], [name2, url2]], :format => :html, :microdata => false)
80
+
81
+ # Rendering the crumbs shouldn't alter the input objects.
82
+ assert_equal('name1', name1);
83
+ assert_equal(nil, url2);
84
+ assert_equal('name2', name2);
85
+ assert_equal(nil, url2);
86
+ end
87
+
88
+ def test_link_html_options
89
+ renderer = StandardRenderer.new
90
+ Crummy.configure do |config|
91
+ config.microdata = false
92
+ end
93
+
94
+ assert_dom_equal('<a href="url" class="first last" title="link title">name</a>',
95
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html))
96
+
97
+ assert_equal('name',
98
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html, :last_crumb_linked => false))
99
+
100
+ assert_equal('<ol class=""><li class="first last"><a title="link title" href="url">name</a></li></ol>',
101
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html_list))
102
+
103
+ assert_equal('<ol class=""><li class="first last"><span>name</span></li></ol>',
104
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html_list, :last_crumb_linked => false))
105
+ end
106
+
107
+ def test_link_html_options_with_microdata
108
+ renderer = StandardRenderer.new
109
+ Crummy.configure do |config|
110
+ config.microdata = true
111
+ end
112
+
113
+ assert_dom_equal('<div itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="url" class="first last" itemprop="url" title="link title"><span itemprop="title">name</span></a></div>',
114
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html))
115
+
116
+ assert_equal('<div itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">name</span></div>',
117
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html, :last_crumb_linked => false))
118
+
119
+ assert_equal('<ol class=""><li class="first last" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><a title="link title" itemprop="url" href="url"><span itemprop="title">name</span></a></li></ol>',
120
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html_list))
121
+
122
+ assert_equal('<ol class=""><li class="first last" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">name</span></li></ol>',
123
+ renderer.render_crumbs([['name', 'url', {:link_html_options => {:title => 'link title'}}]], :first_class => 'first', :last_class => 'last', :format => :html_list, :last_crumb_linked => false))
124
+ end
125
+
126
+ def test_inline_configuration
127
+ renderer = StandardRenderer.new
128
+ Crummy.configure do |config|
129
+ config.microdata = true
130
+ config.last_crumb_linked = true
131
+ end
132
+
133
+ assert_no_match(/itemscope/, renderer.render_crumbs([['name', 'url']], :microdata => false))
134
+ assert_match(/href/, renderer.render_crumbs([['name', 'url']], :last_crumb_linked => true))
135
+
136
+ Crummy.configure do |config|
137
+ config.microdata = false
138
+ config.last_crumb_linked = true
139
+ end
140
+
141
+ assert_match(/itemscope/, renderer.render_crumbs([['name', 'url']], :microdata => true))
142
+ assert_no_match(/href/, renderer.render_crumbs([['name', 'url']], :last_crumb_linked => false))
143
+ end
144
+
145
+ def test_configuration
146
+ renderer = StandardRenderer.new
147
+ # check defaults
148
+ assert_equal " &raquo; ", Crummy.configuration.html_separator
149
+ # adjust configuration
150
+ Crummy.configure do |config|
151
+ config.html_separator = " / "
152
+ end
153
+ assert_equal " / ", Crummy.configuration.html_separator
154
+ end
155
+
156
+ def test_configured_renderer
157
+ renderer = StandardRenderer.new
158
+ Crummy.configure do |config|
159
+ config.html_separator = " / "
160
+ end
161
+ # using configured separator
162
+ assert_dom_equal('<a href="url1" class="">name1</a> / <a href="url2" class="">name2</a>',
163
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']]))
164
+ # overriding configured separator
165
+ assert_dom_equal('<a href="url1" class="">name1</a> | <a href="url2" class="">name2</a>',
166
+ renderer.render_crumbs([['name1', 'url1'], ['name2', 'url2']], :separator => " | "))
167
+ end
168
+
169
+ def test_configured_renderer_with_microdata
170
+ renderer = StandardRenderer.new
171
+ Crummy.configure do |config|
172
+ config.microdata = true
173
+ end
174
+ # using configured microdata setting
175
+ assert_dom_equal('<div itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><a href="url" class="first last" itemprop="url"><span itemprop="title">name</span></a></div>',
176
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html))
177
+ # last crumb not linked
178
+ assert_equal('<div itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb"><span itemprop="title">name</span></div>',
179
+ renderer.render_crumbs([['name', 'url']], :first_class => 'first', :last_class => 'last', :format => :html, :last_crumb_linked => false))
180
+ end
181
+
182
+ end