simple-navigation 2.4.2 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ *2.5.0
2
+
3
+ * added new renderer Renderer::Links to simply render the navigation as links inside a div.
4
+ * also make item.name html_safe (in order you have html_code in the item's name). Thanks again, Johan Svensson.
5
+
1
6
  *2.4.2
2
7
 
3
8
  * Rails 3.0.0.beta2 compatibility
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :patch: 0
2
3
  :major: 2
3
- :minor: 4
4
- :patch: 2
4
+ :minor: 5
@@ -27,7 +27,7 @@ module SimpleNavigation
27
27
 
28
28
  # Sets the config's default-settings
29
29
  def initialize
30
- @renderer = SimpleNavigation::Renderer::List
30
+ @renderer = SimpleNavigation.default_renderer || SimpleNavigation::Renderer::List
31
31
  @selected_class = 'selected'
32
32
  @autogenerate_item_ids = true
33
33
  @id_generator = Proc.new {|id| id.to_s }
@@ -43,7 +43,13 @@ module SimpleNavigation
43
43
  def render_sub_navigation_for(item)
44
44
  item.sub_navigation.render(self.options)
45
45
  end
46
-
46
+
47
+ # Marks the specified input as html_safe (for Rails3). Does nothing if html_safe is not defined on input.
48
+ #
49
+ def html_safe(input)
50
+ input.respond_to?(:html_safe) ? input.html_safe : input
51
+ end
52
+
47
53
  # Renders the specified ItemContainer to HTML.
48
54
  #
49
55
  # When implementing a renderer, please consider to call include_sub_navigation? to determin
@@ -0,0 +1,25 @@
1
+ module SimpleNavigation
2
+ module Renderer
3
+
4
+ # Renders an ItemContainer as a <div> element and its containing items as <a> elements.
5
+ # It adds the 'selected' class to the <a> element that is currently active.
6
+ #
7
+ # The Links renderer cannot be used to render nested navigations. If you would like it to use with nested navigations, you have to render each level separately.
8
+ #
9
+ # By default, the renderer sets the item's key as dom_id for the rendered <a> element unless the config option <tt>autogenerate_item_ids</tt> is set to false.
10
+ # The id can also be explicitely specified by setting the id in the html-options of the 'item' method in the config/navigation.rb file.
11
+ # The ItemContainer's dom_class and dom_id are applied to the surrounding <div> element.
12
+ #
13
+ class Links < SimpleNavigation::Renderer::Base
14
+
15
+ def render(item_container)
16
+ div_content = item_container.items.inject([]) do |list, item|
17
+ list << link_to(html_safe(item.name), item.url, {:method => item.method}.merge(item.html_options))
18
+ end.join
19
+ content_tag(:div, html_safe(div_content), {:id => item_container.dom_id, :class => item_container.dom_class})
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -13,14 +13,13 @@ module SimpleNavigation
13
13
  def render(item_container)
14
14
  list_content = item_container.items.inject([]) do |list, item|
15
15
  html_options = item.html_options
16
- li_content = link_to(item.name, item.url, :class => item.selected_class, :method => item.method)
16
+ li_content = link_to(html_safe(item.name), item.url, :class => item.selected_class, :method => item.method)
17
17
  if include_sub_navigation?(item)
18
18
  li_content << render_sub_navigation_for(item)
19
19
  end
20
20
  list << content_tag(:li, li_content, html_options)
21
21
  end.join
22
- list_content = list_content.html_safe if list_content.respond_to?(:html_safe)
23
- content_tag(:ul, list_content, {:id => item_container.dom_id, :class => item_container.dom_class})
22
+ content_tag(:ul, html_safe(list_content), {:id => item_container.dom_id, :class => item_container.dom_class})
24
23
  end
25
24
  end
26
25
 
@@ -8,13 +8,14 @@ require 'simple_navigation/item_container'
8
8
  require 'simple_navigation/items_provider'
9
9
  require 'simple_navigation/renderer/base'
10
10
  require 'simple_navigation/renderer/list'
11
+ require 'simple_navigation/renderer/links'
11
12
  require 'simple_navigation/initializer'
12
13
  require 'simple_navigation/railtie' if Rails::VERSION::MAJOR == 3
13
14
 
14
15
  # A plugin for generating a simple navigation. See README for resources on usage instructions.
15
16
  module SimpleNavigation
16
17
 
17
- mattr_accessor :config_files, :config_file_path, :controller, :template, :explicit_current_navigation, :rails_env, :rails_root
18
+ mattr_accessor :config_files, :config_file_path, :default_renderer, :controller, :template, :explicit_current_navigation, :rails_env, :rails_root
18
19
 
19
20
  self.config_files = {}
20
21
 
@@ -59,6 +59,22 @@ describe SimpleNavigation::Renderer::Base do
59
59
  end
60
60
  end
61
61
 
62
+ describe 'html_safe' do
63
+ before(:each) do
64
+ @input = stub :input
65
+ end
66
+ context 'input does respond to html_safe' do
67
+ before(:each) do
68
+ @safe = stub :safe
69
+ @input.stub!(:html_safe => @safe)
70
+ end
71
+ it {@base_renderer.html_safe(@input).should == @safe}
72
+ end
73
+ context 'input does not respond to html_safe' do
74
+ it {@base_renderer.html_safe(@input).should == @input}
75
+ end
76
+ end
77
+
62
78
  describe 'expand_all?' do
63
79
  context 'option is set' do
64
80
  context 'expand_all is true' do
@@ -0,0 +1,121 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+ require 'html/document' unless defined? HTML::Document
3
+
4
+ describe SimpleNavigation::Renderer::Links do
5
+
6
+ describe 'render' do
7
+
8
+
9
+ def render(current_nav=nil, options={:level => :all})
10
+ primary_navigation = primary_container
11
+ select_item(current_nav) if current_nav
12
+ @renderer = SimpleNavigation::Renderer::Links.new(options)
13
+ HTML::Document.new(@renderer.render(primary_navigation)).root
14
+ end
15
+
16
+ context 'regarding result' do
17
+
18
+ it "should render a div-tag around the items" do
19
+ HTML::Selector.new('div').select(render).should have(1).entries
20
+ end
21
+ it "the rendered div-tag should have the specified dom_id" do
22
+ HTML::Selector.new('div#nav_dom_id').select(render).should have(1).entries
23
+ end
24
+ it "the rendered div-tag should have the specified class" do
25
+ HTML::Selector.new('div.nav_dom_class').select(render).should have(1).entries
26
+ end
27
+ it "should render an a-tag for each item" do
28
+ HTML::Selector.new('a').select(render).should have(3).entries
29
+ end
30
+ it "should pass the specified html_options to the a element" do
31
+ HTML::Selector.new('a[style=float:right]').select(render).should have(1).entries
32
+ end
33
+ it "should give the a-tag the id specified in the html_options" do
34
+ HTML::Selector.new('a#my_id').select(render).should have(1).entries
35
+ end
36
+ it "should give the a-tag the default id (stringified key) if no id is specified in the html_options" do
37
+ HTML::Selector.new('a#invoices').select(render).should have(1).entries
38
+ end
39
+ it "should not apply the the default id where there is an id specified in the html_options" do
40
+ HTML::Selector.new('a#users').select(render).should be_empty
41
+ end
42
+
43
+ context 'with current_navigation set' do
44
+ it "should mark the matching a-item as selected (with the css_class specified in configuration)" do
45
+ HTML::Selector.new('a.selected').select(render(:invoices)).should have(1).entries
46
+ end
47
+ end
48
+
49
+ context 'without current_navigation set' do
50
+ it "should not mark any of the items as selected" do
51
+ HTML::Selector.new('a.selected').select(render).should be_empty
52
+ end
53
+ end
54
+
55
+ # context 'nested sub_navigation' do
56
+ # it "should nest the current_primary's subnavigation inside the selected li-element" do
57
+ # HTML::Selector.new('li.selected ul li').select(render(:invoices)).should have(2).entries
58
+ # end
59
+ # it "should be possible to identify sub items using an html selector (using ids)" do
60
+ # HTML::Selector.new('#invoices #subnav1').select(render(:invoices)).should have(1).entries
61
+ # end
62
+ # context 'expand_all => false' do
63
+ # it "should not render the invoices submenu if the user-primary is active" do
64
+ # HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => false)).should be_empty
65
+ # HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => false)).should be_empty
66
+ # end
67
+ # end
68
+ #
69
+ # context 'expand_all => true' do
70
+ # it "should render the invoices submenu even if the user-primary is active" do
71
+ # HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
72
+ # HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
73
+ # end
74
+ # end
75
+ #
76
+ # end
77
+ end
78
+
79
+ context 'regarding method calls' do
80
+
81
+ context 'regarding the div_content' do
82
+ before(:each) do
83
+ @primary_navigation = primary_container
84
+ @div_content = stub(:div_content)
85
+ @div_items = stub(:div_items, :join => @div_content)
86
+ @items.stub!(:inject => @div_items)
87
+ @renderer = SimpleNavigation::Renderer::Links.new(options)
88
+ end
89
+
90
+ it "should join the list_items" do
91
+ @div_items.should_receive(:join)
92
+ end
93
+
94
+ it "should html_saferize the list_content" do
95
+ @renderer.should_receive(:html_safe).with(@div_content)
96
+ end
97
+
98
+ after(:each) do
99
+ @renderer.render(@primary_navigation)
100
+ end
101
+ end
102
+
103
+ context 'regarding the items' do
104
+ before(:each) do
105
+ @primary_navigation = primary_container
106
+ @renderer = SimpleNavigation::Renderer::Links.new(options)
107
+ end
108
+
109
+ it "should call html_safe on every item's name" do
110
+ @items.each do |item|
111
+ @renderer.should_receive(:html_safe).with(item.name)
112
+ end
113
+ @renderer.should_receive(:html_safe).with(anything)
114
+ @renderer.render(@primary_navigation)
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+ end
@@ -5,48 +5,6 @@ describe SimpleNavigation::Renderer::List do
5
5
 
6
6
  describe 'render' do
7
7
 
8
- def sub_items
9
- [
10
- [:subnav1, 'subnav1', 'subnav1_url', {}],
11
- [:subnav2, 'subnav2', 'subnav2_url', {}]
12
- ]
13
- end
14
-
15
- def primary_items
16
- [
17
- [:users, 'users', 'first_url', {:id => 'my_id'}],
18
- [:invoices, 'invoices', 'second_url', {}],
19
- [:accounts, 'accounts', 'third_url', {:style => 'float:right'}]
20
- ]
21
- end
22
-
23
- def primary_container
24
- container = SimpleNavigation::ItemContainer.new(0)
25
- container.dom_id = 'nav_dom_id'
26
- container.dom_class = 'nav_dom_class'
27
- @items = primary_items.map {|params| SimpleNavigation::Item.new(container, *params)}
28
- @items.each {|i| i.stub!(:selected? => false)}
29
- container.instance_variable_set(:@items, @items)
30
- primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, subnav_container)}
31
- container
32
- end
33
-
34
- def primary_item(key)
35
- yield @items.find {|i| i.key == key}
36
- end
37
-
38
- def select_item(key)
39
- primary_item(key) {|item| item.stub!(:selected? => true)}
40
- end
41
-
42
- def subnav_container
43
- container = SimpleNavigation::ItemContainer.new(1)
44
- items = sub_items.map {|params| SimpleNavigation::Item.new(container, *params)}
45
- items.each {|i| i.stub!(:selected? => false)}
46
- container.instance_variable_set(:@items, items)
47
- container
48
- end
49
-
50
8
  def render(current_nav=nil, options={:level => :all})
51
9
  primary_navigation = primary_container
52
10
  select_item(current_nav) if current_nav
@@ -128,37 +86,42 @@ describe SimpleNavigation::Renderer::List do
128
86
  end
129
87
 
130
88
  context 'regarding method calls' do
131
- before(:each) do
132
- @primary_navigation = primary_container
133
- @list_content = stub(:list_content)
134
- @list_items = stub(:list_items, :join => @list_content)
135
- @items.stub!(:inject => @list_items)
136
- @renderer = SimpleNavigation::Renderer::List.new(options)
137
- end
138
89
 
139
- it "should join the list_items" do
140
- @list_items.should_receive(:join)
141
- end
142
-
143
- context 'html_safe is defined on the joined list_content' do
90
+ context 'regarding the list_content' do
144
91
  before(:each) do
145
- @safe_list_content = stub(:safe_list_content)
146
- @list_content.stub!(:html_safe => @safe_list_content)
92
+ @primary_navigation = primary_container
93
+ @list_content = stub(:list_content)
94
+ @list_items = stub(:list_items, :join => @list_content)
95
+ @items.stub!(:inject => @list_items)
96
+ @renderer = SimpleNavigation::Renderer::List.new(options)
147
97
  end
148
-
149
- it "should create the ul-element with the html_safe content" do
150
- @renderer.should_receive(:content_tag).with(:ul, @safe_list_content, anything)
98
+
99
+ it "should join the list_items" do
100
+ @list_items.should_receive(:join)
151
101
  end
152
- end
153
102
 
154
- context 'html_safe is not defined on the joined list_content' do
155
- it "should create the ul-element with the normal list content" do
156
- @renderer.should_receive(:content_tag).with(:ul, @list_content, anything)
103
+ it "should html_saferize the list_content" do
104
+ @renderer.should_receive(:html_safe).with(@list_content)
105
+ end
106
+
107
+ after(:each) do
108
+ @renderer.render(@primary_navigation)
157
109
  end
158
110
  end
159
-
160
- after(:each) do
161
- @renderer.render(@primary_navigation)
111
+
112
+ context 'regarding the items' do
113
+ before(:each) do
114
+ @primary_navigation = primary_container
115
+ @renderer = SimpleNavigation::Renderer::List.new(options)
116
+ end
117
+
118
+ it "should call html_safe on every item's name" do
119
+ @items.each do |item|
120
+ @renderer.should_receive(:html_safe).with(item.name)
121
+ end
122
+ @renderer.should_receive(:html_safe).with(anything)
123
+ @renderer.render(@primary_navigation)
124
+ end
162
125
  end
163
126
 
164
127
  end
data/spec/spec_helper.rb CHANGED
@@ -19,4 +19,47 @@ SimpleNavigation.rails_root = './'
19
19
 
20
20
  # Spec::Runner.configure do |config|
21
21
  # no special config
22
- # endx
22
+ # endx
23
+
24
+ # spec helper methods
25
+ def sub_items
26
+ [
27
+ [:subnav1, 'subnav1', 'subnav1_url', {}],
28
+ [:subnav2, 'subnav2', 'subnav2_url', {}]
29
+ ]
30
+ end
31
+
32
+ def primary_items
33
+ [
34
+ [:users, 'users', 'first_url', {:id => 'my_id'}],
35
+ [:invoices, 'invoices', 'second_url', {}],
36
+ [:accounts, 'accounts', 'third_url', {:style => 'float:right'}]
37
+ ]
38
+ end
39
+
40
+ def primary_container
41
+ container = SimpleNavigation::ItemContainer.new(0)
42
+ container.dom_id = 'nav_dom_id'
43
+ container.dom_class = 'nav_dom_class'
44
+ @items = primary_items.map {|params| SimpleNavigation::Item.new(container, *params)}
45
+ @items.each {|i| i.stub!(:selected? => false)}
46
+ container.instance_variable_set(:@items, @items)
47
+ primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, subnav_container)}
48
+ container
49
+ end
50
+
51
+ def primary_item(key)
52
+ yield @items.find {|i| i.key == key}
53
+ end
54
+
55
+ def select_item(key)
56
+ primary_item(key) {|item| item.stub!(:selected? => true)}
57
+ end
58
+
59
+ def subnav_container
60
+ container = SimpleNavigation::ItemContainer.new(1)
61
+ items = sub_items.map {|params| SimpleNavigation::Item.new(container, *params)}
62
+ items.each {|i| i.stub!(:selected? => false)}
63
+ container.instance_variable_set(:@items, items)
64
+ container
65
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 2
7
- - 4
8
- - 2
9
- version: 2.4.2
7
+ - 5
8
+ - 0
9
+ version: 2.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andi Schacke
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-11 00:00:00 +02:00
17
+ date: 2010-04-13 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - lib/simple_navigation/items_provider.rb
60
60
  - lib/simple_navigation/railtie.rb
61
61
  - lib/simple_navigation/renderer/base.rb
62
+ - lib/simple_navigation/renderer/links.rb
62
63
  - lib/simple_navigation/renderer/list.rb
63
64
  - rails/init.rb
64
65
  - spec/lib/simple_navigation/configuration_spec.rb
@@ -69,6 +70,7 @@ files:
69
70
  - spec/lib/simple_navigation/item_spec.rb
70
71
  - spec/lib/simple_navigation/items_provider_spec.rb
71
72
  - spec/lib/simple_navigation/renderer/base_spec.rb
73
+ - spec/lib/simple_navigation/renderer/links_spec.rb
72
74
  - spec/lib/simple_navigation/renderer/list_spec.rb
73
75
  - spec/lib/simple_navigation_spec.rb
74
76
  - spec/spec_helper.rb
@@ -112,6 +114,7 @@ test_files:
112
114
  - spec/lib/simple_navigation/item_spec.rb
113
115
  - spec/lib/simple_navigation/items_provider_spec.rb
114
116
  - spec/lib/simple_navigation/renderer/base_spec.rb
117
+ - spec/lib/simple_navigation/renderer/links_spec.rb
115
118
  - spec/lib/simple_navigation/renderer/list_spec.rb
116
119
  - spec/lib/simple_navigation_spec.rb
117
120
  - spec/spec_helper.rb