semantic_navigation 0.0.10 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Changelog.md +11 -0
- data/lib/semantic_navigation/configuration.rb +1 -5
- data/lib/semantic_navigation/core/base.rb +12 -0
- data/lib/semantic_navigation/core/leaf.rb +6 -2
- data/lib/semantic_navigation/core/navigation.rb +37 -3
- data/lib/semantic_navigation/core/node.rb +7 -2
- data/lib/semantic_navigation/renderers/render_helpers.rb +5 -7
- data/lib/semantic_navigation/twitter_bootstrap/tabs.rb +4 -4
- data/lib/semantic_navigation/version.rb +1 -1
- data/semantic_navigation.gemspec +1 -0
- data/spec/lib/semantic_navigation/configuration_spec.rb +0 -50
- data/spec/lib/semantic_navigation/core/base_spec.rb +1 -0
- data/spec/lib/semantic_navigation/core/leaf_spec.rb +98 -0
- data/spec/lib/semantic_navigation/core/navigation_spec.rb +181 -0
- data/spec/lib/semantic_navigation/core/node_spec.rb +118 -0
- data/spec/spec_helper.rb +8 -1
- metadata +21 -6
data/.gitignore
CHANGED
data/Changelog.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
### 0.0.12 / Not yet
|
2
|
+
|
3
|
+
* Bug fixes in renderers
|
4
|
+
* Added possibility to pass an array of urls for one item
|
5
|
+
|
6
|
+
### 0.0.11 / August 4, 2012
|
7
|
+
|
8
|
+
* Added possibility to define urls in Rails routes way like 'namespace/controller#action'
|
9
|
+
* Removed empty `class` parameter for tags
|
10
|
+
* Added new semantic navigation configuration methods `header` and `divider`
|
11
|
+
|
1
12
|
### 0.0.10 / July 30, 2012
|
2
13
|
|
3
14
|
* Added Simple bootstrap navigation class
|
@@ -33,11 +33,7 @@ module SemanticNavigation
|
|
33
33
|
def self.styles_for(name)
|
34
34
|
@@render_styles[name.to_sym] = proc
|
35
35
|
end
|
36
|
-
|
37
|
-
def self.to_s
|
38
|
-
"<#{self.name}:#{@@navigations};#{@@renderers}>"
|
39
|
-
end
|
40
|
-
|
36
|
+
|
41
37
|
def self.register_renderer(*options)
|
42
38
|
if options.count == 1
|
43
39
|
name = options[0].name.demodulize.underscore.to_sym
|
@@ -26,6 +26,18 @@ module SemanticNavigation
|
|
26
26
|
def view_object
|
27
27
|
SemanticNavigation::Configuration.view_object
|
28
28
|
end
|
29
|
+
|
30
|
+
def current_page?(options = {})
|
31
|
+
result = true
|
32
|
+
if options.is_a? Hash
|
33
|
+
options.each do |key, value|
|
34
|
+
result &= (view_object.params[key.to_sym].to_s == value.to_s)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
result = view_object.current_page? options
|
38
|
+
end
|
39
|
+
result
|
40
|
+
end
|
29
41
|
|
30
42
|
end
|
31
43
|
end
|
@@ -1,8 +1,12 @@
|
|
1
1
|
module SemanticNavigation
|
2
2
|
module Core
|
3
3
|
class Leaf < Base
|
4
|
-
attr :
|
4
|
+
attr :link_classes
|
5
5
|
|
6
|
+
def url
|
7
|
+
@url.is_a?(Array) ? @url.first : @url
|
8
|
+
end
|
9
|
+
|
6
10
|
def initialize(options, level)
|
7
11
|
super options, level
|
8
12
|
end
|
@@ -18,7 +22,7 @@ module SemanticNavigation
|
|
18
22
|
|
19
23
|
def mark_active
|
20
24
|
if @url
|
21
|
-
@active =
|
25
|
+
@active = [@url].flatten(1).map{|u| current_page?(u) rescue false}.reduce(:"|")
|
22
26
|
else
|
23
27
|
@active = false
|
24
28
|
end
|
@@ -10,7 +10,7 @@ module SemanticNavigation
|
|
10
10
|
|
11
11
|
def item(id, url=nil, options={}, &block)
|
12
12
|
options[:id] = id.to_sym
|
13
|
-
options[:url] = url unless url.nil?
|
13
|
+
options[:url] = decode_url(url) unless url.nil?
|
14
14
|
options[:i18n_name] = @i18n_name
|
15
15
|
|
16
16
|
if block_given?
|
@@ -21,14 +21,48 @@ module SemanticNavigation
|
|
21
21
|
end
|
22
22
|
|
23
23
|
@sub_elements.push element
|
24
|
-
end
|
25
|
-
|
24
|
+
end
|
25
|
+
|
26
|
+
def header(id, options={})
|
27
|
+
options[:id] = id.to_sym
|
28
|
+
options[:url] = nil
|
29
|
+
options[:i18n_name] = @i18n_name
|
30
|
+
@sub_elements.push Leaf.new(options, @level+1)
|
31
|
+
end
|
32
|
+
|
33
|
+
def divider
|
34
|
+
options = {:id => :divider,
|
35
|
+
:url => nil,
|
36
|
+
:i18n_name => nil}
|
37
|
+
@sub_elements.push Leaf.new(options, @level+1)
|
38
|
+
end
|
39
|
+
|
40
|
+
def method_missing(m,*args,&block)
|
41
|
+
if m.to_s.match(/^[_]+$/).to_a.size > 0
|
42
|
+
divider
|
43
|
+
else
|
44
|
+
super(m,args,&block)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
26
48
|
def mark_active
|
27
49
|
@sub_elements.each do |element|
|
28
50
|
element.mark_active
|
29
51
|
end
|
30
52
|
@active = !@sub_elements.find{|element| element.active}.nil?
|
31
53
|
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def decode_url(url)
|
58
|
+
if url.is_a? String
|
59
|
+
controller_name, action_name = url.split('#')
|
60
|
+
if controller_name && action_name
|
61
|
+
url = {:controller => controller_name, :action => action_name}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
url
|
65
|
+
end
|
32
66
|
|
33
67
|
end
|
34
68
|
end
|
@@ -1,9 +1,14 @@
|
|
1
1
|
module SemanticNavigation
|
2
2
|
module Core
|
3
3
|
class Node < Navigation
|
4
|
-
attr :
|
4
|
+
attr :link_classes, :node_classes
|
5
|
+
|
6
|
+
def url
|
7
|
+
@url.is_a?(Array) ? @url.first : @url
|
8
|
+
end
|
5
9
|
|
6
10
|
def initialize(options, level)
|
11
|
+
@url = []
|
7
12
|
super options, level
|
8
13
|
end
|
9
14
|
|
@@ -18,7 +23,7 @@ module SemanticNavigation
|
|
18
23
|
|
19
24
|
def mark_active
|
20
25
|
@sub_elements.each{|element| element.mark_active}
|
21
|
-
@active =
|
26
|
+
@active = [@url].flatten(1).map{|u| current_page?(u) rescue false}.reduce(:"|")
|
22
27
|
@active |= !@sub_elements.find{|element| element.active}.nil?
|
23
28
|
end
|
24
29
|
|
@@ -93,13 +93,11 @@ module SemanticNavigation
|
|
93
93
|
end
|
94
94
|
|
95
95
|
def merge_classes(name, active, object_classes = [])
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
classes += [object_classes].flatten
|
102
|
-
classes.compact
|
96
|
+
default_classes = send("#{name}_default_classes")
|
97
|
+
active_classes = active && send("show_#{name}_active_class") ? send("#{name}_active_class") : nil
|
98
|
+
|
99
|
+
classes = [*default_classes, *active_classes, *object_classes]
|
100
|
+
!classes.empty? ? classes : nil
|
103
101
|
end
|
104
102
|
|
105
103
|
def show_id(name, id)
|
@@ -18,17 +18,17 @@ module SemanticNavigation
|
|
18
18
|
|
19
19
|
def navigation(object)
|
20
20
|
content_tag :ul, nil, :id => show_id(:navigation, object.id),
|
21
|
-
:class => merge_classes(:navigation, object.active, object.classes
|
21
|
+
:class => merge_classes(:navigation, object.active, [object.classes,"pull-#{direction}"]) do
|
22
22
|
yield
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def node(object)
|
27
27
|
content_tag :li, nil, :id => show_id(:leaf, object.id),
|
28
|
-
:class => merge_classes(:leaf, object.active, object.classes
|
28
|
+
:class => merge_classes(:leaf, object.active, [object.classes,'dropdown'].flatten) do
|
29
29
|
content_tag(:a, :href => '#',
|
30
30
|
:id => show_id(:link, object.id),
|
31
|
-
:class => merge_classes(:link, object.active, object.link_classes
|
31
|
+
:class => merge_classes(:link, object.active, [object.link_classes,'dropdown-toggle'].flatten),
|
32
32
|
'data-toggle'=> :dropdown) do
|
33
33
|
[object.ico ? content_tag(:i,nil,:class => "icon-#{object.ico}") : '',
|
34
34
|
object.name,
|
@@ -41,7 +41,7 @@ module SemanticNavigation
|
|
41
41
|
|
42
42
|
def node_content(object)
|
43
43
|
content_tag(:ul, nil, :id => show_id(:node, object.id),
|
44
|
-
:class => merge_classes(:node, false, object.node_classes
|
44
|
+
:class => merge_classes(:node, false, [object.node_classes,'dropdown-menu'].flatten)) do
|
45
45
|
yield
|
46
46
|
end
|
47
47
|
end
|
data/semantic_navigation.gemspec
CHANGED
@@ -2,54 +2,4 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe SemanticNavigation::Configuration do
|
4
4
|
|
5
|
-
before :all do
|
6
|
-
@config_class = SemanticNavigation::Configuration
|
7
|
-
default_renderers = @config_class.class_variable_get("@@renderers")
|
8
|
-
default_renderers.keys.should == [:list, :breadcrumb, :bootstrap_breadcrumb, :bootstrap_list, :bootstrap_tabs, :bootstrap_pills]
|
9
|
-
end
|
10
|
-
|
11
|
-
describe '#navigate' do
|
12
|
-
it 'should accept navigation method' do
|
13
|
-
@config_class.class_variable_get("@@navigations").should == {}
|
14
|
-
SemanticNavigation::Configuration.run do
|
15
|
-
navigate :navigation
|
16
|
-
end
|
17
|
-
navigations = @config_class.class_variable_get("@@navigations")
|
18
|
-
navigations[:navigation].should_not be_nil
|
19
|
-
navigations[:navigation].id.should == :navigation
|
20
|
-
navigations[:navigation].instance_variable_get("@i18n_name").should == 'semantic_navigation.navigation'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe '#styles_for' do
|
25
|
-
it 'should save the proc' do
|
26
|
-
|
27
|
-
SemanticNavigation::Configuration.run do
|
28
|
-
styles_for :navigation do
|
29
|
-
navigation_default_classes [:default]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
render_styles = @config_class.class_variable_get("@@render_styles")
|
34
|
-
render_styles[:navigation].should_not be_nil
|
35
|
-
render_styles[:navigation].class.should == Proc
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe 'register_render' do
|
40
|
-
it 'should get the name and render class and reg as renderer' do
|
41
|
-
@config_class.register_renderer(:some_renderer,CustomRenderer)
|
42
|
-
renderers = @config_class.class_variable_get("@@renderers")
|
43
|
-
renderers[:some_renderer].should_not be_nil
|
44
|
-
renderers[:some_renderer].should == CustomRenderer
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should receive only renderer class and make a renderer name for itself' do
|
48
|
-
@config_class.register_renderer(CustomRenderer)
|
49
|
-
renderers = @config_class.class_variable_get("@@renderers")
|
50
|
-
renderers[:custom_renderer].should_not be_nil
|
51
|
-
renderers[:custom_renderer].should == CustomRenderer
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
5
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'spec_helper'
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SemanticNavigation::Core::Leaf do
|
4
|
+
describe '#name' do
|
5
|
+
|
6
|
+
it 'should return saved name' do
|
7
|
+
leaf = SemanticNavigation::Core::Leaf.new({:id => :first, :name => 'first'},1)
|
8
|
+
leaf.name.should == 'first'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should return i18n name if @name is nil' do
|
12
|
+
leaf = SemanticNavigation::Core::Leaf.new({:id => :first, :i18n_name => 'some_navigation'},1)
|
13
|
+
I18n.should_receive(:t).with("some_navigation.first", {:default => ""}).and_return 'first'
|
14
|
+
leaf.name.should == 'first'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should return result of proc block if name is_a? proc' do
|
18
|
+
leaf = SemanticNavigation::Core::Leaf.new({:name => proc{["first", "item"].join(' ')}},1)
|
19
|
+
leaf.name.should == "first item"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#url' do
|
24
|
+
it 'should return passed url' do
|
25
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => {:controller => 'controller', :action => 'action'}},1)
|
26
|
+
leaf.url.should == {:controller => 'controller', :action => 'action'}
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should return first url if passed array of urls' do
|
30
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => [{:controller => 'controller1', :action => 'action'},
|
31
|
+
{:controller => 'controller2', :action => 'action'}]},1)
|
32
|
+
leaf.url.should == {:controller => 'controller1', :action => 'action'}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#mark_active' do
|
37
|
+
|
38
|
+
before :each do
|
39
|
+
@view_object = mock
|
40
|
+
SemanticNavigation::Configuration.stub!(:view_object).and_return @view_object
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should set as active if have active url with symbol names' do
|
44
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => {:controller => :first, :action => :index}}, 1)
|
45
|
+
@view_object.stub(:params).and_return({:controller => 'first', :action => 'index'})
|
46
|
+
leaf.mark_active.should be_true
|
47
|
+
leaf.active.should be_true
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should set as active if have active url with string names' do
|
51
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => {:controller => "first", :action => "index"}}, 1)
|
52
|
+
@view_object.stub(:params).and_return({:controller => 'first', :action => 'index'})
|
53
|
+
leaf.mark_active.should be_true
|
54
|
+
leaf.active.should be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should set as inactive if have inactive url with symbol names' do
|
58
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => {:controller => :first, :action => :index}}, 1)
|
59
|
+
@view_object.stub(:params).and_return({controller: "second", action: 'index'})
|
60
|
+
leaf.mark_active.should be_false
|
61
|
+
leaf.active.should be_false
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should set as inactive if have inactive url with string names' do
|
65
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => {:controller => "first", :action => "index"}}, 1)
|
66
|
+
@view_object.stub(:params).and_return({controller: "second", action: 'index'})
|
67
|
+
leaf.mark_active.should be_false
|
68
|
+
leaf.active.should be_false
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should set as inactive if have nil url' do
|
72
|
+
leaf = SemanticNavigation::Core::Leaf.new({},1)
|
73
|
+
leaf.mark_active.should be_false
|
74
|
+
leaf.active.should be_false
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should be active if at least one url in passed array is active' do
|
78
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => [{:controller => :leaf_controller1, :action => :action},
|
79
|
+
{:controller => :leaf_controller2, :action => :action}]},1)
|
80
|
+
@view_object.stub(:params).and_return(:controller => 'leaf_controller2', :action => 'action')
|
81
|
+
leaf.mark_active
|
82
|
+
leaf.active.should be_true
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should accept array like urls with other urls' do
|
86
|
+
leaf = SemanticNavigation::Core::Leaf.new({:url => [['url','with','id'],
|
87
|
+
:symbol_url_name,
|
88
|
+
{:controller => 'hash', :action => 'url'},
|
89
|
+
"string_url"]}, 1)
|
90
|
+
leaf.should_receive(:current_page?).with(['url','with','id'])
|
91
|
+
leaf.should_receive(:current_page?).with(:symbol_url_name)
|
92
|
+
leaf.should_receive(:current_page?).with({:controller => 'hash', :action => 'url'})
|
93
|
+
leaf.should_receive(:current_page?).with("string_url")
|
94
|
+
leaf.mark_active
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SemanticNavigation::Core::Navigation do
|
4
|
+
|
5
|
+
describe '#initialize' do
|
6
|
+
|
7
|
+
it 'should create instance of navigation with level 0 and empty subitems' do
|
8
|
+
navigation = SemanticNavigation::Core::Navigation.new({})
|
9
|
+
navigation.level.should == 0
|
10
|
+
navigation.sub_elements.should be_empty
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should create instance and save sended options' do
|
14
|
+
navigation = SemanticNavigation::Core::Navigation.new :id => :some_id,
|
15
|
+
:classes => [:one, :two]
|
16
|
+
navigation.id.should == :some_id
|
17
|
+
navigation.classes.should == [:one,:two]
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should create instance and save unintended properties' do
|
21
|
+
navigation = SemanticNavigation::Core::Navigation.new :some_attribute => :some_value
|
22
|
+
navigation.instance_variable_get("@some_attribute").should == :some_value
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#item' do
|
28
|
+
before :each do
|
29
|
+
@navigation = SemanticNavigation::Core::Navigation.new({})
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should receive item method and make the leaf" do
|
33
|
+
@navigation.item :some_id, 'some_url'
|
34
|
+
@navigation.sub_elements.size.should == 1
|
35
|
+
@navigation.sub_elements.first.is_a?(SemanticNavigation::Core::Leaf).should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should receive item method with block and create node" do
|
39
|
+
@navigation.item :node_id, 'node_url' do
|
40
|
+
item :leaf_id, 'leaf_url'
|
41
|
+
end
|
42
|
+
@navigation.sub_elements.size.should == 1
|
43
|
+
@navigation.sub_elements.first.is_a?(SemanticNavigation::Core::Node).should be_true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#header' do
|
48
|
+
before :each do
|
49
|
+
@navigation = SemanticNavigation::Core::Navigation.new({})
|
50
|
+
@navigation.header :some_id
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should create item with nil url" do
|
54
|
+
@navigation.sub_elements.size.should == 1
|
55
|
+
@navigation.sub_elements.first.url.should be_nil
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#divider" do
|
60
|
+
before :each do
|
61
|
+
@navigation = SemanticNavigation::Core::Navigation.new({})
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should create divider item with nil url and name" do
|
65
|
+
@navigation.divider
|
66
|
+
@navigation.sub_elements.size.should == 1
|
67
|
+
@navigation.sub_elements.first.url.should be_nil
|
68
|
+
@navigation.sub_elements.first.name.should be_empty
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should receive any length method containing char `_` and create divider" do
|
72
|
+
|
73
|
+
(1..10).each do |c|
|
74
|
+
@navigation.send ('_'*c).to_sym
|
75
|
+
end
|
76
|
+
@navigation.sub_elements.size.should == 10
|
77
|
+
@navigation.sub_elements.map(&:id).should == [:divider]*10
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#decode_url" do
|
82
|
+
|
83
|
+
before :each do
|
84
|
+
@navigation = SemanticNavigation::Core::Navigation.new({})
|
85
|
+
end
|
86
|
+
|
87
|
+
it "while creating the item make support for urls in format controller#action" do
|
88
|
+
view_object = mock
|
89
|
+
|
90
|
+
@navigation.item :some_id, 'controller#action'
|
91
|
+
@navigation.sub_elements.size.should == 1
|
92
|
+
@navigation.sub_elements.first.url.should == {:controller => "controller", :action => "action"}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '#render_if' do
|
97
|
+
it 'should return true if render_if proc is nil' do
|
98
|
+
navigation = SemanticNavigation::Core::Navigation.new({})
|
99
|
+
navigation.render_if.should be_true
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should return true if render_if proc return true' do
|
103
|
+
navigation = SemanticNavigation::Core::Navigation.new({:render_if => proc{true}})
|
104
|
+
navigation.render_if.should be_true
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should return false if render_if proc return false' do
|
108
|
+
navigation = SemanticNavigation::Core::Navigation.new({:render_if => proc{false}})
|
109
|
+
navigation.render_if.should be_false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#render' do
|
114
|
+
it 'should call renderer class :render_navigation method' do
|
115
|
+
renderer = mock
|
116
|
+
navigation = SemanticNavigation::Core::Navigation.new({})
|
117
|
+
renderer.should_receive(:render_navigation).with(navigation)
|
118
|
+
navigation.render(renderer)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe '#mark_active' do
|
123
|
+
before :each do
|
124
|
+
@navigation = SemanticNavigation::Core::Navigation.new({})
|
125
|
+
|
126
|
+
@view_object = mock
|
127
|
+
SemanticNavigation::Configuration.stub(:view_object).and_return @view_object
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should define @active variable to false if no sub_elemetns' do
|
131
|
+
@navigation.mark_active
|
132
|
+
@navigation.instance_variable_get("@active").should be false
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should define @active variable to true if at least one sub_element is active' do
|
136
|
+
@navigation.item :first_item, '111'
|
137
|
+
@navigation.item :second_item, '222'
|
138
|
+
@view_object.should_receive(:current_page?).with('111').and_return true
|
139
|
+
@view_object.should_receive(:current_page?).with('222').and_return false
|
140
|
+
|
141
|
+
@navigation.mark_active
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should define @active variable to false if all sub_elements is unactive' do
|
145
|
+
@navigation.item :first_item, '333'
|
146
|
+
@navigation.item :secodn_item, '444'
|
147
|
+
@view_object.should_receive(:current_page?).with('333').and_return false
|
148
|
+
@view_object.should_receive(:current_page?).with('444').and_return false
|
149
|
+
|
150
|
+
@navigation.mark_active
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should use custom current_page? for Hash url params' do
|
154
|
+
@navigation.item :first_item, {:controller => "controller1", :action => "action"}
|
155
|
+
@navigation.item :second_item, {:controller => "controller2", :action => "action"}
|
156
|
+
@view_object.stub(:params).and_return({:controller => "controller1", :action => "action", :some_other_params => "blablabla"})
|
157
|
+
|
158
|
+
@navigation.mark_active
|
159
|
+
@navigation.sub_elements[0].active.should be_true
|
160
|
+
@navigation.sub_elements[1].active.should be_false
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'should work for route like urls as good as for Hash url params' do
|
164
|
+
@navigation.item :first_item, "controller1#action"
|
165
|
+
@navigation.item :second_item, "controller2#action"
|
166
|
+
@view_object.stub(:params).and_return({:controller => "controller1", :action => "action", :some_other_params => "blablabla"})
|
167
|
+
|
168
|
+
@navigation.mark_active
|
169
|
+
@navigation.sub_elements[0].active.should be_true
|
170
|
+
@navigation.sub_elements[1].active.should be_false
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
describe '#method_missing' do
|
176
|
+
it 'should get unknown method and call super' do
|
177
|
+
@navigation = SemanticNavigation::Core::Navigation.new({})
|
178
|
+
lambda{@navigation.unknow_method}.should raise_exception
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SemanticNavigation::Core::Node do
|
4
|
+
|
5
|
+
describe '#name' do
|
6
|
+
|
7
|
+
it 'should return passed name while creating node' do
|
8
|
+
node = SemanticNavigation::Core::Node.new({:name => 'some name'}, 1)
|
9
|
+
node.name.should == 'some name'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should return i18n name if passed name is nil' do
|
13
|
+
node = SemanticNavigation::Core::Node.new({:id => :some_id, :i18n_name => :parent_name}, 1)
|
14
|
+
I18n.should_receive(:t).with("parent_name.some_id", {:default=>""}).and_return 'some name'
|
15
|
+
node.name.should == 'some name'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should return result of proc if passed name is a proc' do
|
19
|
+
node = SemanticNavigation::Core::Node.new({:name => proc{'some name'}},1)
|
20
|
+
node.name.should == 'some name'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#url' do
|
25
|
+
it 'should return passed url' do
|
26
|
+
node = SemanticNavigation::Core::Node.new({:url => {:controller => 'controller', :action => 'action'}},1)
|
27
|
+
node.url.should == {:controller => 'controller', :action => 'action'}
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should return first url if passed array of urls' do
|
31
|
+
node = SemanticNavigation::Core::Node.new({:url => [{:controller => 'controller1', :action => 'action'},
|
32
|
+
{:controller => 'controller2', :action => 'action'}]},1)
|
33
|
+
node.url.should == {:controller => 'controller1', :action => 'action'}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#mark_active' do
|
38
|
+
before :each do
|
39
|
+
@node = SemanticNavigation::Core::Node.new({:url => [{:controller => :node_controller, :action => :action},
|
40
|
+
{:controller => :node_controller2, :action => :action}]},1)
|
41
|
+
|
42
|
+
@view_object = mock
|
43
|
+
@view_object.stub(:params).and_return({:action => "some", :action => "some"})
|
44
|
+
SemanticNavigation::Configuration.stub(:view_object).and_return @view_object
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should define @active variable to false if no active sub_elements and node url is not active' do
|
48
|
+
@node.mark_active
|
49
|
+
@node.instance_variable_get("@active").should be_false
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should define @active variable to true if at least one sub_element is active' do
|
53
|
+
@node.item :first_item, '111'
|
54
|
+
@node.item :second_item, '222'
|
55
|
+
@view_object.should_receive(:current_page?).with('111').and_return true
|
56
|
+
@view_object.should_receive(:current_page?).with('222').and_return false
|
57
|
+
|
58
|
+
@node.mark_active
|
59
|
+
@node.sub_elements[0].active.should be_true
|
60
|
+
@node.sub_elements[1].active.should be_false
|
61
|
+
@node.active.should be_true
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should define @active variable to false if all sub_elements is unactive' do
|
65
|
+
@node.item :first_item, '333'
|
66
|
+
@node.item :second_item, '444'
|
67
|
+
@view_object.should_receive(:current_page?).with('333').and_return false
|
68
|
+
@view_object.should_receive(:current_page?).with('444').and_return false
|
69
|
+
|
70
|
+
@node.mark_active
|
71
|
+
@node.sub_elements[0].active.should be_false
|
72
|
+
@node.sub_elements[1].active.should be_false
|
73
|
+
@node.active.should be_false
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should use custom current_page? for Hash url params' do
|
77
|
+
@node.item :first_item, {:controller => "controller1", :action => "action"}
|
78
|
+
@node.item :second_item, {:controller => "controller2", :action => "action"}
|
79
|
+
@view_object.stub(:params).and_return({:controller => "controller1", :action => "action", :some_other_params => "blablabla"})
|
80
|
+
|
81
|
+
@node.mark_active
|
82
|
+
@node.sub_elements[0].active.should be_true
|
83
|
+
@node.sub_elements[1].active.should be_false
|
84
|
+
@node.active.should be true
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should work for route like urls as good as for Hash url params' do
|
88
|
+
@node.item :first_item, "controller1#action"
|
89
|
+
@node.item :second_item, "controller2#action"
|
90
|
+
@view_object.stub(:params).and_return({:controller => "controller1", :action => "action", :some_other_params => "blablabla"})
|
91
|
+
|
92
|
+
@node.mark_active
|
93
|
+
@node.sub_elements[0].active.should be_true
|
94
|
+
@node.sub_elements[1].active.should be_false
|
95
|
+
@node.active.should be true
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should be active if at least one url in passed array is active' do
|
99
|
+
@view_object.stub(:params).and_return(:controller => 'node_controller2', :action => 'action')
|
100
|
+
@node.mark_active
|
101
|
+
@node.active.should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should accept array like urls with other urls' do
|
105
|
+
node = SemanticNavigation::Core::Node.new({:url => [['url','with','id'],
|
106
|
+
:symbol_url_name,
|
107
|
+
{:controller => 'hash', :action => 'url'},
|
108
|
+
"string_url"]}, 1)
|
109
|
+
node.should_receive(:current_page?).with(['url','with','id'])
|
110
|
+
node.should_receive(:current_page?).with(:symbol_url_name)
|
111
|
+
node.should_receive(:current_page?).with({:controller => 'hash', :action => 'url'})
|
112
|
+
node.should_receive(:current_page?).with("string_url")
|
113
|
+
node.mark_active
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
require 'rails'
|
2
1
|
require 'rspec'
|
2
|
+
require 'rails'
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter '/spec/'
|
6
|
+
add_group "Core", "lib/semantic_navigation/core"
|
7
|
+
add_group "Renderers", "lib/semantic_navigation/renderers"
|
8
|
+
add_group "Twitter Bootstrap Renderer", "lib/semantic_navigation/twitter_bootstrap"
|
9
|
+
end
|
3
10
|
require 'semantic_navigation'
|
4
11
|
|
5
12
|
RSpec.configure do |config|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: semantic_navigation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &13675440 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: 2.0.1
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *13675440
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: simplecov
|
27
|
+
requirement: &13674940 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *13674940
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rails
|
27
|
-
requirement: &
|
38
|
+
requirement: &13674260 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,7 +43,7 @@ dependencies:
|
|
32
43
|
version: 3.0.0
|
33
44
|
type: :runtime
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *13674260
|
36
47
|
description: ! "Simply and customizable navigation in the Ruby on Rails 3 application.\n
|
37
48
|
\ Predefined bootstrap renderers"
|
38
49
|
email:
|
@@ -70,6 +81,10 @@ files:
|
|
70
81
|
- lib/semantic_navigation/version.rb
|
71
82
|
- semantic_navigation.gemspec
|
72
83
|
- spec/lib/semantic_navigation/configuration_spec.rb
|
84
|
+
- spec/lib/semantic_navigation/core/base_spec.rb
|
85
|
+
- spec/lib/semantic_navigation/core/leaf_spec.rb
|
86
|
+
- spec/lib/semantic_navigation/core/navigation_spec.rb
|
87
|
+
- spec/lib/semantic_navigation/core/node_spec.rb
|
73
88
|
- spec/lib/semantic_navigation/custom_renderer.rb
|
74
89
|
- spec/lib/semantic_navigation/helper_methods_spec.rb
|
75
90
|
- spec/lib/semantic_navigation_spec.rb
|