simple-navigation 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/VERSION.yml +2 -2
- data/lib/simple_navigation.rb +29 -14
- data/lib/simple_navigation/configuration.rb +3 -3
- data/lib/simple_navigation/controller_methods.rb +2 -2
- data/lib/simple_navigation/helpers.rb +29 -16
- data/rails/init.rb +1 -2
- data/simple-navigation.gemspec +2 -2
- data/spec/lib/simple_navigation/configuration_spec.rb +14 -6
- data/spec/lib/simple_navigation/helpers_spec.rb +18 -39
- data/spec/lib/simple_navigation_spec.rb +95 -4
- metadata +2 -2
data/CHANGELOG
CHANGED
data/VERSION.yml
CHANGED
data/lib/simple_navigation.rb
CHANGED
@@ -1,25 +1,40 @@
|
|
1
1
|
# A plugin for generating a simple navigation. See README for resources on usage instructions.
|
2
2
|
module SimpleNavigation
|
3
3
|
|
4
|
-
mattr_accessor :
|
4
|
+
mattr_accessor :config_files
|
5
5
|
mattr_accessor :config_file_path
|
6
6
|
mattr_accessor :controller
|
7
|
+
self.config_files = {}
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Reads the config_file for the specified navigation_context and stores it for later evaluation.
|
12
|
+
def load_config(navigation_context = :default)
|
13
|
+
raise "config_file_path is not set!" unless self.config_file_path
|
14
|
+
raise "Config file '#{config_file_name(navigation_context)}' does not exists!" unless File.exists?(config_file_name(navigation_context))
|
15
|
+
if ::RAILS_ENV == 'production'
|
16
|
+
self.config_files[navigation_context] ||= IO.read(config_file_name(navigation_context))
|
17
|
+
else
|
18
|
+
self.config_files[navigation_context] = IO.read(config_file_name(navigation_context))
|
19
|
+
end
|
20
|
+
end
|
14
21
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
22
|
+
# Returns the singleton instance of the SimpleNavigation::Configuration
|
23
|
+
def config
|
24
|
+
Configuration.instance
|
25
|
+
end
|
19
26
|
|
20
|
-
|
21
|
-
|
22
|
-
|
27
|
+
# Returns the ItemContainer that contains the items for the primary navigation
|
28
|
+
def primary_navigation
|
29
|
+
config.primary_navigation
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns the path to the config_file for the given navigation_context
|
33
|
+
def config_file_name(navigation_context = :default)
|
34
|
+
file_name = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
|
35
|
+
File.join(config_file_path, "#{file_name}navigation.rb")
|
36
|
+
end
|
37
|
+
|
23
38
|
end
|
24
39
|
|
25
40
|
end
|
@@ -14,9 +14,9 @@ module SimpleNavigation
|
|
14
14
|
|
15
15
|
class << self
|
16
16
|
|
17
|
-
# Evals the config_file inside the specified context (usually a controller or view)
|
18
|
-
def eval_config(context)
|
19
|
-
context.instance_eval(SimpleNavigation.
|
17
|
+
# Evals the config_file for the given navigation_context inside the specified context (usually a controller or view)
|
18
|
+
def eval_config(context, navigation_context = :default)
|
19
|
+
context.instance_eval(SimpleNavigation.config_files[navigation_context])
|
20
20
|
SimpleNavigation.controller = extract_controller_from context
|
21
21
|
end
|
22
22
|
|
@@ -28,11 +28,11 @@ module SimpleNavigation
|
|
28
28
|
#
|
29
29
|
# def your_special_action
|
30
30
|
# ...
|
31
|
-
# current_navigation :special
|
31
|
+
# current_navigation :account, :special
|
32
32
|
# end
|
33
33
|
# end
|
34
34
|
#
|
35
|
-
#
|
35
|
+
# The code above still sets the active primary navigation to :account but also sets the sub_navigation to :special for 'your_special_action'.
|
36
36
|
#
|
37
37
|
# Note: The specified symbols must match the keys for your navigation items in your config/navigation.rb file.
|
38
38
|
module ControllerMethods
|
@@ -15,16 +15,29 @@ module SimpleNavigation
|
|
15
15
|
#
|
16
16
|
module Helpers
|
17
17
|
|
18
|
-
# Renders the navigation according to the specified
|
19
|
-
#
|
20
|
-
# The
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
|
27
|
-
|
18
|
+
# Renders the navigation according to the specified options-hash.
|
19
|
+
#
|
20
|
+
# The following options are supported:
|
21
|
+
# * <tt>level</tt> - defaults to :nested which renders the the sub_navigation for an active primary_navigation inside that active primary_navigation item.
|
22
|
+
# Other possible levels are :primary which only renders the primary_navigation (also see render_primary_navigation) and :secondary which only renders the sub_navigation (see render_sub_navigation).
|
23
|
+
# * <tt>context</tt> - specifies the context for which you would render the navigation. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb)
|
24
|
+
# if you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call <tt>render_navigation(:context => :admin)</tt> the file config/admin_navigation.rb
|
25
|
+
# will be loaded and used for rendering the navigation.
|
26
|
+
#
|
27
|
+
def render_navigation(*args)
|
28
|
+
args = [Hash.new] if args.empty?
|
29
|
+
default_options = {:context => :default, :level => :nested}
|
30
|
+
level, navigation_context = case args.first
|
31
|
+
when Hash
|
32
|
+
options = default_options.merge(args.first)
|
33
|
+
[options[:level], options[:context]]
|
34
|
+
when Symbol
|
35
|
+
[args[0], default_options.merge(args[1] || {})[:context]]
|
36
|
+
else
|
37
|
+
raise ArgumentError, "Invalid arguments"
|
38
|
+
end
|
39
|
+
SimpleNavigation.load_config(navigation_context)
|
40
|
+
SimpleNavigation::Configuration.eval_config(self, navigation_context)
|
28
41
|
case level
|
29
42
|
when :primary
|
30
43
|
SimpleNavigation.primary_navigation.render(@current_primary_navigation)
|
@@ -38,14 +51,14 @@ module SimpleNavigation
|
|
38
51
|
end
|
39
52
|
end
|
40
53
|
|
41
|
-
# Renders the primary_navigation with the configured renderer. Calling render_navigation(:primary) has the same effect.
|
42
|
-
def render_primary_navigation
|
43
|
-
render_navigation(:primary)
|
54
|
+
# Renders the primary_navigation with the configured renderer. Calling render_navigation(:level => :primary) has the same effect.
|
55
|
+
def render_primary_navigation(options = {})
|
56
|
+
render_navigation(options.merge(:level => :primary))
|
44
57
|
end
|
45
58
|
|
46
|
-
# Renders the sub_navigation with the configured renderer. Calling render_navigation(:secondary) has the same effect.
|
47
|
-
def render_sub_navigation
|
48
|
-
render_navigation(:secondary)
|
59
|
+
# Renders the sub_navigation with the configured renderer. Calling render_navigation(:level => :secondary) has the same effect.
|
60
|
+
def render_sub_navigation(options = {})
|
61
|
+
render_navigation(options.merge(:level => :secondary))
|
49
62
|
end
|
50
63
|
|
51
64
|
end
|
data/rails/init.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
default_config_file_path = File.join(RAILS_ROOT, 'config'
|
1
|
+
default_config_file_path = File.join(RAILS_ROOT, 'config')
|
2
2
|
SimpleNavigation.config_file_path = default_config_file_path unless SimpleNavigation.config_file_path
|
3
|
-
SimpleNavigation.load_config
|
4
3
|
ActionController::Base.send(:include, SimpleNavigation::ControllerMethods)
|
data/simple-navigation.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{simple-navigation}
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.4.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Andi Schacke"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-08-10}
|
10
10
|
s.description = %q{Simple Navigation is a ruby library for creating a navigation (optionally with sub navigation) for your rails app.}
|
11
11
|
s.email = %q{andreas.schacke@gmail.com}
|
12
12
|
s.extra_rdoc_files = [
|
@@ -18,13 +18,21 @@ describe SimpleNavigation::Configuration do
|
|
18
18
|
before(:each) do
|
19
19
|
@context = mock(:context)
|
20
20
|
@context.stub!(:instance_eval)
|
21
|
-
@
|
22
|
-
SimpleNavigation.stub!(:
|
21
|
+
@config_files = {:default => 'default', :my_context => 'my_context'}
|
22
|
+
SimpleNavigation.stub!(:config_files).and_return(@config_files)
|
23
|
+
end
|
24
|
+
context "with default navigation context" do
|
25
|
+
it "should instance_eval the default config_file-string inside the context" do
|
26
|
+
@context.should_receive(:instance_eval).with('default')
|
27
|
+
SimpleNavigation::Configuration.eval_config(@context)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
context 'with non default navigation context' do
|
31
|
+
it "should instance_eval the specified config_file-string inside the context" do
|
32
|
+
@context.should_receive(:instance_eval).with('my_context')
|
33
|
+
SimpleNavigation::Configuration.eval_config(@context, :my_context)
|
34
|
+
end
|
23
35
|
end
|
24
|
-
it "should instance_eval the config_file-string inside the context" do
|
25
|
-
@context.should_receive(:instance_eval).with(@config_file)
|
26
|
-
SimpleNavigation::Configuration.eval_config(@context)
|
27
|
-
end
|
28
36
|
it "should set the controller" do
|
29
37
|
@controller = stub(:controller)
|
30
38
|
SimpleNavigation::Configuration.should_receive(:extract_controller_from).with(@context).and_return(@controller)
|
@@ -15,50 +15,23 @@ describe SimpleNavigation::Helpers do
|
|
15
15
|
|
16
16
|
describe 'render_navigation' do
|
17
17
|
describe 'regarding loading of the config-file' do
|
18
|
-
context
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
it "should load the config file" do
|
23
|
-
SimpleNavigation.should_receive(:load_config)
|
18
|
+
context 'no options specified' do
|
19
|
+
it "should load the config-file for the default context" do
|
20
|
+
SimpleNavigation.should_receive(:load_config).with(:default)
|
24
21
|
@controller.render_navigation
|
25
22
|
end
|
26
23
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
it "should not load the config file" do
|
33
|
-
SimpleNavigation.should_not_receive(:load_config)
|
34
|
-
@controller.render_navigation
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "RAILS_ENV=development" do
|
39
|
-
before(:each) do
|
40
|
-
::RAILS_ENV = 'development'
|
41
|
-
end
|
42
|
-
it "should load the config file" do
|
43
|
-
SimpleNavigation.should_receive(:load_config)
|
44
|
-
@controller.render_navigation
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "RAILS_ENV=test" do
|
49
|
-
before(:each) do
|
50
|
-
::RAILS_ENV = 'test'
|
51
|
-
end
|
52
|
-
it "should load the config file" do
|
53
|
-
SimpleNavigation.should_receive(:load_config)
|
54
|
-
@controller.render_navigation
|
55
|
-
end
|
24
|
+
|
25
|
+
context 'with options specified' do
|
26
|
+
it "should load the config-file for the specified context" do
|
27
|
+
SimpleNavigation.should_receive(:load_config).with(:my_context)
|
28
|
+
@controller.render_navigation(:context => :my_context)
|
56
29
|
end
|
57
30
|
end
|
58
31
|
end
|
59
32
|
|
60
33
|
it "should eval the config on every request" do
|
61
|
-
SimpleNavigation::Configuration.should_receive(:eval_config).with(@controller)
|
34
|
+
SimpleNavigation::Configuration.should_receive(:eval_config).with(@controller, :default)
|
62
35
|
@controller.render_navigation
|
63
36
|
end
|
64
37
|
|
@@ -70,6 +43,10 @@ describe SimpleNavigation::Helpers do
|
|
70
43
|
@primary_navigation.should_receive(:render).with(:current_primary)
|
71
44
|
@controller.render_navigation(:primary)
|
72
45
|
end
|
46
|
+
it "should call render on the primary_navigation (specifying level through options)" do
|
47
|
+
@primary_navigation.should_receive(:render).with(:current_primary)
|
48
|
+
@controller.render_navigation(:level => :primary)
|
49
|
+
end
|
73
50
|
end
|
74
51
|
|
75
52
|
context 'secondary' do
|
@@ -125,20 +102,22 @@ describe SimpleNavigation::Helpers do
|
|
125
102
|
end
|
126
103
|
|
127
104
|
context 'unknown level' do
|
128
|
-
it
|
105
|
+
it "should raise an error" do
|
106
|
+
lambda {@controller.render_navigation(:unknown)}.should raise_error(ArgumentError)
|
107
|
+
end
|
129
108
|
end
|
130
109
|
end
|
131
110
|
|
132
111
|
describe 'render_primary_navigation' do
|
133
112
|
it "should delegate to render_navigation(:primary)" do
|
134
|
-
@controller.should_receive(:render_navigation).with(:primary)
|
113
|
+
@controller.should_receive(:render_navigation).with(:level => :primary)
|
135
114
|
@controller.render_primary_navigation
|
136
115
|
end
|
137
116
|
end
|
138
117
|
|
139
118
|
describe 'render_sub_navigation' do
|
140
119
|
it "should delegate to render_navigation(:secondary)" do
|
141
|
-
@controller.should_receive(:render_navigation).with(:secondary)
|
120
|
+
@controller.should_receive(:render_navigation).with(:level => :secondary)
|
142
121
|
@controller.render_sub_navigation
|
143
122
|
end
|
144
123
|
end
|
@@ -2,10 +2,40 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
|
2
2
|
|
3
3
|
describe SimpleNavigation do
|
4
4
|
|
5
|
+
describe 'config_files' do
|
6
|
+
before(:each) do
|
7
|
+
SimpleNavigation.config_files = {}
|
8
|
+
end
|
9
|
+
it "should be an empty hash after loading the module" do
|
10
|
+
SimpleNavigation.config_files.should == {}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'config_file_name' do
|
15
|
+
before(:each) do
|
16
|
+
SimpleNavigation.config_file_path = 'path_to_config'
|
17
|
+
end
|
18
|
+
context 'for :default navigation_context' do
|
19
|
+
it "should return the path to default config file" do
|
20
|
+
SimpleNavigation.config_file_name.should == 'path_to_config/navigation.rb'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'for other navigation_context' do
|
25
|
+
it "should return the path to the config file matching the specified context" do
|
26
|
+
SimpleNavigation.config_file_name(:my_other_context).should == 'path_to_config/my_other_context_navigation.rb'
|
27
|
+
end
|
28
|
+
it "should convert camelcase-contexts to underscore" do
|
29
|
+
SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'path_to_config/why_would_you_do_this_navigation.rb'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
5
34
|
describe 'load_config' do
|
6
35
|
context 'config_file_path is set' do
|
7
36
|
before(:each) do
|
8
37
|
SimpleNavigation.config_file_path = 'path_to_config'
|
38
|
+
#SimpleNavigation.stub!(:config_file_name => 'path_to_config/navigation.rb')
|
9
39
|
end
|
10
40
|
|
11
41
|
context 'config_file does exist' do
|
@@ -16,13 +46,19 @@ describe SimpleNavigation do
|
|
16
46
|
it "should not raise an error" do
|
17
47
|
lambda{SimpleNavigation.load_config}.should_not raise_error
|
18
48
|
end
|
19
|
-
it "should read the config file from disc" do
|
20
|
-
IO.should_receive(:read).with('path_to_config')
|
49
|
+
it "should read the specified config file from disc" do
|
50
|
+
IO.should_receive(:read).with('path_to_config/navigation.rb')
|
21
51
|
SimpleNavigation.load_config
|
22
52
|
end
|
23
|
-
it "should store the read content in the module" do
|
24
|
-
SimpleNavigation.should_receive(:
|
53
|
+
it "should store the read content in the module (default context)" do
|
54
|
+
SimpleNavigation.should_receive(:config_file_name).with(:default).twice
|
25
55
|
SimpleNavigation.load_config
|
56
|
+
SimpleNavigation.config_files[:default].should == 'file_content'
|
57
|
+
end
|
58
|
+
it "should store the content in the module (non default context)" do
|
59
|
+
SimpleNavigation.should_receive(:config_file_name).with(:my_context).twice
|
60
|
+
SimpleNavigation.load_config(:my_context)
|
61
|
+
SimpleNavigation.config_files[:my_context].should == 'file_content'
|
26
62
|
end
|
27
63
|
end
|
28
64
|
|
@@ -40,6 +76,61 @@ describe SimpleNavigation do
|
|
40
76
|
end
|
41
77
|
it {lambda{SimpleNavigation.load_config}.should raise_error}
|
42
78
|
end
|
79
|
+
|
80
|
+
describe 'regarding caching of the config-files' do
|
81
|
+
before(:each) do
|
82
|
+
IO.stub!(:read).and_return('file_content')
|
83
|
+
SimpleNavigation.config_file_path = 'path_to_config'
|
84
|
+
File.stub!(:exists? => true)
|
85
|
+
end
|
86
|
+
context "RAILS_ENV undefined" do
|
87
|
+
before(:each) do
|
88
|
+
::RAILS_ENV = nil
|
89
|
+
end
|
90
|
+
it "should load the config file twice" do
|
91
|
+
IO.should_receive(:read).twice
|
92
|
+
SimpleNavigation.load_config
|
93
|
+
SimpleNavigation.load_config
|
94
|
+
end
|
95
|
+
end
|
96
|
+
context "RAILS_ENV defined" do
|
97
|
+
before(:each) do
|
98
|
+
::RAILS_ENV = 'production'
|
99
|
+
end
|
100
|
+
context "RAILS_ENV=production" do
|
101
|
+
it "should load the config file only once" do
|
102
|
+
IO.should_receive(:read).once
|
103
|
+
SimpleNavigation.load_config
|
104
|
+
SimpleNavigation.load_config
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "RAILS_ENV=development" do
|
109
|
+
before(:each) do
|
110
|
+
::RAILS_ENV = 'development'
|
111
|
+
end
|
112
|
+
it "should load the config file twice" do
|
113
|
+
IO.should_receive(:read).twice
|
114
|
+
SimpleNavigation.load_config
|
115
|
+
SimpleNavigation.load_config
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "RAILS_ENV=test" do
|
120
|
+
before(:each) do
|
121
|
+
::RAILS_ENV = 'test'
|
122
|
+
end
|
123
|
+
it "should load the config file twice" do
|
124
|
+
IO.should_receive(:read).twice
|
125
|
+
SimpleNavigation.load_config
|
126
|
+
SimpleNavigation.load_config
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
after(:each) do
|
131
|
+
SimpleNavigation.config_files = {}
|
132
|
+
end
|
133
|
+
end
|
43
134
|
end
|
44
135
|
|
45
136
|
describe 'config' do
|
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: 1.
|
4
|
+
version: 1.4.0
|
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: 2009-
|
12
|
+
date: 2009-08-10 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|