simple-navigation 2.2.2 → 2.2.3
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 +5 -0
- data/VERSION.yml +1 -1
- data/lib/simple_navigation.rb +27 -1
- data/lib/simple_navigation/configuration.rb +2 -19
- data/lib/simple_navigation/helpers.rb +5 -2
- data/spec/lib/simple_navigation/configuration_spec.rb +4 -77
- data/spec/lib/simple_navigation/helpers_spec.rb +36 -15
- data/spec/lib/simple_navigation_spec.rb +111 -4
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
*2.2.3
|
2
|
+
|
3
|
+
* changed error handling in config-file. Do not ignore errors in config-file anymore.
|
4
|
+
* only load config-file if it is present. Needed when directly providing items in render_navigation.
|
5
|
+
|
1
6
|
*2.2.2
|
2
7
|
|
3
8
|
* added lib/simple-navigation.rb to allow 'require simple-navigation' or 'config.gem "simple-navigation"'
|
data/VERSION.yml
CHANGED
data/lib/simple_navigation.rb
CHANGED
@@ -18,10 +18,14 @@ module SimpleNavigation
|
|
18
18
|
|
19
19
|
class << self
|
20
20
|
|
21
|
+
def config_file?(navigation_context=nil)
|
22
|
+
File.exists?(config_file_name(navigation_context))
|
23
|
+
end
|
24
|
+
|
21
25
|
# Reads the config_file for the specified navigation_context and stores it for later evaluation.
|
22
26
|
def load_config(navigation_context = :default)
|
23
27
|
raise "config_file_path is not set!" unless self.config_file_path
|
24
|
-
raise "Config file '#{config_file_name(navigation_context)}' does not exists!" unless
|
28
|
+
raise "Config file '#{config_file_name(navigation_context)}' does not exists!" unless config_file?(navigation_context)
|
25
29
|
if ::RAILS_ENV == 'production'
|
26
30
|
self.config_files[navigation_context] ||= IO.read(config_file_name(navigation_context))
|
27
31
|
else
|
@@ -29,6 +33,18 @@ module SimpleNavigation
|
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
36
|
+
def set_template_from(context)
|
37
|
+
SimpleNavigation.controller = extract_controller_from context
|
38
|
+
SimpleNavigation.template = SimpleNavigation.controller.instance_variable_get(:@template)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the context in which the config file should be evaluated.
|
42
|
+
# This is preferably the template, otherwise te controller
|
43
|
+
def context_for_eval
|
44
|
+
raise 'no context set for evaluation the config file' unless SimpleNavigation.template || SimpleNavigation.controller
|
45
|
+
SimpleNavigation.template || SimpleNavigation.controller
|
46
|
+
end
|
47
|
+
|
32
48
|
# Returns the singleton instance of the SimpleNavigation::Configuration
|
33
49
|
def config
|
34
50
|
SimpleNavigation::Configuration.instance
|
@@ -88,8 +104,18 @@ module SimpleNavigation
|
|
88
104
|
end
|
89
105
|
end
|
90
106
|
|
107
|
+
# Extracts a controller from the context.
|
108
|
+
def extract_controller_from(context)
|
109
|
+
if context.respond_to? :controller
|
110
|
+
context.controller
|
111
|
+
else
|
112
|
+
context
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
91
116
|
private
|
92
117
|
|
118
|
+
|
93
119
|
# TODO: refactor this ugly thing to make it nice and short
|
94
120
|
def parse_explicit_navigation_args
|
95
121
|
args = SimpleNavigation.explicit_navigation_args
|
@@ -14,31 +14,14 @@ module SimpleNavigation
|
|
14
14
|
|
15
15
|
# Evals the config_file for the given navigation_context inside the specified context (usually a controller or view)
|
16
16
|
def eval_config(context, navigation_context = :default)
|
17
|
-
SimpleNavigation.
|
18
|
-
SimpleNavigation.
|
19
|
-
context_for_eval.instance_eval(SimpleNavigation.config_files[navigation_context])
|
17
|
+
SimpleNavigation.set_template_from context
|
18
|
+
SimpleNavigation.context_for_eval.instance_eval(SimpleNavigation.config_files[navigation_context])
|
20
19
|
end
|
21
20
|
|
22
21
|
# Starts processing the configuration
|
23
22
|
def run(&block)
|
24
23
|
block.call Configuration.instance
|
25
24
|
end
|
26
|
-
|
27
|
-
# Returns the context in which the config file should be evaluated.
|
28
|
-
# This is preferably the template, otherwise te controller
|
29
|
-
def context_for_eval
|
30
|
-
raise 'no context set for evaluation the config file' unless SimpleNavigation.template || SimpleNavigation.controller
|
31
|
-
SimpleNavigation.template || SimpleNavigation.controller
|
32
|
-
end
|
33
|
-
|
34
|
-
# Extracts a controller from the context.
|
35
|
-
def extract_controller_from(context)
|
36
|
-
if context.respond_to? :controller
|
37
|
-
context.controller
|
38
|
-
else
|
39
|
-
context
|
40
|
-
end
|
41
|
-
end
|
42
25
|
|
43
26
|
end #class << self
|
44
27
|
|
@@ -35,8 +35,11 @@ module SimpleNavigation
|
|
35
35
|
options = extract_backwards_compatible_options(*args)
|
36
36
|
options = {:context => :default, :level => :all}.merge(options)
|
37
37
|
ctx = options.delete(:context)
|
38
|
-
SimpleNavigation.
|
39
|
-
SimpleNavigation
|
38
|
+
SimpleNavigation.set_template_from self
|
39
|
+
if SimpleNavigation.config_file?(ctx)
|
40
|
+
SimpleNavigation.load_config(ctx)
|
41
|
+
SimpleNavigation::Configuration.eval_config(self, ctx)
|
42
|
+
end
|
40
43
|
SimpleNavigation.config.items(options[:items]) if options[:items]
|
41
44
|
SimpleNavigation.handle_explicit_navigation
|
42
45
|
raise "no primary navigation defined, either use a navigation config file or pass items directly to render_navigation" unless SimpleNavigation.primary_navigation
|
@@ -18,7 +18,7 @@ describe SimpleNavigation::Configuration do
|
|
18
18
|
before(:each) do
|
19
19
|
@context = mock(:context)
|
20
20
|
@context.stub!(:instance_eval)
|
21
|
-
SimpleNavigation
|
21
|
+
SimpleNavigation.stub!(:context_for_eval => @context)
|
22
22
|
@config_files = {:default => 'default', :my_context => 'my_context'}
|
23
23
|
SimpleNavigation.stub!(:config_files).and_return(@config_files)
|
24
24
|
end
|
@@ -34,82 +34,9 @@ describe SimpleNavigation::Configuration do
|
|
34
34
|
SimpleNavigation::Configuration.eval_config(@context, :my_context)
|
35
35
|
end
|
36
36
|
end
|
37
|
-
it "should set the
|
38
|
-
|
39
|
-
SimpleNavigation::Configuration.
|
40
|
-
SimpleNavigation.should_receive(:controller=).with(@controller)
|
41
|
-
SimpleNavigation::Configuration.eval_config(@context)
|
42
|
-
end
|
43
|
-
it "should set the template" do
|
44
|
-
@template = stub(:template)
|
45
|
-
@controller = stub(:controller, :instance_variable_get => @template)
|
46
|
-
SimpleNavigation.stub!(:controller => @controller)
|
47
|
-
SimpleNavigation.should_receive(:template=).with(@template)
|
48
|
-
SimpleNavigation::Configuration.eval_config(@context)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe 'context_for_eval' do
|
53
|
-
context 'controller is present' do
|
54
|
-
before(:each) do
|
55
|
-
@controller = stub(:controller)
|
56
|
-
SimpleNavigation.stub!(:controller => @controller)
|
57
|
-
end
|
58
|
-
context 'template is present' do
|
59
|
-
before(:each) do
|
60
|
-
@template = stub(:template)
|
61
|
-
SimpleNavigation.stub!(:template => @template)
|
62
|
-
end
|
63
|
-
it {SimpleNavigation::Configuration.context_for_eval.should == @template}
|
64
|
-
end
|
65
|
-
context 'template is not present' do
|
66
|
-
before(:each) do
|
67
|
-
SimpleNavigation.stub!(:template => nil)
|
68
|
-
end
|
69
|
-
it {SimpleNavigation::Configuration.context_for_eval.should == @controller}
|
70
|
-
end
|
71
|
-
end
|
72
|
-
context 'controller is not present' do
|
73
|
-
before(:each) do
|
74
|
-
SimpleNavigation.stub!(:controller => nil)
|
75
|
-
end
|
76
|
-
context 'template is present' do
|
77
|
-
before(:each) do
|
78
|
-
@template = stub(:template)
|
79
|
-
SimpleNavigation.stub!(:template => @template)
|
80
|
-
end
|
81
|
-
it {SimpleNavigation::Configuration.context_for_eval.should == @template}
|
82
|
-
end
|
83
|
-
context 'template is not present' do
|
84
|
-
before(:each) do
|
85
|
-
SimpleNavigation.stub!(:template => nil)
|
86
|
-
end
|
87
|
-
it {lambda {SimpleNavigation::Configuration.context_for_eval}.should raise_error}
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe 'self.extract_controller_from' do
|
93
|
-
before(:each) do
|
94
|
-
@nav_context = stub(:nav_context)
|
95
|
-
end
|
96
|
-
|
97
|
-
context 'object responds to controller' do
|
98
|
-
before(:each) do
|
99
|
-
@controller = stub(:controller)
|
100
|
-
@nav_context.stub!(:controller).and_return(@controller)
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should return the controller" do
|
104
|
-
SimpleNavigation::Configuration.extract_controller_from(@nav_context).should == @controller
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
context 'object does not respond to controller' do
|
110
|
-
it "should return the nav_context" do
|
111
|
-
SimpleNavigation::Configuration.extract_controller_from(@nav_context).should == @nav_context
|
112
|
-
end
|
37
|
+
it "should set the template from the specified context" do
|
38
|
+
SimpleNavigation.should_receive(:set_template_from).with(@context)
|
39
|
+
SimpleNavigation::Configuration.eval_config(@context, :my_context)
|
113
40
|
end
|
114
41
|
end
|
115
42
|
|
@@ -11,28 +11,49 @@ describe SimpleNavigation::Helpers do
|
|
11
11
|
SimpleNavigation::Configuration.stub!(:eval_config)
|
12
12
|
@primary_navigation = stub(:primary_navigation, :null_object => true)
|
13
13
|
SimpleNavigation.stub!(:primary_navigation).and_return(@primary_navigation)
|
14
|
+
SimpleNavigation.stub!(:config_file? => true)
|
14
15
|
end
|
15
16
|
|
16
17
|
describe 'render_navigation' do
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
22
29
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
29
36
|
end
|
30
37
|
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
|
42
|
+
end
|
31
43
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
36
57
|
end
|
37
58
|
|
38
59
|
describe 'regarding setting of items' do
|
@@ -11,6 +11,28 @@ describe SimpleNavigation do
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
describe 'config_file?' do
|
15
|
+
before(:each) do
|
16
|
+
SimpleNavigation.stub!(:config_file_name => 'file_name')
|
17
|
+
end
|
18
|
+
it "should check for the file existance with the file_name" do
|
19
|
+
File.should_receive(:exists?).with('file_name')
|
20
|
+
SimpleNavigation.config_file?(:ctx)
|
21
|
+
end
|
22
|
+
context 'config file exists' do
|
23
|
+
before(:each) do
|
24
|
+
File.stub!(:exists? => true)
|
25
|
+
end
|
26
|
+
it {SimpleNavigation.config_file?(:ctx).should be_true}
|
27
|
+
end
|
28
|
+
context 'config file does not exist' do
|
29
|
+
before(:each) do
|
30
|
+
File.stub!(:exists? => false)
|
31
|
+
end
|
32
|
+
it {SimpleNavigation.config_file?(:ctx).should be_false}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
14
36
|
describe 'config_file_name' do
|
15
37
|
before(:each) do
|
16
38
|
SimpleNavigation.config_file_path = 'path_to_config'
|
@@ -31,6 +53,91 @@ describe SimpleNavigation do
|
|
31
53
|
end
|
32
54
|
end
|
33
55
|
|
56
|
+
describe 'set_template_from' do
|
57
|
+
before(:each) do
|
58
|
+
@context = stub :context
|
59
|
+
SimpleNavigation.stub!(:extract_controller_from => @controller)
|
60
|
+
end
|
61
|
+
it "should set the controller" do
|
62
|
+
@controller = stub(:controller)
|
63
|
+
SimpleNavigation.should_receive(:extract_controller_from).with(@context).and_return(@controller)
|
64
|
+
SimpleNavigation.should_receive(:controller=).with(@controller)
|
65
|
+
SimpleNavigation.set_template_from @context
|
66
|
+
end
|
67
|
+
it "should set the template" do
|
68
|
+
@template = stub(:template)
|
69
|
+
@controller = stub(:controller, :instance_variable_get => @template)
|
70
|
+
SimpleNavigation.stub!(:controller => @controller)
|
71
|
+
SimpleNavigation.should_receive(:template=).with(@template)
|
72
|
+
SimpleNavigation.set_template_from @context
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'self.extract_controller_from' do
|
77
|
+
before(:each) do
|
78
|
+
@nav_context = stub(:nav_context)
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'object responds to controller' do
|
82
|
+
before(:each) do
|
83
|
+
@controller = stub(:controller)
|
84
|
+
@nav_context.stub!(:controller).and_return(@controller)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should return the controller" do
|
88
|
+
SimpleNavigation.send(:extract_controller_from, @nav_context).should == @controller
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'object does not respond to controller' do
|
94
|
+
it "should return the nav_context" do
|
95
|
+
SimpleNavigation.send(:extract_controller_from, @nav_context).should == @nav_context
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe 'context_for_eval' do
|
101
|
+
context 'controller is present' do
|
102
|
+
before(:each) do
|
103
|
+
@controller = stub(:controller)
|
104
|
+
SimpleNavigation.stub!(:controller => @controller)
|
105
|
+
end
|
106
|
+
context 'template is present' do
|
107
|
+
before(:each) do
|
108
|
+
@template = stub(:template)
|
109
|
+
SimpleNavigation.stub!(:template => @template)
|
110
|
+
end
|
111
|
+
it {SimpleNavigation.context_for_eval.should == @template}
|
112
|
+
end
|
113
|
+
context 'template is not present' do
|
114
|
+
before(:each) do
|
115
|
+
SimpleNavigation.stub!(:template => nil)
|
116
|
+
end
|
117
|
+
it {SimpleNavigation.context_for_eval.should == @controller}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
context 'controller is not present' do
|
121
|
+
before(:each) do
|
122
|
+
SimpleNavigation.stub!(:controller => nil)
|
123
|
+
end
|
124
|
+
context 'template is present' do
|
125
|
+
before(:each) do
|
126
|
+
@template = stub(:template)
|
127
|
+
SimpleNavigation.stub!(:template => @template)
|
128
|
+
end
|
129
|
+
it {SimpleNavigation.context_for_eval.should == @template}
|
130
|
+
end
|
131
|
+
context 'template is not present' do
|
132
|
+
before(:each) do
|
133
|
+
SimpleNavigation.stub!(:template => nil)
|
134
|
+
end
|
135
|
+
it {lambda {SimpleNavigation.context_for_eval}.should raise_error}
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
|
34
141
|
describe 'load_config' do
|
35
142
|
context 'config_file_path is set' do
|
36
143
|
before(:each) do
|
@@ -40,7 +147,7 @@ describe SimpleNavigation do
|
|
40
147
|
|
41
148
|
context 'config_file does exist' do
|
42
149
|
before(:each) do
|
43
|
-
|
150
|
+
SimpleNavigation.stub!(:config_file? => true)
|
44
151
|
IO.stub!(:read).and_return('file_content')
|
45
152
|
end
|
46
153
|
it "should not raise an error" do
|
@@ -51,12 +158,12 @@ describe SimpleNavigation do
|
|
51
158
|
SimpleNavigation.load_config
|
52
159
|
end
|
53
160
|
it "should store the read content in the module (default context)" do
|
54
|
-
SimpleNavigation.should_receive(:config_file_name).with(:default)
|
161
|
+
SimpleNavigation.should_receive(:config_file_name).with(:default)
|
55
162
|
SimpleNavigation.load_config
|
56
163
|
SimpleNavigation.config_files[:default].should == 'file_content'
|
57
164
|
end
|
58
165
|
it "should store the content in the module (non default context)" do
|
59
|
-
SimpleNavigation.should_receive(:config_file_name).with(:my_context)
|
166
|
+
SimpleNavigation.should_receive(:config_file_name).with(:my_context)
|
60
167
|
SimpleNavigation.load_config(:my_context)
|
61
168
|
SimpleNavigation.config_files[:my_context].should == 'file_content'
|
62
169
|
end
|
@@ -64,7 +171,7 @@ describe SimpleNavigation do
|
|
64
171
|
|
65
172
|
context 'config_file does not exist' do
|
66
173
|
before(:each) do
|
67
|
-
|
174
|
+
SimpleNavigation.stub!(:config_file? => false)
|
68
175
|
end
|
69
176
|
it {lambda{SimpleNavigation.load_config}.should raise_error}
|
70
177
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-navigation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andi Schacke
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-21 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|