simple-navigation 2.7.3 → 3.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/CHANGELOG +14 -4
  2. data/README +1 -2
  3. data/Rakefile +1 -0
  4. data/VERSION.yml +4 -4
  5. data/generators/navigation_config/templates/config/navigation.rb +14 -14
  6. data/lib/simple_navigation.rb +69 -151
  7. data/lib/simple_navigation/adapters.rb +9 -0
  8. data/lib/simple_navigation/adapters/base.rb +37 -0
  9. data/lib/simple_navigation/adapters/padrino.rb +20 -0
  10. data/lib/simple_navigation/adapters/rails.rb +93 -0
  11. data/lib/simple_navigation/adapters/sinatra.rb +76 -0
  12. data/lib/simple_navigation/core.rb +5 -0
  13. data/lib/simple_navigation/{configuration.rb → core/configuration.rb} +14 -19
  14. data/lib/simple_navigation/{item.rb → core/item.rb} +10 -11
  15. data/lib/simple_navigation/{item_adapter.rb → core/item_adapter.rb} +11 -7
  16. data/lib/simple_navigation/{item_container.rb → core/item_container.rb} +14 -21
  17. data/lib/simple_navigation/{items_provider.rb → core/items_provider.rb} +7 -7
  18. data/lib/simple_navigation/{controller_methods.rb → rails_controller_methods.rb} +67 -5
  19. data/lib/simple_navigation/rendering.rb +10 -0
  20. data/lib/simple_navigation/{helpers.rb → rendering/helpers.rb} +16 -18
  21. data/lib/simple_navigation/{renderer → rendering/renderer}/base.rb +20 -37
  22. data/lib/simple_navigation/{renderer → rendering/renderer}/breadcrumbs.rb +7 -7
  23. data/lib/simple_navigation/{renderer → rendering/renderer}/links.rb +7 -7
  24. data/lib/simple_navigation/{renderer → rendering/renderer}/list.rb +6 -6
  25. data/rails/init.rb +1 -5
  26. data/spec/lib/simple_navigation/adapters/padrino_spec.rb +29 -0
  27. data/spec/lib/simple_navigation/adapters/rails_spec.rb +269 -0
  28. data/spec/lib/simple_navigation/adapters/sinatra_spec.rb +60 -0
  29. data/spec/lib/simple_navigation/{configuration_spec.rb → core/configuration_spec.rb} +7 -18
  30. data/spec/lib/simple_navigation/{item_adapter_spec.rb → core/item_adapter_spec.rb} +11 -11
  31. data/spec/lib/simple_navigation/{item_container_spec.rb → core/item_container_spec.rb} +29 -46
  32. data/spec/lib/simple_navigation/{item_spec.rb → core/item_spec.rb} +30 -64
  33. data/spec/lib/simple_navigation/{items_provider_spec.rb → core/items_provider_spec.rb} +7 -7
  34. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +251 -0
  35. data/spec/lib/simple_navigation/{helpers_spec.rb → rendering/helpers_spec.rb} +34 -53
  36. data/spec/lib/simple_navigation/{renderer → rendering/renderer}/base_spec.rb +11 -62
  37. data/spec/lib/simple_navigation/{renderer → rendering/renderer}/breadcrumbs_spec.rb +9 -51
  38. data/spec/lib/simple_navigation/rendering/renderer/links_spec.rb +59 -0
  39. data/spec/lib/simple_navigation/{renderer → rendering/renderer}/list_spec.rb +17 -58
  40. data/spec/lib/simple_navigation_spec.rb +51 -298
  41. data/spec/spec_helper.rb +16 -5
  42. metadata +67 -48
  43. data/lib/simple_navigation/initializer.rb +0 -21
  44. data/lib/simple_navigation/railtie.rb +0 -7
  45. data/spec/lib/simple_navigation/controller_methods_spec.rb +0 -90
  46. data/spec/lib/simple_navigation/renderer/links_spec.rb +0 -121
@@ -0,0 +1,251 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe 'explicit navigation in rails' do
4
+ require 'simple_navigation/rails_controller_methods'
5
+
6
+ it 'should have enhanced the ActionController after loading the extensions' do
7
+ ActionController::Base.instance_methods.map {|m| m.to_s}.should include('current_navigation')
8
+ end
9
+
10
+ describe SimpleNavigation::ControllerMethods do
11
+
12
+ def stub_loading_config
13
+ SimpleNavigation::Configuration.stub!(:load)
14
+ end
15
+
16
+ before(:each) do
17
+ stub_loading_config
18
+ class TestController
19
+ class << self
20
+ def helper_method(*args)
21
+ @helper_methods = args
22
+ end
23
+ def before_filter(*args)
24
+ @before_filters = args
25
+ end
26
+ end
27
+ end
28
+ TestController.send(:include, SimpleNavigation::ControllerMethods)
29
+ @controller = TestController.new
30
+ end
31
+
32
+ describe 'when being included' do
33
+ it "should extend the ClassMethods" do
34
+ @controller.class.should respond_to(:navigation)
35
+ end
36
+ it "should include the InstanceMethods" do
37
+ @controller.should respond_to(:current_navigation)
38
+ end
39
+ end
40
+
41
+ describe 'class_methods' do
42
+
43
+ describe 'navigation' do
44
+
45
+ def call_navigation(key1, key2=nil)
46
+ ActiveSupport::Deprecation.silence do
47
+ @controller.class_eval do
48
+ navigation key1, key2
49
+ end
50
+ end
51
+ end
52
+
53
+ it "should not have an instance-method 'sn_set_navigation' if navigation-method has not been called" do
54
+ @controller.respond_to?(:sn_set_navigation).should be_false
55
+ end
56
+ it 'should create an instance-method "sn_set_navigation" when being called' do
57
+ call_navigation(:key)
58
+ @controller.respond_to?(:sn_set_navigation).should be_true
59
+ end
60
+ it "the created method should not be public" do
61
+ call_navigation(:key)
62
+ @controller.public_methods.map(&:to_sym).should_not include(:sn_set_navigation)
63
+ end
64
+ it "the created method should be protected" do
65
+ call_navigation(:key)
66
+ @controller.protected_methods.map(&:to_sym).should include(:sn_set_navigation)
67
+ end
68
+ it 'the created method should call current_navigation with the specified keys' do
69
+ call_navigation(:primary, :secondary)
70
+ @controller.should_receive(:current_navigation).with(:primary, :secondary)
71
+ @controller.send(:sn_set_navigation)
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ describe 'instance_methods' do
78
+
79
+ describe 'current_navigation' do
80
+ it "should set the sn_current_navigation_args as specified" do
81
+ ActiveSupport::Deprecation.silence {@controller.current_navigation(:first)}
82
+ @controller.instance_variable_get(:@sn_current_navigation_args).should == [:first]
83
+ end
84
+ it "should set the sn_current_navigation_args as specified" do
85
+ ActiveSupport::Deprecation.silence {@controller.current_navigation(:first, :second)}
86
+ @controller.instance_variable_get(:@sn_current_navigation_args).should == [:first, :second]
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ describe 'SimpleNavigation module additions' do
95
+
96
+ describe 'handle_explicit_navigation' do
97
+ def args(*args)
98
+ SimpleNavigation.stub!(:explicit_navigation_args => args.compact.empty? ? nil : args)
99
+ end
100
+
101
+ before(:each) do
102
+ @controller = stub(:controller)
103
+ @adapter = stub(:adapter, :controller => @controller)
104
+ SimpleNavigation.stub!(:adapter => @adapter)
105
+ end
106
+
107
+ context 'with explicit navigation set' do
108
+ context 'list of navigations' do
109
+ before(:each) do
110
+ args :first, :second, :third
111
+ end
112
+ it "should set the correct instance var in the controller" do
113
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_3, :third)
114
+ SimpleNavigation.handle_explicit_navigation
115
+ end
116
+ end
117
+ context 'single navigation' do
118
+ context 'specified key is a valid navigation item' do
119
+ before(:each) do
120
+ @primary = stub(:primary, :level_for_item => 2)
121
+ SimpleNavigation.stub!(:primary_navigation => @primary)
122
+ args :key
123
+ end
124
+ it "should set the correct instance var in the controller" do
125
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
126
+ SimpleNavigation.handle_explicit_navigation
127
+ end
128
+ end
129
+ context 'specified key is an invalid navigation item' do
130
+ before(:each) do
131
+ @primary = stub(:primary, :level_for_item => nil)
132
+ SimpleNavigation.stub!(:primary_navigation => @primary)
133
+ args :key
134
+ end
135
+ it "should raise an ArgumentError" do
136
+ lambda {SimpleNavigation.handle_explicit_navigation}.should raise_error(ArgumentError)
137
+ end
138
+ end
139
+ end
140
+ context 'hash with level' do
141
+ before(:each) do
142
+ args :level_2 => :key
143
+ end
144
+ it "should set the correct instance var in the controller" do
145
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
146
+ SimpleNavigation.handle_explicit_navigation
147
+ end
148
+ end
149
+ context 'hash with multiple_levels' do
150
+ before(:each) do
151
+ args :level_2 => :key, :level_1 => :bla
152
+ end
153
+ it "should set the correct instance var in the controller" do
154
+ @controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
155
+ SimpleNavigation.handle_explicit_navigation
156
+ end
157
+ end
158
+ end
159
+ context 'without explicit navigation set' do
160
+ before(:each) do
161
+ args nil
162
+ end
163
+ it "should not set the current_navigation instance var in the controller" do
164
+ @controller.should_not_receive(:instance_variable_set)
165
+ SimpleNavigation.handle_explicit_navigation
166
+ end
167
+ end
168
+ end
169
+
170
+ describe 'current_navigation_for' do
171
+ before(:each) do
172
+ @controller = stub(:controller)
173
+ @adapter = stub(:adapter, :controller => @controller)
174
+ SimpleNavigation.stub!(:adapter => @adapter)
175
+ end
176
+ it "should access the correct instance_var in the controller" do
177
+ @controller.should_receive(:instance_variable_get).with(:@sn_current_navigation_1)
178
+ SimpleNavigation.current_navigation_for(1)
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ describe SimpleNavigation::Item do
185
+ before(:each) do
186
+ @item_container = stub(:item_container, :level => 1)
187
+ @item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
188
+
189
+ end
190
+ describe 'selected_by_config?' do
191
+ context 'navigation explicitly set' do
192
+ it "should return true if current matches key" do
193
+ SimpleNavigation.stub!(:current_navigation_for => :my_key)
194
+ @item.should be_selected_by_config
195
+ end
196
+ it "should return false if current does not match key" do
197
+ SimpleNavigation.stub!(:current_navigation_for => :other_key)
198
+ @item.should_not be_selected_by_config
199
+ end
200
+ end
201
+ context 'navigation not explicitly set' do
202
+ before(:each) do
203
+ SimpleNavigation.stub!(:current_navigation_for => nil)
204
+ end
205
+ it {@item.should_not be_selected_by_config}
206
+ end
207
+ end
208
+ end
209
+
210
+ describe SimpleNavigation::ItemContainer do
211
+ describe 'selected_item' do
212
+ before(:each) do
213
+ SimpleNavigation.stub!(:current_navigation_for)
214
+ @item_container = SimpleNavigation::ItemContainer.new
215
+
216
+ @item_1 = stub(:item, :selected? => false)
217
+ @item_2 = stub(:item, :selected? => false)
218
+ @item_container.instance_variable_set(:@items, [@item_1, @item_2])
219
+ end
220
+ context 'navigation explicitely set' do
221
+ before(:each) do
222
+ @item_container.stub!(:[] => @item_1)
223
+ end
224
+ it "should return the explicitely selected item" do
225
+ @item_container.selected_item.should == @item_1
226
+ end
227
+ end
228
+ context 'navigation not explicitely set' do
229
+ before(:each) do
230
+ @item_container.stub!(:[] => nil)
231
+ end
232
+ context 'no item selected' do
233
+ it "should return nil" do
234
+ @item_container.selected_item.should be_nil
235
+ end
236
+ end
237
+ context 'one item selected' do
238
+ before(:each) do
239
+ @item_1.stub!(:selected? => true)
240
+ end
241
+ it "should return the selected item" do
242
+ @item_container.selected_item.should == @item_1
243
+ end
244
+ end
245
+ end
246
+ end
247
+
248
+ end
249
+
250
+ end
251
+
@@ -1,10 +1,10 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
2
 
3
3
  describe SimpleNavigation::Helpers do
4
4
  class ControllerMock
5
5
  include SimpleNavigation::Helpers
6
6
  end
7
-
7
+
8
8
  before(:each) do
9
9
  @controller = ControllerMock.new
10
10
  SimpleNavigation.stub!(:load_config)
@@ -13,49 +13,30 @@ describe SimpleNavigation::Helpers do
13
13
  SimpleNavigation.stub!(:primary_navigation).and_return(@primary_navigation)
14
14
  SimpleNavigation.stub!(:config_file? => true)
15
15
  end
16
-
16
+
17
17
  describe 'render_navigation' do
18
-
19
- context 'with config_file present' do
20
- before(:each) do
21
- SimpleNavigation.stub!(:config_file? => true)
22
- end
23
- describe 'regarding loading of the config-file' do
24
- context 'no options specified' do
25
- it "should load the config-file for the default context" do
26
- SimpleNavigation.should_receive(:load_config).with(:default)
27
- @controller.render_navigation
28
- end
29
- end
30
-
31
- context 'with options specified' do
32
- it "should load the config-file for the specified context" do
33
- SimpleNavigation.should_receive(:load_config).with(:my_context)
34
- @controller.render_navigation(:context => :my_context)
35
- end
18
+
19
+ describe 'regarding loading of the config-file' do
20
+ context 'no options specified' do
21
+ it "should load the config-file for the default context" do
22
+ SimpleNavigation.should_receive(:load_config).with(:default)
23
+ @controller.render_navigation
36
24
  end
37
25
  end
38
-
39
- it "should eval the config on every request" do
40
- SimpleNavigation::Configuration.should_receive(:eval_config).with(@controller, :default)
41
- @controller.render_navigation
26
+
27
+ context 'with options specified' do
28
+ it "should load the config-file for the specified context" do
29
+ SimpleNavigation.should_receive(:load_config).with(:my_context)
30
+ @controller.render_navigation(:context => :my_context)
31
+ end
42
32
  end
43
33
  end
44
- context 'with config_file not present' do
45
- before(:each) do
46
- SimpleNavigation.stub!(:config_file? => false)
47
- end
48
- it "should not load the config file" do
49
- SimpleNavigation.should_not_receive(:load_config)
50
- @controller.render_navigation
51
- end
52
-
53
- it "should not eval the config on every request" do
54
- SimpleNavigation::Configuration.should_not_receive(:eval_config)
55
- @controller.render_navigation
56
- end
34
+
35
+ it "should eval the config on every request" do
36
+ SimpleNavigation::Configuration.should_receive(:eval_config).with(:default)
37
+ @controller.render_navigation
57
38
  end
58
-
39
+
59
40
  describe 'regarding setting of items' do
60
41
  context 'not items specified in options' do
61
42
  it "should not set the items directly" do
@@ -73,14 +54,14 @@ describe SimpleNavigation::Helpers do
73
54
  end
74
55
  end
75
56
  end
76
-
57
+
77
58
  describe 'no primary navigation defined' do
78
59
  before(:each) do
79
60
  SimpleNavigation.stub!(:primary_navigation => nil)
80
61
  end
81
62
  it {lambda {@controller.render_navigation}.should raise_error}
82
63
  end
83
-
64
+
84
65
  context 'rendering of the item_container' do
85
66
  before(:each) do
86
67
  @active_item_container = stub(:item_container, :null_object => true)
@@ -106,7 +87,7 @@ describe SimpleNavigation::Helpers do
106
87
  end
107
88
  end
108
89
  end
109
-
90
+
110
91
  context 'primary' do
111
92
  it "should call render on the primary_navigation" do
112
93
  @primary_navigation.should_receive(:render)
@@ -121,7 +102,7 @@ describe SimpleNavigation::Helpers do
121
102
  @controller.render_navigation(:level => 1)
122
103
  end
123
104
  end
124
-
105
+
125
106
  context 'secondary' do
126
107
  context 'with current_primary_navigation set' do
127
108
  before(:each) do
@@ -153,16 +134,16 @@ describe SimpleNavigation::Helpers do
153
134
  lambda{ActiveSupport::Deprecation.silence {@controller.render_navigation(:secondary)}}.should_not raise_error
154
135
  end
155
136
  end
156
-
137
+
157
138
  end
158
-
139
+
159
140
  context 'nested' do
160
141
  it "should call render on the primary navigation with the :level => :all option set" do
161
142
  @primary_navigation.should_receive(:render).with(:level => :all)
162
143
  ActiveSupport::Deprecation.silence {@controller.render_navigation(:nested)}
163
144
  end
164
145
  end
165
-
146
+
166
147
  context 'unknown level' do
167
148
  it "should raise an error" do
168
149
  lambda {ActiveSupport::Deprecation.silence {@controller.render_navigation(:unknown)}}.should raise_error(ArgumentError)
@@ -175,8 +156,8 @@ describe SimpleNavigation::Helpers do
175
156
  end
176
157
  end
177
158
  end
178
-
179
- describe 'render_primary_navigation' do
159
+
160
+ describe 'render_primary_navigation' do
180
161
  it "should delegate to render_navigation(:level => 1)" do
181
162
  ActiveSupport::Deprecation.silence do
182
163
  @controller.should_receive(:render_navigation).with(:level => 1)
@@ -184,7 +165,7 @@ describe SimpleNavigation::Helpers do
184
165
  end
185
166
  end
186
167
  end
187
-
168
+
188
169
  describe 'render_sub_navigation' do
189
170
  it "should delegate to render_navigation(:level => 2)" do
190
171
  ActiveSupport::Deprecation.silence do
@@ -193,7 +174,7 @@ describe SimpleNavigation::Helpers do
193
174
  end
194
175
  end
195
176
  end
196
-
177
+
197
178
  describe "should treat :level and :levels options the same" do
198
179
  before(:each) do
199
180
  @selected_item_container = stub(:selected_container, :null_object => true)
@@ -204,12 +185,12 @@ describe SimpleNavigation::Helpers do
204
185
  @controller.render_navigation(:levels => 2)
205
186
  end
206
187
  end
207
-
188
+
208
189
  describe 'option all_open should work as expand_all' do
209
190
  it "should call render on the primary navigation with the include_subnavigation option set" do
210
191
  @primary_navigation.should_receive(:render).with(:level => :all, :expand_all => true)
211
192
  ActiveSupport::Deprecation.silence {@controller.render_navigation(:level => :all, :all_open => true)}
212
- end
193
+ end
213
194
  end
214
-
195
+
215
196
  end
@@ -1,80 +1,29 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
1
+ require File.dirname(__FILE__) + '/../../../../spec_helper'
2
2
 
3
3
  describe SimpleNavigation::Renderer::Base do
4
4
  before(:each) do
5
5
  @options = stub(:options, :null_object => true)
6
- @controller = stub(:controller)
7
- SimpleNavigation.stub!(:controller).and_return(@controller)
6
+ @adapter = stub(:adapter)
7
+ SimpleNavigation.stub!(:adapter => @adapter)
8
8
  @base_renderer = SimpleNavigation::Renderer::Base.new(@options)
9
9
  end
10
- it "should inclue ActionView::Helpers::UrlHelper" do
11
- @base_renderer.should respond_to(:link_to)
12
- end
13
- it "should include ActionView::Helpers::TagHelper" do
14
- @base_renderer.should respond_to(:content_tag)
15
- end
16
10
 
17
11
  describe 'delegated methods' do
18
- it {@base_renderer.should respond_to(:form_authenticity_token)}
19
- it {@base_renderer.should respond_to(:protect_against_forgery?)}
20
- it {@base_renderer.should respond_to(:request_forgery_protection_token)}
12
+ it {@base_renderer.should respond_to(:link_to)}
13
+ it {@base_renderer.should respond_to(:content_tag)}
21
14
  end
22
-
15
+
23
16
  describe 'initialize' do
24
- it {@base_renderer.controller.should == @controller}
17
+ it {@base_renderer.adapter.should == @adapter}
25
18
  it {@base_renderer.options.should == @options}
26
19
  end
27
-
28
- describe 'controller_method' do
29
- context 'delegate a single method' do
30
- before(:each) do
31
- @base_renderer.class_eval do
32
- controller_method :my_method
33
- end
34
- end
35
- it 'should delegate a controller_method to the controller' do
36
- @controller.should_receive(:my_method)
37
- @base_renderer.my_method
38
- end
39
- end
40
20
 
41
- context 'delegate multiple methods' do
42
- before(:each) do
43
- @base_renderer.class_eval do
44
- controller_method :test1, :test2
45
- end
46
- end
47
- it 'should delegate all controller_methods to the controller' do
48
- @controller.should_receive(:test1)
49
- @base_renderer.test1
50
- @controller.should_receive(:test2)
51
- @base_renderer.test2
52
- end
53
- end
54
- end
55
-
56
21
  describe 'render' do
57
22
  it "be subclass responsability" do
58
23
  lambda {@base_renderer.render(:container)}.should raise_error('subclass responsibility')
59
24
  end
60
25
  end
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
-
26
+
78
27
  describe 'expand_all?' do
79
28
  context 'option is set' do
80
29
  context 'expand_all is true' do
@@ -97,7 +46,7 @@ describe SimpleNavigation::Renderer::Base do
97
46
  it {@base_renderer.expand_all?.should be_false}
98
47
  end
99
48
  end
100
-
49
+
101
50
  describe 'skip_if_empty?' do
102
51
  context 'option is set' do
103
52
  context 'skip_if_empty is true' do
@@ -155,7 +104,7 @@ describe SimpleNavigation::Renderer::Base do
155
104
  before(:each) do
156
105
  @base_renderer.stub!(:level => 'unknown')
157
106
  end
158
- it {@base_renderer.send(:consider_sub_navigation?, @item).should be_false}
107
+ it {@base_renderer.send(:consider_sub_navigation?, @item).should be_false}
159
108
  end
160
109
  context 'level is :all' do
161
110
  before(:each) do
@@ -184,7 +133,7 @@ describe SimpleNavigation::Renderer::Base do
184
133
  @sub_navigation.stub!(:level => 3)
185
134
  end
186
135
  it {@base_renderer.send(:consider_sub_navigation?, @item).should be_true}
187
-
136
+
188
137
  end
189
138
  context 'subnavs level < range.max' do
190
139
  before(:each) do