simple-navigation 3.11.0 → 3.12.0

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/.gitignore +6 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +4 -0
  4. data/CHANGELOG +11 -0
  5. data/Gemfile +2 -16
  6. data/Guardfile +5 -0
  7. data/LICENSE +22 -0
  8. data/README.md +40 -0
  9. data/Rakefile +6 -39
  10. data/generators/navigation_config/templates/config/navigation.rb +7 -5
  11. data/init.rb +1 -0
  12. data/install.rb +5 -0
  13. data/lib/simple_navigation/adapters/rails.rb +5 -1
  14. data/lib/simple_navigation/core/configuration.rb +5 -1
  15. data/lib/simple_navigation/core/item.rb +2 -1
  16. data/lib/simple_navigation/core/item_adapter.rb +4 -4
  17. data/lib/simple_navigation/core/item_container.rb +6 -1
  18. data/lib/simple_navigation/rendering/renderer/breadcrumbs.rb +2 -2
  19. data/lib/simple_navigation/rendering/renderer/links.rb +2 -2
  20. data/lib/simple_navigation/rendering/renderer/list.rb +1 -1
  21. data/lib/simple_navigation/version.rb +3 -0
  22. data/lib/simple_navigation.rb +1 -0
  23. data/simple-navigation.gemspec +40 -0
  24. data/spec/initializers/have_css_matcher.rb +13 -0
  25. data/spec/lib/simple_navigation/adapters/padrino_spec.rb +23 -25
  26. data/spec/lib/simple_navigation/adapters/rails_spec.rb +276 -250
  27. data/spec/lib/simple_navigation/adapters/sinatra_spec.rb +64 -53
  28. data/spec/lib/simple_navigation/core/configuration_spec.rb +128 -106
  29. data/spec/lib/simple_navigation/core/item_adapter_spec.rb +144 -168
  30. data/spec/lib/simple_navigation/core/item_container_spec.rb +361 -339
  31. data/spec/lib/simple_navigation/core/item_spec.rb +571 -434
  32. data/spec/lib/simple_navigation/core/items_provider_spec.rb +35 -49
  33. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +194 -173
  34. data/spec/lib/simple_navigation/rendering/helpers_spec.rb +381 -225
  35. data/spec/lib/simple_navigation/rendering/renderer/base_spec.rb +205 -164
  36. data/spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb +87 -67
  37. data/spec/lib/simple_navigation/rendering/renderer/json_spec.rb +52 -31
  38. data/spec/lib/simple_navigation/rendering/renderer/links_spec.rb +75 -48
  39. data/spec/lib/simple_navigation/rendering/renderer/list_spec.rb +62 -174
  40. data/spec/lib/simple_navigation/rendering/renderer/text_spec.rb +41 -28
  41. data/spec/lib/simple_navigation_spec.rb +207 -225
  42. data/spec/spec_helper.rb +53 -82
  43. data/uninstall.rb +1 -0
  44. metadata +100 -22
  45. data/README +0 -22
  46. data/VERSION +0 -1
@@ -1,287 +1,313 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SimpleNavigation::Adapters::Rails do
3
+ module SimpleNavigation
4
+ module Adapters
5
+ describe Rails do
6
+ let(:action_controller) { ActionController::Base }
7
+ let(:adapter) { SimpleNavigation::Adapters::Rails.new(context) }
8
+ let(:context) { double(:context, controller: controller) }
9
+ let(:controller) { double(:controller) }
10
+ let(:request) { double(:request) }
11
+ let(:simple_navigation) { SimpleNavigation }
12
+ let(:template) { double(:template, request: request) }
4
13
 
5
- def create_adapter
6
- SimpleNavigation::Adapters::Rails.new(@context)
7
- end
14
+ describe '.register' do
15
+ before { action_controller.stub(:include) }
8
16
 
9
- before(:each) do
10
- @context = stub(:context)
11
- @controller = stub(:controller)
12
- @context.stub!(:controller => @controller)
13
- @request = stub(:request)
14
- @template = stub(:template, :request => @request)
15
- @adapter = create_adapter
16
- end
17
-
18
- describe 'self.register' do
19
- before(:each) do
20
- ActionController::Base.stub!(:include)
21
- end
22
- it "should call set_env" do
23
- SimpleNavigation.should_receive(:set_env).with('./', 'test')
24
- SimpleNavigation.register
25
- end
26
- it "should extend the ActionController::Base with the Helpers" do
27
- ActionController::Base.should_receive(:include).with(SimpleNavigation::Helpers)
28
- SimpleNavigation.register
29
- end
30
- it "should install the helper methods in the controller" do
31
- ActionController::Base.should_receive(:helper_method).with(:render_navigation)
32
- ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_name)
33
- ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_key)
34
- ActionController::Base.should_receive(:helper_method).with(:active_navigation_item)
35
- ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_container)
36
- SimpleNavigation.register
37
- end
38
-
39
- end
17
+ it 'calls set_env' do
18
+ expect(simple_navigation).to receive(:set_env).with('./', 'test')
19
+ simple_navigation.register
20
+ end
40
21
 
41
- describe 'initialize' do
42
- context 'regarding setting the request' do
43
- context 'template is present' do
44
- before(:each) do
45
- @controller.stub!(:instance_variable_get => @template)
46
- @adapter = create_adapter
22
+ it 'extends the ActionController::Base with the Helpers' do
23
+ expect(action_controller).to receive(:include)
24
+ .with(SimpleNavigation::Helpers)
25
+ simple_navigation.register
47
26
  end
48
- it {@adapter.request.should == @request}
49
- end
50
- context 'template is not present' do
51
- before(:each) do
52
- @controller.stub!(:instance_variable_get => nil)
27
+
28
+ it 'installs the helper methods in the controller' do
29
+ expect(action_controller).to receive(:helper_method).with(:render_navigation)
30
+ expect(action_controller).to receive(:helper_method).with(:active_navigation_item_name)
31
+ expect(action_controller).to receive(:helper_method).with(:active_navigation_item_key)
32
+ expect(action_controller).to receive(:helper_method).with(:active_navigation_item)
33
+ expect(action_controller).to receive(:helper_method).with(:active_navigation_item_container)
34
+ simple_navigation.register
53
35
  end
54
- it {@adapter.request.should be_nil}
55
36
  end
56
- end
57
- context 'regarding setting the controller' do
58
- it "should set the controller" do
59
- @adapter.controller.should == @controller
60
- end
61
- end
62
- context 'regarding setting the template' do
63
- context 'template is stored in controller as instance_var (Rails2)' do
64
- context 'template is set' do
65
- before(:each) do
66
- @controller.stub!(:instance_variable_get => @template)
67
- @adapter = create_adapter
37
+
38
+ describe '#initialize' do
39
+ context "when the controller's template is set" do
40
+ before { controller.stub(instance_variable_get: template) }
41
+
42
+ it "sets the adapter's request accordingly" do
43
+ expect(adapter.request).to be request
68
44
  end
69
- it {@adapter.template.should == @template}
70
- end
71
- context 'template is not set' do
72
- before(:each) do
73
- @controller.stub!(:instance_variable_get => nil)
74
- @adapter = create_adapter
45
+ end
46
+
47
+ context "when the controller's template is not set" do
48
+ before { controller.stub(instance_variable_get: nil) }
49
+
50
+ it "sets the adapter's request to nil" do
51
+ expect(adapter.request).to be_nil
75
52
  end
76
- it {@adapter.template.should be_nil}
77
53
  end
78
- end
79
- context 'template is stored in controller as view_context (Rails3)' do
80
- context 'template is set' do
81
- before(:each) do
82
- @controller.stub!(:view_context => @template)
83
- @adapter = create_adapter
54
+
55
+ it "sets the adapter's controller to the context's controller" do
56
+ expect(adapter.controller).to be controller
57
+ end
58
+
59
+ context "when the controller's template is stored as instance var (Rails2)" do
60
+ context "when the controller's template is set" do
61
+ before { controller.stub(instance_variable_get: template) }
62
+
63
+ it "sets the adapter's template accordingly" do
64
+ expect(adapter.template).to be template
65
+ end
66
+ end
67
+
68
+ context "when the controller's template is not set" do
69
+ before { controller.stub(instance_variable_get: nil) }
70
+
71
+ it "set the adapter's template to nil" do
72
+ expect(adapter.template).to be_nil
73
+ end
84
74
  end
85
- it {@adapter.template.should == @template}
86
75
  end
87
- context 'template is not set' do
88
- before(:each) do
89
- @controller.stub!(:view_context => nil)
90
- @adapter = create_adapter
76
+
77
+ context "when the controller's template is stored as view_context (Rails3)" do
78
+ context 'and the template is set' do
79
+ before { controller.stub(view_context: template) }
80
+
81
+ it "sets the adapter's template accordingly" do
82
+ expect(adapter.template).to be template
83
+ end
84
+ end
85
+
86
+ context 'and the template is not set' do
87
+ before { controller.stub(view_context: nil) }
88
+
89
+ it "sets the adapter's template to nil" do
90
+ expect(adapter.template).to be_nil
91
+ end
91
92
  end
92
- it {@adapter.template.should be_nil}
93
93
  end
94
94
  end
95
- end
96
- end
97
95
 
98
- describe 'request_uri' do
99
- context 'request is set' do
100
- context 'fullpath is defined on request' do
101
- before(:each) do
102
- @request = stub(:request, :fullpath => '/fullpath')
103
- @adapter.stub!(:request => @request)
96
+ describe '#request_uri' do
97
+ context "when the adapter's request is set" do
98
+ before { adapter.stub(request: request) }
99
+
100
+ context 'and request.fullpath is defined' do
101
+ let(:request) { double(:request, fullpath: '/fullpath') }
102
+
103
+ it "sets the adapter's request_uri to the request.fullpath" do
104
+ expect(adapter.request_uri).to eq '/fullpath'
105
+ end
106
+ end
107
+
108
+ context 'and request.fullpath is not defined' do
109
+ let(:request) { double(:request, request_uri: '/request_uri') }
110
+
111
+ before { adapter.stub(request: request) }
112
+
113
+ it "sets the adapter's request_uri to the request.request_uri" do
114
+ expect(adapter.request_uri).to eq '/request_uri'
115
+ end
116
+ end
104
117
  end
105
- it {@adapter.request_uri.should == '/fullpath'}
106
- end
107
- context 'fullpath is not defined on request' do
108
- before(:each) do
109
- @request = stub(:request, :request_uri => '/request_uri')
110
- @adapter.stub!(:request => @request)
118
+
119
+ context "when the adapter's request is not set" do
120
+ before { adapter.stub(request: nil) }
121
+
122
+ it "sets the adapter's request_uri to an empty string" do
123
+ expect(adapter.request_uri).to eq ''
124
+ end
111
125
  end
112
- it {@adapter.request_uri.should == '/request_uri'}
113
126
  end
114
- end
115
- context 'request is not set' do
116
- before(:each) do
117
- @adapter.stub!(:request => nil)
118
- end
119
- it {@adapter.request_uri.should == ''}
120
- end
121
- end
122
127
 
123
- describe 'request_path' do
124
- context 'request is set' do
125
- before(:each) do
126
- @request = stub(:request, :path => '/request_path')
127
- @adapter.stub!(:request => @request)
128
- end
129
- it {@adapter.request_path.should == '/request_path'}
130
- end
131
- context 'request is not set' do
132
- before(:each) do
133
- @adapter.stub!(:request => nil)
134
- end
135
- it {@adapter.request_path.should == ''}
136
- end
137
- end
138
-
139
- describe 'context_for_eval' do
140
- context 'controller is present' do
141
- before(:each) do
142
- @controller = stub(:controller)
143
- @adapter.instance_variable_set(:@controller, @controller)
144
- end
145
- context 'template is present' do
146
- before(:each) do
147
- @template = stub(:template)
148
- @adapter.instance_variable_set(:@template, @template)
128
+ describe '#request_path' do
129
+ context "when the adapter's request is set" do
130
+ let(:request) { double(:request, path: '/request_path') }
131
+
132
+ before { adapter.stub(request: request) }
133
+
134
+ it "sets the adapter's request_path to the request.path" do
135
+ expect(adapter.request_path).to eq '/request_path'
136
+ end
149
137
  end
150
- it {@adapter.context_for_eval.should == @template}
151
- end
152
- context 'template is not present' do
153
- before(:each) do
154
- @adapter.instance_variable_set(:@template, nil)
138
+
139
+ context "when the adapter's request is not set" do
140
+ before { adapter.stub(request: nil) }
141
+
142
+ it "sets the adapter's request_path to an empty string" do
143
+ expect(adapter.request_path).to eq ''
144
+ end
155
145
  end
156
- it {@adapter.context_for_eval.should == @controller}
157
146
  end
158
- end
159
- context 'controller is not present' do
160
- before(:each) do
161
- @adapter.instance_variable_set(:@controller, nil)
162
- end
163
- context 'template is present' do
164
- before(:each) do
165
- @template = stub(:template)
166
- @adapter.instance_variable_set(:@template, @template)
147
+
148
+ describe '#context_for_eval' do
149
+ context "when the adapter's controller is set" do
150
+ before { adapter.instance_variable_set(:@controller, controller) }
151
+
152
+ context "and the adapter's template is set" do
153
+ before { adapter.instance_variable_set(:@template, template) }
154
+
155
+ it "sets the adapter's context_for_eval to the template" do
156
+ expect(adapter.context_for_eval).to be template
157
+ end
158
+ end
159
+
160
+ context "and the adapter's template is not set" do
161
+ before { adapter.instance_variable_set(:@template, nil) }
162
+
163
+ it "sets the adapter's context_for_eval to the controller" do
164
+ expect(adapter.context_for_eval).to be controller
165
+ end
166
+ end
167
167
  end
168
- it {@adapter.context_for_eval.should == @template}
169
- end
170
- context 'template is not present' do
171
- before(:each) do
172
- @adapter.instance_variable_set(:@template, nil)
168
+
169
+ context "when the adapter's controller is not set" do
170
+ before { adapter.instance_variable_set(:@controller, nil) }
171
+
172
+ context "and the adapter's template is set" do
173
+ before { adapter.instance_variable_set(:@template, template) }
174
+
175
+ it "sets the adapter's context_for_eval to the template" do
176
+ expect(adapter.context_for_eval).to be template
177
+ end
178
+ end
179
+
180
+ context "and the adapter's template is not set" do
181
+ before { adapter.instance_variable_set(:@template, nil) }
182
+
183
+ it 'raises an exception' do
184
+ expect{ adapter.context_for_eval }.to raise_error
185
+ end
186
+ end
173
187
  end
174
- it {lambda {@adapter.context_for_eval}.should raise_error}
175
- end
176
- end
177
- end
178
-
179
- describe 'current_page?' do
180
- context 'template is set' do
181
- before(:each) do
182
- @adapter.stub!(:template => @template)
183
- end
184
- it "should delegate the call to the template" do
185
- @template.should_receive(:current_page?).with(:page)
186
- @adapter.current_page?(:page)
187
- end
188
- end
189
- context 'template is not set' do
190
- before(:each) do
191
- @adapter.stub!(:template => nil)
192
- end
193
- it {@adapter.should_not be_current_page(:page)}
194
- end
195
- end
196
-
197
- describe 'link_to' do
198
- context 'template is set' do
199
- before(:each) do
200
- @adapter.stub!(:template => @template)
201
- @adapter.stub!(:html_safe => 'safe_text')
202
- @options = stub(:options)
203
- end
204
- it "should delegate the call to the template (with html_safe text)" do
205
- @template.should_receive(:link_to).with('safe_text', 'url', @options)
206
- @adapter.link_to('text', 'url', @options)
207
- end
208
- end
209
- context 'template is not set' do
210
- before(:each) do
211
- @adapter.stub!(:template => nil)
212
188
  end
213
- it {@adapter.link_to('text', 'url', @options).should be_nil}
214
- end
215
- end
216
-
217
- describe 'content_tag' do
218
- context 'template is set' do
219
- before(:each) do
220
- @adapter.stub!(:template => @template)
221
- @adapter.stub!(:html_safe => 'safe_text')
222
- @options = stub(:options)
223
- end
224
- it "should delegate the call to the template (with html_safe text)" do
225
- @template.should_receive(:content_tag).with(:div, 'safe_text', @options)
226
- @adapter.content_tag(:div, 'text', @options)
227
- end
228
- end
229
- context 'template is not set' do
230
- before(:each) do
231
- @adapter.stub!(:template => nil)
232
- end
233
- it {@adapter.content_tag(:div, 'text', @options).should be_nil}
234
- end
235
- end
236
-
237
- describe 'self.extract_controller_from' do
238
- context 'object responds to controller' do
239
- before(:each) do
240
- @context.stub!(:controller => @controller)
241
- end
242
- it "should return the controller" do
243
- @adapter.send(:extract_controller_from, @context).should == @controller
189
+
190
+ describe '#current_page?' do
191
+ context "when the adapter's template is set" do
192
+ before { adapter.stub(template: template) }
193
+
194
+ it 'delegates the call to the template' do
195
+ expect(template).to receive(:current_page?).with(:page)
196
+ adapter.current_page?(:page)
197
+ end
198
+ end
199
+
200
+ context "when the adapter's template is not set" do
201
+ before { adapter.stub(template: nil) }
202
+
203
+ it 'returns false' do
204
+ expect(adapter).not_to be_current_page(:page)
205
+ end
206
+ end
244
207
  end
245
- end
246
- context 'object does not respond to controller' do
247
- before(:each) do
248
- @context = stub(:context)
208
+
209
+ describe '#link_to' do
210
+ let(:options) { double(:options) }
211
+
212
+ context "when the adapter's template is set" do
213
+ before { adapter.stub(template: template, html_safe: 'safe_text') }
214
+
215
+ it 'delegates the call to the template (with html_safe text)' do
216
+ expect(template).to receive(:link_to)
217
+ .with('safe_text', 'url', options)
218
+ adapter.link_to('text', 'url', options)
219
+ end
220
+ end
221
+
222
+ context "when the adapter's template is not set" do
223
+ before { adapter.stub(template: nil) }
224
+
225
+ it 'returns nil' do
226
+ expect(adapter.link_to('text', 'url', options)).to be_nil
227
+ end
228
+ end
249
229
  end
250
- it "should return the context" do
251
- @adapter.send(:extract_controller_from, @context).should == @context
230
+
231
+ describe '#content_tag' do
232
+ let(:options) { double(:options) }
233
+
234
+ context "when the adapter's template is set" do
235
+ before { adapter.stub(template: template, html_safe: 'safe_text') }
236
+
237
+ it 'delegates the call to the template (with html_safe text)' do
238
+ expect(template).to receive(:content_tag)
239
+ .with(:div, 'safe_text', options)
240
+ adapter.content_tag(:div, 'text', options)
241
+ end
242
+ end
243
+
244
+ context "when the adapter's template is not set" do
245
+ before { adapter.stub(template: nil) }
246
+
247
+ it 'returns nil' do
248
+ expect(adapter.content_tag(:div, 'text', options)).to be_nil
249
+ end
250
+ end
252
251
  end
253
- end
254
- end
255
-
256
- describe 'html_safe' do
257
- before(:each) do
258
- @input = stub :input
259
- end
260
- context 'input does respond to html_safe' do
261
- before(:each) do
262
- @safe = stub :safe
263
- @input.stub!(:html_safe => @safe)
252
+
253
+ describe '#extract_controller_from' do
254
+ context 'when context responds to controller' do
255
+ it 'returns the controller' do
256
+ expect(adapter.send(:extract_controller_from, context)).to be controller
257
+ end
258
+ end
259
+
260
+ context 'when context does not respond to controller' do
261
+ let(:context) { double(:context) }
262
+
263
+ it 'returns the context' do
264
+ expect(adapter.send(:extract_controller_from, context)).to be context
265
+ end
266
+ end
264
267
  end
265
- it {@adapter.send(:html_safe, @input).should == @safe}
266
- end
267
- context 'input does not respond to html_safe' do
268
- it {@adapter.send(:html_safe, @input).should == @input}
269
- end
270
- end
271
268
 
272
- describe 'template_from' do
273
- context 'Rails3' do
274
- before(:each) do
275
- @controller.stub!(:view_context => 'view')
269
+ describe '#html_safe' do
270
+ let(:input) { double(:input) }
271
+
272
+ context 'when input responds to html_safe' do
273
+ let(:safe) { double(:safe) }
274
+
275
+ before { input.stub(html_safe: safe) }
276
+
277
+ it 'returns the html safe version of the input' do
278
+ expect(adapter.send(:html_safe, input)).to be safe
279
+ end
280
+ end
281
+
282
+ context 'when input does not respond to html_safe' do
283
+ it 'returns the input' do
284
+ expect(adapter.send(:html_safe, input)).to be input
285
+ end
286
+ end
276
287
  end
277
- it {@adapter.send(:template_from, @controller).should == 'view'}
278
- end
279
- context 'Rails2' do
280
- before(:each) do
281
- @controller.instance_variable_set(:@template, 'view')
288
+
289
+ describe '#link_title' do
290
+ let(:name) { double(:name, html_safe: safe) }
291
+ let(:safe) { double(:safe) }
292
+
293
+ context 'when config option consider_item_names_as_safe is true' do
294
+ it 'uses the html_safe version of the name' do
295
+ expect(adapter.send(:link_title, name)).to be safe
296
+ end
297
+ end
298
+
299
+ # TODO: Does it make sense ?
300
+ context 'when config option consider_item_names_as_safe is false' do
301
+ before do
302
+ SimpleNavigation.config.consider_item_names_as_safe = false
303
+ adapter.stub(template: template)
304
+ end
305
+
306
+ it 'uses the item name' do
307
+ expect(adapter.send(:link_title, name)).to be name
308
+ end
309
+ end
282
310
  end
283
- it {@adapter.send(:template_from, @controller).should == 'view'}
284
311
  end
285
312
  end
286
-
287
313
  end