simple-navigation 2.7.1 → 2.7.2

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ *2.7.2
2
+
3
+ * added ability to have more than one config_file_path (useful if simple-navigation is used as a part of another gem/plugin). Credits to Luke Imhoff.
4
+
1
5
  *2.7.1
2
6
 
3
7
  * added SimpleNavigation.request and SimpleNavigation.request_uri as abstraction for getting request_uri (rails2/rails3)
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 2
3
3
  :minor: 7
4
- :patch: 1
4
+ :patch: 2
@@ -16,7 +16,7 @@ require 'simple_navigation/railtie' if Rails::VERSION::MAJOR == 3
16
16
  # A plugin for generating a simple navigation. See README for resources on usage instructions.
17
17
  module SimpleNavigation
18
18
 
19
- mattr_accessor :config_files, :config_file_path, :default_renderer, :controller, :template, :explicit_current_navigation, :rails_env, :rails_root, :registered_renderers
19
+ mattr_accessor :config_files, :config_file_paths, :default_renderer, :controller, :template, :explicit_current_navigation, :rails_env, :rails_root, :registered_renderers
20
20
 
21
21
  self.config_files = {}
22
22
  self.registered_renderers = {
@@ -30,7 +30,7 @@ module SimpleNavigation
30
30
 
31
31
  # Sets the config file path and installs the ControllerMethods in ActionController::Base.
32
32
  def init_rails
33
- SimpleNavigation.config_file_path = SimpleNavigation.default_config_file_path unless SimpleNavigation.config_file_path
33
+ SimpleNavigation.config_file_paths ||= [SimpleNavigation.default_config_file_path]
34
34
  ActionController::Base.send(:include, SimpleNavigation::ControllerMethods)
35
35
  end
36
36
 
@@ -40,17 +40,42 @@ module SimpleNavigation
40
40
 
41
41
  # Returns true if the config_file for specified context does exist.
42
42
  def config_file?(navigation_context = :default)
43
- File.exists?(config_file_name(navigation_context))
43
+ !!config_file(navigation_context)
44
+ end
45
+
46
+ # Returns the config file for the given navigation context or nil if no matching config file can be found.
47
+ # If multiple config_paths are set, it returns the first matching file.
48
+ #
49
+ def config_file(navigation_context = :default)
50
+ config_file_paths.collect { |path| File.join(path, config_file_name(navigation_context)) }.detect {|full_path| File.exists?(full_path)}
51
+ end
52
+
53
+ # Returns the name of the config file for the given navigation_context
54
+ def config_file_name(navigation_context = :default)
55
+ prefix = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
56
+ "#{prefix}navigation.rb"
57
+ end
58
+
59
+ # Returns a nice sentence including all config_paths
60
+ def config_file_paths_sentence
61
+ self.config_file_paths.to_sentence(
62
+ :last_word_connector => ', or ',
63
+ :two_words_connector => ' or '
64
+ )
65
+ end
66
+
67
+ # Sets the config_file_path
68
+ def config_file_path=(path)
69
+ self.config_file_paths = [path]
44
70
  end
45
71
 
46
72
  # Reads the config_file for the specified navigation_context and stores it for later evaluation.
47
73
  def load_config(navigation_context = :default)
48
- raise "config_file_path is not set!" unless self.config_file_path
49
- raise "Config file '#{config_file_name(navigation_context)}' does not exists!" unless config_file?(navigation_context)
74
+ raise "Config file for #{navigation_context} context not found in #{config_file_paths_sentence}!" unless config_file?(navigation_context)
50
75
  if SimpleNavigation.rails_env == 'production'
51
- self.config_files[navigation_context] ||= IO.read(config_file_name(navigation_context))
76
+ self.config_files[navigation_context] ||= IO.read(config_file(navigation_context))
52
77
  else
53
- self.config_files[navigation_context] = IO.read(config_file_name(navigation_context))
78
+ self.config_files[navigation_context] = IO.read(config_file(navigation_context))
54
79
  end
55
80
  end
56
81
 
@@ -76,12 +101,6 @@ module SimpleNavigation
76
101
  config.primary_navigation
77
102
  end
78
103
 
79
- # Returns the path to the config_file for the given navigation_context
80
- def config_file_name(navigation_context = :default)
81
- file_name = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
82
- File.join(config_file_path, "#{file_name}navigation.rb")
83
- end
84
-
85
104
  def explicit_navigation_args
86
105
  self.controller.instance_variable_get(:"@sn_current_navigation_args")
87
106
  end
@@ -11,47 +11,125 @@ describe SimpleNavigation do
11
11
  end
12
12
  end
13
13
 
14
- describe 'config_file?' do
15
- it "should check for the file existance with the file_name" do
16
- File.should_receive(:exists?).with('path_to_config/ctx_navigation.rb')
17
- SimpleNavigation.config_file?(:ctx)
14
+ describe 'config_file_name' do
15
+ context 'for :default navigation_context' do
16
+ it "should return the name of the default config file" do
17
+ SimpleNavigation.config_file_name.should == 'navigation.rb'
18
+ end
18
19
  end
19
- it "should check for the file existance on default context" do
20
- File.should_receive(:exists?).with('path_to_config/navigation.rb')
21
- SimpleNavigation.config_file?
20
+ context 'for other navigation_context' do
21
+ it "should return the name of the config file matching the specified context" do
22
+ SimpleNavigation.config_file_name(:my_other_context).should == 'my_other_context_navigation.rb'
23
+ end
24
+ it "should convert camelcase-contexts to underscore" do
25
+ SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'why_would_you_do_this_navigation.rb'
26
+ end
22
27
  end
23
- context 'config file exists' do
28
+ end
29
+
30
+ describe 'config_file_paths_sentence' do
31
+ context 'no paths are set' do
24
32
  before(:each) do
25
- File.stub!(:exists? => true)
33
+ SimpleNavigation.config_file_paths = []
26
34
  end
27
- it {SimpleNavigation.config_file?(:ctx).should be_true}
35
+ it {SimpleNavigation.config_file_paths_sentence.should == ''}
28
36
  end
29
- context 'config file does not exist' do
37
+ context 'one path is set' do
30
38
  before(:each) do
31
- File.stub!(:exists? => false)
39
+ SimpleNavigation.config_file_paths = ['first_path']
32
40
  end
33
- it {SimpleNavigation.config_file?(:ctx).should be_false}
41
+ it {SimpleNavigation.config_file_paths_sentence.should == 'first_path'}
42
+ end
43
+ context 'two paths are set' do
44
+ before(:each) do
45
+ SimpleNavigation.config_file_paths = ['first_path', 'second_path']
46
+ end
47
+ it {SimpleNavigation.config_file_paths_sentence.should == 'first_path or second_path'}
48
+ end
49
+ context 'three pahts are set' do
50
+ before(:each) do
51
+ SimpleNavigation.config_file_paths = ['first_path', 'second_path', 'third_path']
52
+ end
53
+ it {SimpleNavigation.config_file_paths_sentence.should == 'first_path, second_path, or third_path'}
34
54
  end
35
55
  end
36
56
 
37
- describe 'config_file_name' do
57
+ describe 'config_file_path=' do
38
58
  before(:each) do
39
- SimpleNavigation.config_file_path = 'path_to_config'
59
+ SimpleNavigation.config_file_paths = ['existing_path']
40
60
  end
41
- context 'for :default navigation_context' do
42
- it "should return the path to default config file" do
43
- SimpleNavigation.config_file_name.should == 'path_to_config/navigation.rb'
61
+ it "should override the config_file_paths" do
62
+ SimpleNavigation.config_file_path = 'new_path'
63
+ SimpleNavigation.config_file_paths.should == ['new_path']
64
+ end
65
+ end
66
+
67
+ describe 'config_file' do
68
+ context 'no config_file_paths are set' do
69
+ before(:each) do
70
+ SimpleNavigation.config_file_paths = []
71
+ end
72
+ it "should return nil" do
73
+ SimpleNavigation.config_file.should be_nil
44
74
  end
45
75
  end
46
-
47
- context 'for other navigation_context' do
48
- it "should return the path to the config file matching the specified context" do
49
- SimpleNavigation.config_file_name(:my_other_context).should == 'path_to_config/my_other_context_navigation.rb'
76
+ context 'one config_file_path is set' do
77
+ before(:each) do
78
+ SimpleNavigation.config_file_paths = ['my_config_file_path']
50
79
  end
51
- it "should convert camelcase-contexts to underscore" do
52
- SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'path_to_config/why_would_you_do_this_navigation.rb'
80
+ context 'requested config file does exist' do
81
+ before(:each) do
82
+ File.stub!(:exists? => true)
83
+ end
84
+ it "should return the path to the config_file" do
85
+ SimpleNavigation.config_file.should == 'my_config_file_path/navigation.rb'
86
+ end
87
+ end
88
+ context 'requested config file does not exist' do
89
+ before(:each) do
90
+ File.stub!(:exists? => false)
91
+ end
92
+ it "should return nil" do
93
+ SimpleNavigation.config_file.should be_nil
94
+ end
53
95
  end
54
96
  end
97
+ context 'multiple config_file_paths are set' do
98
+ before(:each) do
99
+ SimpleNavigation.config_file_paths = ['first_path', 'second_path']
100
+ end
101
+ context 'requested config file does exist' do
102
+ before(:each) do
103
+ File.stub!(:exists? => true)
104
+ end
105
+ it "should return the path to the first matching config_file" do
106
+ SimpleNavigation.config_file.should == 'first_path/navigation.rb'
107
+ end
108
+ end
109
+ context 'requested config file does not exist' do
110
+ before(:each) do
111
+ File.stub!(:exists? => false)
112
+ end
113
+ it "should return nil" do
114
+ SimpleNavigation.config_file.should be_nil
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ describe 'config_file?' do
121
+ context 'config_file present' do
122
+ before(:each) do
123
+ SimpleNavigation.stub!(:config_file => 'file')
124
+ end
125
+ it {SimpleNavigation.config_file?.should be_true}
126
+ end
127
+ context 'config_file not present' do
128
+ before(:each) do
129
+ SimpleNavigation.stub!(:config_file => nil)
130
+ end
131
+ it {SimpleNavigation.config_file?.should be_false}
132
+ end
55
133
  end
56
134
 
57
135
  describe 'set_template_from' do
@@ -119,16 +197,16 @@ describe SimpleNavigation do
119
197
  end
120
198
  it "should not override the config_file_path" do
121
199
  SimpleNavigation.init_rails
122
- SimpleNavigation.config_file_path.should == 'my_path'
200
+ SimpleNavigation.config_file_paths.should == ['my_path']
123
201
  end
124
202
  end
125
- context 'SimpleNavigation.config_file_path is not set' do
203
+ context 'SimpleNavigation.config_file_paths are not set' do
126
204
  before(:each) do
127
- SimpleNavigation.config_file_path = nil
205
+ SimpleNavigation.config_file_paths = nil
128
206
  end
129
207
  it "should set the config_file_path to the default" do
130
208
  SimpleNavigation.init_rails
131
- SimpleNavigation.config_file_path.should == 'default_path'
209
+ SimpleNavigation.config_file_paths.should == ['default_path']
132
210
  end
133
211
  end
134
212
  it "should extend the ActionController::Base" do
@@ -226,29 +304,27 @@ describe SimpleNavigation do
226
304
  describe 'load_config' do
227
305
  context 'config_file_path is set' do
228
306
  before(:each) do
229
- SimpleNavigation.config_file_path = 'path_to_config'
230
- #SimpleNavigation.stub!(:config_file_name => 'path_to_config/navigation.rb')
307
+ SimpleNavigation.stub!(:config_file => 'path_to_config_file')
231
308
  end
232
-
233
309
  context 'config_file does exist' do
234
310
  before(:each) do
235
311
  SimpleNavigation.stub!(:config_file? => true)
236
- IO.stub!(:read).and_return('file_content')
312
+ IO.stub!(:read => 'file_content')
237
313
  end
238
314
  it "should not raise an error" do
239
315
  lambda{SimpleNavigation.load_config}.should_not raise_error
240
316
  end
241
317
  it "should read the specified config file from disc" do
242
- IO.should_receive(:read).with('path_to_config/navigation.rb')
318
+ IO.should_receive(:read).with('path_to_config_file')
243
319
  SimpleNavigation.load_config
244
320
  end
245
321
  it "should store the read content in the module (default context)" do
246
- SimpleNavigation.should_receive(:config_file_name).with(:default)
322
+ SimpleNavigation.should_receive(:config_file).with(:default)
247
323
  SimpleNavigation.load_config
248
324
  SimpleNavigation.config_files[:default].should == 'file_content'
249
325
  end
250
326
  it "should store the content in the module (non default context)" do
251
- SimpleNavigation.should_receive(:config_file_name).with(:my_context)
327
+ SimpleNavigation.should_receive(:config_file).with(:my_context)
252
328
  SimpleNavigation.load_config(:my_context)
253
329
  SimpleNavigation.config_files[:my_context].should == 'file_content'
254
330
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-navigation
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 23
4
5
  prerelease: false
5
6
  segments:
6
7
  - 2
7
8
  - 7
8
- - 1
9
- version: 2.7.1
9
+ - 2
10
+ version: 2.7.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Andi Schacke
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-08-12 00:00:00 +02:00
18
+ date: 2010-08-20 00:00:00 +02:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rspec
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 15
27
30
  segments:
28
31
  - 1
29
32
  - 2
@@ -88,23 +91,27 @@ rdoc_options:
88
91
  require_paths:
89
92
  - lib
90
93
  required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
91
95
  requirements:
92
96
  - - ">="
93
97
  - !ruby/object:Gem::Version
98
+ hash: 3
94
99
  segments:
95
100
  - 0
96
101
  version: "0"
97
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
98
104
  requirements:
99
105
  - - ">="
100
106
  - !ruby/object:Gem::Version
107
+ hash: 3
101
108
  segments:
102
109
  - 0
103
110
  version: "0"
104
111
  requirements: []
105
112
 
106
113
  rubyforge_project: andi
107
- rubygems_version: 1.3.6
114
+ rubygems_version: 1.3.7
108
115
  signing_key:
109
116
  specification_version: 3
110
117
  summary: Simple Navigation is a ruby library for creating navigations (with multiple levels) for your Ruby on Rails application.