simple-navigation 3.11.0 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
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,307 +1,289 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SimpleNavigation do
4
-
5
- before(:each) do
6
- SimpleNavigation.config_file_path = 'path_to_config'
7
- end
4
+ before { subject.config_file_path = 'path_to_config' }
8
5
 
9
6
  describe 'config_file_name' do
10
- context 'for :default navigation_context' do
11
- it "should return the name of the default config file" do
12
- SimpleNavigation.config_file_name.should == 'navigation.rb'
7
+ context 'when the navigation_context is :default' do
8
+ it 'returns the name of the default config file' do
9
+ expect(subject.config_file_name).to eq 'navigation.rb'
13
10
  end
14
11
  end
15
- context 'for other navigation_context' do
16
- it "should return the name of the config file matching the specified context" do
17
- SimpleNavigation.config_file_name(:my_other_context).should == 'my_other_context_navigation.rb'
12
+
13
+ context 'when the navigation_context is NOT :default' do
14
+ it 'returns the name of the config file matching the specified context' do
15
+ file_name = subject.config_file_name(:my_other_context)
16
+ expect(file_name).to eq 'my_other_context_navigation.rb'
18
17
  end
19
- it "should convert camelcase-contexts to underscore" do
20
- SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'why_would_you_do_this_navigation.rb'
18
+
19
+ it 'converts camelcase-contexts to underscore' do
20
+ file_name = subject.config_file_name(:WhyWouldYouDoThis)
21
+ expect(file_name).to eq 'why_would_you_do_this_navigation.rb'
21
22
  end
22
23
  end
23
24
  end
24
-
25
+
25
26
  describe 'config_file_path=' do
26
- before(:each) do
27
- SimpleNavigation.config_file_paths = ['existing_path']
28
- end
29
- it "should override the config_file_paths" do
30
- SimpleNavigation.config_file_path = 'new_path'
31
- SimpleNavigation.config_file_paths.should == ['new_path']
27
+ before { subject.config_file_paths = ['existing_path'] }
28
+
29
+ it 'overrides the config_file_paths' do
30
+ subject.config_file_path = 'new_path'
31
+ expect(subject.config_file_paths).to eq ['new_path']
32
32
  end
33
33
  end
34
-
34
+
35
35
  describe 'config_file' do
36
- context 'no config_file_paths are set' do
37
- before(:each) do
38
- SimpleNavigation.config_file_paths = []
39
- end
40
- it "should return nil" do
41
- SimpleNavigation.config_file.should be_nil
36
+ context 'when no config_file_paths are set' do
37
+ before { subject.config_file_paths = [] }
38
+
39
+ it 'returns nil' do
40
+ expect(subject.config_file).to be_nil
42
41
  end
43
42
  end
44
- context 'one config_file_path is set' do
45
- before(:each) do
46
- SimpleNavigation.config_file_paths = ['my_config_file_path']
47
- end
48
- context 'requested config file does exist' do
49
- before(:each) do
50
- File.stub!(:exists? => true)
51
- end
52
- it "should return the path to the config_file" do
53
- SimpleNavigation.config_file.should == 'my_config_file_path/navigation.rb'
43
+
44
+ context 'when one config_file_path is set' do
45
+ before { subject.config_file_paths = ['my_config_file_path'] }
46
+
47
+ context 'and the requested config file exists' do
48
+ before { File.stub(exists?: true) }
49
+
50
+ it 'returns the path to the config_file' do
51
+ expect(subject.config_file).to eq 'my_config_file_path/navigation.rb'
54
52
  end
55
53
  end
56
- context 'requested config file does not exist' do
57
- before(:each) do
58
- File.stub!(:exists? => false)
59
- end
60
- it "should return nil" do
61
- SimpleNavigation.config_file.should be_nil
54
+
55
+ context 'and the requested config file does not exist' do
56
+ before { File.stub(exists?: false) }
57
+
58
+ it 'returns nil' do
59
+ expect(subject.config_file).to be_nil
62
60
  end
63
61
  end
64
62
  end
65
- context 'multiple config_file_paths are set' do
66
- before(:each) do
67
- SimpleNavigation.config_file_paths = ['first_path', 'second_path']
68
- end
69
- context 'requested config file does exist' do
70
- before(:each) do
71
- File.stub!(:exists? => true)
72
- end
73
- it "should return the path to the first matching config_file" do
74
- SimpleNavigation.config_file.should == 'first_path/navigation.rb'
63
+
64
+ context 'when multiple config_file_paths are set' do
65
+ before { subject.config_file_paths = ['first_path', 'second_path'] }
66
+
67
+ context 'and the requested config file exists' do
68
+ before { File.stub(exists?: true) }
69
+
70
+ it 'returns the path to the first matching config_file' do
71
+ expect(subject.config_file).to eq 'first_path/navigation.rb'
75
72
  end
76
73
  end
77
- context 'requested config file does not exist' do
78
- before(:each) do
79
- File.stub!(:exists? => false)
80
- end
81
- it "should return nil" do
82
- SimpleNavigation.config_file.should be_nil
74
+
75
+ context 'and the requested config file does not exist' do
76
+ before { File.stub(exists?: false) }
77
+
78
+ it 'returns nil' do
79
+ expect(subject.config_file).to be_nil
83
80
  end
84
81
  end
85
82
  end
86
83
  end
87
-
88
- describe 'config_file?' do
89
- context 'config_file present' do
90
- before(:each) do
91
- SimpleNavigation.stub!(:config_file => 'file')
84
+
85
+ describe '.config_file?' do
86
+ context 'when config_file is present' do
87
+ before { subject.stub(config_file: 'file') }
88
+
89
+ it 'returns true' do
90
+ expect(subject.config_file?).to be_true
92
91
  end
93
- it {SimpleNavigation.config_file?.should be_true}
94
92
  end
95
- context 'config_file not present' do
96
- before(:each) do
97
- SimpleNavigation.stub!(:config_file => nil)
93
+
94
+ context 'when config_file is not present' do
95
+ before { subject.stub(config_file: nil) }
96
+
97
+ it 'returns false' do
98
+ expect(subject.config_file?).to be_false
98
99
  end
99
- it {SimpleNavigation.config_file?.should be_false}
100
100
  end
101
101
  end
102
102
 
103
- describe 'self.default_config_file_path' do
104
- before(:each) do
105
- SimpleNavigation.stub!(:root => 'root')
103
+ describe '.default_config_file_path' do
104
+ before { subject.stub(root: 'root') }
105
+
106
+ it 'returns the config file path according to :root setting' do
107
+ expect(subject.default_config_file_path).to eq 'root/config'
106
108
  end
107
- it {SimpleNavigation.default_config_file_path.should == 'root/config'}
108
109
  end
109
-
110
- describe 'regarding renderers' do
111
- it "should have registered the builtin renderers by default" do
112
- SimpleNavigation.registered_renderers.should_not be_empty
110
+
111
+ describe 'Regarding renderers' do
112
+ it 'registers the builtin renderers by default' do
113
+ expect(subject.registered_renderers).not_to be_empty
113
114
  end
114
115
 
115
- describe 'register_renderer' do
116
- before(:each) do
117
- @renderer = stub(:renderer)
118
- end
119
- it "should add the specified renderer to the list of renderers" do
120
- SimpleNavigation.register_renderer(:my_renderer => @renderer)
121
- SimpleNavigation.registered_renderers[:my_renderer].should == @renderer
116
+ describe '.register_renderer' do
117
+ let(:renderer) { double(:renderer) }
118
+
119
+ it 'adds the specified renderer to the list of renderers' do
120
+ subject.register_renderer(my_renderer: renderer)
121
+ expect(subject.registered_renderers[:my_renderer]).to be renderer
122
122
  end
123
123
  end
124
-
125
124
  end
126
125
 
127
- describe 'set_env' do
128
- before(:each) do
129
- SimpleNavigation.config_file_paths = []
130
- SimpleNavigation.stub!(:default_config_file_path => 'default_path')
131
- SimpleNavigation.set_env('root', 'my_env')
126
+ describe '.set_env' do
127
+ before do
128
+ subject.config_file_paths = []
129
+ subject.stub(default_config_file_path: 'default_path')
130
+ subject.set_env('root', 'my_env')
132
131
  end
133
- it "should set the root" do
134
- SimpleNavigation.root.should == 'root'
132
+
133
+ it 'sets the root' do
134
+ expect(subject.root).to eq 'root'
135
135
  end
136
- it "should set the environment" do
137
- SimpleNavigation.environment.should == 'my_env'
136
+
137
+ it 'sets the environment' do
138
+ expect(subject.environment).to eq 'my_env'
138
139
  end
139
- it "should add the default-config path to the list of config_file_paths" do
140
- SimpleNavigation.config_file_paths.should == ['default_path']
140
+
141
+ it 'adds the default-config path to the list of config_file_paths' do
142
+ expect(subject.config_file_paths).to eq ['default_path']
141
143
  end
142
144
  end
143
145
 
144
- describe 'load_config' do
145
- context 'config_file_path is set' do
146
- before(:each) do
147
- SimpleNavigation.stub!(:config_file => 'path_to_config_file')
148
- end
149
- context 'config_file does exist' do
150
- before(:each) do
151
- SimpleNavigation.stub!(:config_file? => true)
152
- IO.stub!(:read => 'file_content')
146
+ describe '.load_config' do
147
+ context 'when config_file_path is set' do
148
+ before { subject.stub(config_file: 'path_to_config_file') }
149
+
150
+ context 'and config_file exists' do
151
+ before do
152
+ subject.stub(config_file?: true)
153
+ IO.stub(read: 'file_content')
153
154
  end
154
- it "should not raise an error" do
155
- lambda{SimpleNavigation.load_config}.should_not raise_error
155
+
156
+ it "doesn't raise any error" do
157
+ expect{ subject.load_config }.not_to raise_error
156
158
  end
157
- it "should read the specified config file from disc" do
158
- IO.should_receive(:read).with('path_to_config_file')
159
- SimpleNavigation.load_config
159
+
160
+ it 'reads the specified config file from disc' do
161
+ expect(IO).to receive(:read).with('path_to_config_file')
162
+ subject.load_config
160
163
  end
161
- it "should store the read content in the module (default context)" do
162
- SimpleNavigation.should_receive(:config_file).with(:default)
163
- SimpleNavigation.load_config
164
- SimpleNavigation.config_files[:default].should == 'file_content'
164
+
165
+ it 'stores the read content in the module (default context)' do
166
+ expect(subject).to receive(:config_file).with(:default)
167
+ subject.load_config
168
+ expect(subject.config_files[:default]).to eq 'file_content'
165
169
  end
166
- it "should store the content in the module (non default context)" do
167
- SimpleNavigation.should_receive(:config_file).with(:my_context)
168
- SimpleNavigation.load_config(:my_context)
169
- SimpleNavigation.config_files[:my_context].should == 'file_content'
170
+
171
+ it 'stores the content in the module (non default context)' do
172
+ expect(subject).to receive(:config_file).with(:my_context)
173
+ subject.load_config(:my_context)
174
+ expect(subject.config_files[:my_context]).to eq 'file_content'
170
175
  end
171
176
  end
172
-
173
- context 'config_file does not exist' do
174
- before(:each) do
175
- SimpleNavigation.stub!(:config_file? => false)
177
+
178
+ context 'and config_file does not exist' do
179
+ before { subject.stub(config_file?: false) }
180
+
181
+ it 'raises an exception' do
182
+ expect{ subject.load_config }.to raise_error
176
183
  end
177
- it {lambda{SimpleNavigation.load_config}.should raise_error}
178
184
  end
179
185
  end
180
-
181
- context 'config_file_path is not set' do
182
- before(:each) do
183
- SimpleNavigation.config_file_path = nil
186
+
187
+ context 'when config_file_path is not set' do
188
+ before { subject.config_file_path = nil }
189
+
190
+ it 'raises an exception' do
191
+ expect{ subject.load_config }.to raise_error
184
192
  end
185
- it {lambda{SimpleNavigation.load_config}.should raise_error}
186
193
  end
187
-
188
- describe 'regarding caching of the config-files' do
189
- before(:each) do
190
- IO.stub!(:read).and_return('file_content')
191
- SimpleNavigation.config_file_path = 'path_to_config'
192
- File.stub!(:exists? => true)
193
- end
194
- context "environment undefined" do
195
- before(:each) do
196
- SimpleNavigation.stub!(:environment => nil)
197
- end
198
- it "should load the config file twice" do
199
- IO.should_receive(:read).twice
200
- SimpleNavigation.load_config
201
- SimpleNavigation.load_config
202
- end
194
+
195
+ describe 'Regarding caching of the config-files' do
196
+ before do
197
+ subject.config_file_path = 'path_to_config'
198
+ IO.stub(:read).and_return('file_content')
199
+ File.stub(exists?: true)
203
200
  end
204
- context "environment defined" do
205
- before(:each) do
206
- SimpleNavigation.stub!(:environment => 'production')
207
- end
208
- context "environment=production" do
209
- it "should load the config file only once" do
210
- IO.should_receive(:read).once
211
- SimpleNavigation.load_config
212
- SimpleNavigation.load_config
213
- end
214
- end
215
-
216
- context "environment=development" do
217
- before(:each) do
218
- SimpleNavigation.stub!(:environment => 'development')
219
- end
220
- it "should load the config file twice" do
221
- IO.should_receive(:read).twice
222
- SimpleNavigation.load_config
223
- SimpleNavigation.load_config
224
- end
225
- end
226
-
227
- context "environment=test" do
228
- before(:each) do
229
- SimpleNavigation.stub!(:environment => 'test')
230
- end
231
- it "should load the config file twice" do
232
- IO.should_receive(:read).twice
233
- SimpleNavigation.load_config
234
- SimpleNavigation.load_config
201
+
202
+ after { subject.config_files = {} }
203
+
204
+ shared_examples 'loading config file' do |env, count|
205
+ context "when environment is '#{env}'" do
206
+ before { subject.stub(environment: env) }
207
+
208
+ it "loads the config file #{count}" do
209
+ expect(IO).to receive(:read).exactly(count)
210
+ 2.times { subject.load_config }
235
211
  end
236
212
  end
237
213
  end
238
- after(:each) do
239
- SimpleNavigation.config_files = {}
240
- end
214
+
215
+ it_behaves_like 'loading config file', nil, :twice
216
+ it_behaves_like 'loading config file', 'production', :once
217
+ it_behaves_like 'loading config file', 'development', :twice
218
+ it_behaves_like 'loading config file', 'test', :twice
241
219
  end
242
220
  end
243
-
244
- describe 'config' do
245
- it {SimpleNavigation.config.should == SimpleNavigation::Configuration.instance}
246
- end
247
-
248
- describe 'active_item_container_for' do
249
- before(:each) do
250
- @primary = stub(:primary)
251
- SimpleNavigation.config.stub!(:primary_navigation => @primary)
221
+
222
+ describe '.config' do
223
+ it 'returns the Configuration singleton instance' do
224
+ expect(subject.config).to be SimpleNavigation::Configuration.instance
252
225
  end
253
- context 'level is :all' do
254
- it "should return the primary_navigation" do
255
- SimpleNavigation.active_item_container_for(:all).should == @primary
226
+ end
227
+
228
+ describe '.active_item_container_for' do
229
+ let(:primary) { double(:primary) }
230
+
231
+ before { subject.config.stub(primary_navigation: primary) }
232
+
233
+ context 'when level is :all' do
234
+ it 'returns the primary_navigation' do
235
+ nav = subject.active_item_container_for(:all)
236
+ expect(nav).to be primary
256
237
  end
257
238
  end
258
- context 'level is :leaves' do
259
- it "should return the currently active leaf-container" do
260
- @primary.should_receive(:active_leaf_container)
261
- SimpleNavigation.active_item_container_for(:leaves)
239
+
240
+ context 'when level is :leaves' do
241
+ it 'returns the currently active leaf-container' do
242
+ expect(primary).to receive(:active_leaf_container)
243
+ subject.active_item_container_for(:leaves)
262
244
  end
263
245
  end
264
- context 'level is a Range' do
265
- it "should take the min of the range to lookup the active container" do
266
- @primary.should_receive(:active_item_container_for).with(2)
267
- SimpleNavigation.active_item_container_for(2..3)
246
+
247
+ context 'when level is a Range' do
248
+ it 'takes the min of the range to lookup the active container' do
249
+ expect(primary).to receive(:active_item_container_for).with(2)
250
+ subject.active_item_container_for(2..3)
268
251
  end
269
252
  end
270
- context 'level is an Integer' do
271
- it "should consider the Integer to lookup the active container" do
272
- @primary.should_receive(:active_item_container_for).with(1)
273
- SimpleNavigation.active_item_container_for(1)
253
+
254
+ context 'when level is an Integer' do
255
+ it 'considers the Integer to lookup the active container' do
256
+ expect(primary).to receive(:active_item_container_for).with(1)
257
+ subject.active_item_container_for(1)
274
258
  end
275
259
  end
276
- context 'level is something else' do
277
- it "should raise an ArgumentError" do
278
- lambda {SimpleNavigation.active_item_container_for('something else')}.should raise_error(ArgumentError)
260
+
261
+ context 'when level is something else' do
262
+ it 'raises an exception' do
263
+ expect{
264
+ subject.active_item_container_for('something else')
265
+ }.to raise_error
279
266
  end
280
267
  end
281
268
  end
282
269
 
283
- describe 'load_adapter' do
284
- context 'Rails' do
285
- before(:each) do
286
- SimpleNavigation.stub!(:framework => :rails)
287
- SimpleNavigation.load_adapter
288
- end
289
- it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Rails}
290
- end
291
- context 'Padrino' do
292
- before(:each) do
293
- SimpleNavigation.stub!(:framework => :padrino)
294
- SimpleNavigation.load_adapter
295
- end
296
- it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Padrino}
297
- end
298
- context 'Sinatra' do
299
- before(:each) do
300
- SimpleNavigation.stub!(:framework => :sinatra)
301
- SimpleNavigation.load_adapter
270
+ describe '.load_adapter' do
271
+ shared_examples 'loading the right adapter' do |framework, adapter|
272
+ context "when the context is #{framework}" do
273
+ before do
274
+ subject.stub(framework: framework)
275
+ subject.load_adapter
276
+ end
277
+
278
+ it "returns the #{framework} adapter" do
279
+ adapter_class = SimpleNavigation::Adapters.const_get(adapter)
280
+ expect(subject.adapter_class).to be adapter_class
281
+ end
302
282
  end
303
- it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Sinatra}
304
283
  end
284
+
285
+ it_behaves_like 'loading the right adapter', :rails, :Rails
286
+ it_behaves_like 'loading the right adapter', :padrino, :Padrino
287
+ it_behaves_like 'loading the right adapter', :sinatra, :Sinatra
305
288
  end
306
-
307
289
  end