menu_builder 0.4.1 → 0.4.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/Rakefile +1 -1
- data/lib/menu_builder/helper.rb +64 -16
- data/lib/menu_builder/version.rb +1 -1
- data/test/helper_test.rb +40 -14
- metadata +4 -4
data/Rakefile
CHANGED
data/lib/menu_builder/helper.rb
CHANGED
@@ -1,23 +1,71 @@
|
|
1
1
|
module MenuBuilder
|
2
2
|
module ViewHelpers
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
@menu_items = @context.instance_variable_get('@menu_items')
|
7
|
-
end
|
3
|
+
def menu(options={}, &block)
|
4
|
+
content_tag :ul, Menu.new(self, &block).render, options
|
5
|
+
end
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
private
|
8
|
+
|
9
|
+
class MenuItem
|
10
|
+
attr_reader :item, :args, :block
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def initialize(item, args, block)
|
13
|
+
@item, @args, @block = item, args, block
|
14
|
+
end
|
15
|
+
|
16
|
+
def link_to_in_context context
|
17
|
+
context.link_to *@args, &@block
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_sym
|
21
|
+
item.to_sym
|
22
|
+
end
|
16
23
|
end
|
17
|
-
end
|
18
24
|
|
19
|
-
|
20
|
-
|
21
|
-
|
25
|
+
class Menu
|
26
|
+
def initialize(context, &block)
|
27
|
+
@context = context
|
28
|
+
@menu_items = context.instance_variable_get('@menu_items')
|
29
|
+
@actual_items = []
|
30
|
+
@block = block
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing item, *args, &block
|
34
|
+
@actual_items << MenuItem.new(item, args, block)
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def render
|
39
|
+
@block.call(self)
|
40
|
+
@actual_items.map { |item| render_one item }.join.html_safe
|
41
|
+
end
|
42
|
+
|
43
|
+
def render_one item
|
44
|
+
@context.content_tag :li, item.link_to_in_context(@context), html_options_for(item)
|
45
|
+
end
|
46
|
+
|
47
|
+
def html_options_for item
|
48
|
+
css_classes = []
|
49
|
+
css_classes << "current" if included_in_current_items? item
|
50
|
+
css_classes << "first" if first? item
|
51
|
+
css_classes << "last" if last? item
|
52
|
+
|
53
|
+
options = {}
|
54
|
+
options[:class] = css_classes.join(" ") if css_classes.any?
|
55
|
+
options
|
56
|
+
end
|
57
|
+
|
58
|
+
def included_in_current_items?(item)
|
59
|
+
@menu_items.present? && @menu_items.include?(item.to_sym)
|
60
|
+
end
|
61
|
+
|
62
|
+
def last? item
|
63
|
+
@actual_items.last == item
|
64
|
+
end
|
65
|
+
|
66
|
+
def first? item
|
67
|
+
@actual_items.first == item
|
68
|
+
end
|
69
|
+
end
|
22
70
|
end
|
23
|
-
end
|
71
|
+
end
|
data/lib/menu_builder/version.rb
CHANGED
data/test/helper_test.rb
CHANGED
@@ -6,9 +6,9 @@ class HelperTest < ActionView::TestCase
|
|
6
6
|
attr_accessor :controller, :request
|
7
7
|
|
8
8
|
test "menu yields an instance of Menu" do
|
9
|
-
concat(menu
|
9
|
+
concat(menu { |m|
|
10
10
|
assert m.instance_of?(MenuBuilder::ViewHelpers::Menu)
|
11
|
-
|
11
|
+
})
|
12
12
|
end
|
13
13
|
|
14
14
|
test "menu create an unordered list" do
|
@@ -17,7 +17,7 @@ class HelperTest < ActionView::TestCase
|
|
17
17
|
end
|
18
18
|
|
19
19
|
test "menu accept html options like classes and id" do
|
20
|
-
concat(menu
|
20
|
+
concat(menu(:id=>"menu", :class=>"tabs") { |m| })
|
21
21
|
assert_select "ul#menu.tabs"
|
22
22
|
end
|
23
23
|
|
@@ -28,16 +28,15 @@ class HelperTest < ActionView::TestCase
|
|
28
28
|
|
29
29
|
test "create a link inside line item" do
|
30
30
|
concat(menu { |m| concat m.home "Home", "/" })
|
31
|
-
|
32
|
-
assert_dom_equal expected, output_buffer
|
31
|
+
assert_select "ul > li > a"
|
33
32
|
end
|
34
33
|
|
35
34
|
test "set the class to the current item li" do
|
36
35
|
@menu_items = [:home]
|
37
|
-
concat(menu
|
36
|
+
concat(menu { |m|
|
38
37
|
concat m.home "Home", "/"
|
39
38
|
concat m.contact "Store", "/store"
|
40
|
-
|
39
|
+
})
|
41
40
|
|
42
41
|
assert_select "li.current", 1
|
43
42
|
end
|
@@ -45,11 +44,11 @@ class HelperTest < ActionView::TestCase
|
|
45
44
|
test "accept more than one menu item" do
|
46
45
|
@menu_items = [:settings, :notifications]
|
47
46
|
|
48
|
-
concat(menu
|
47
|
+
concat(menu { |m|
|
49
48
|
concat m.home "Home", "/"
|
50
49
|
concat m.notifications "Notifications", "/notifications"
|
51
50
|
concat m.settings "Settings", "/settings"
|
52
|
-
|
51
|
+
})
|
53
52
|
|
54
53
|
assert_select "li.current", 2
|
55
54
|
end
|
@@ -57,16 +56,43 @@ class HelperTest < ActionView::TestCase
|
|
57
56
|
test "accept more than one menu" do
|
58
57
|
@menu_items = [:settings, :notifications]
|
59
58
|
|
60
|
-
concat(menu
|
59
|
+
concat(menu { |m|
|
61
60
|
concat m.home "Home", "/"
|
62
61
|
concat m.notifications "Notifications", "/notifications"
|
63
|
-
|
62
|
+
})
|
64
63
|
|
65
|
-
concat(menu
|
64
|
+
concat(menu { |m|
|
66
65
|
concat m.home "Contact", "/contact"
|
67
66
|
concat m.notifications "Settings", "/settings"
|
68
|
-
|
67
|
+
})
|
69
68
|
|
70
69
|
assert_select "li.current", 2
|
71
70
|
end
|
72
|
-
|
71
|
+
|
72
|
+
test "marks the first item with class .first" do
|
73
|
+
concat(menu { |m|
|
74
|
+
concat m.home "Home", "/"
|
75
|
+
concat m.contact "Store", "/store"
|
76
|
+
})
|
77
|
+
assert_select "li.first", 1
|
78
|
+
end
|
79
|
+
|
80
|
+
test 'marks the last item with class .last' do
|
81
|
+
concat(menu { |m|
|
82
|
+
concat m.home "Home", "/"
|
83
|
+
concat m.contact "Store", "/store"
|
84
|
+
concat m.notifications "Settings", "/settings"
|
85
|
+
})
|
86
|
+
assert_select "li.last", 1
|
87
|
+
end
|
88
|
+
|
89
|
+
test "pass a block to an item" do
|
90
|
+
concat(menu { |m|
|
91
|
+
concat(m.account("/account") {
|
92
|
+
concat tag(:img, nil, { :src => 'icon.jpg' })
|
93
|
+
})
|
94
|
+
})
|
95
|
+
|
96
|
+
assert_select "ul > li > a > img"
|
97
|
+
end
|
98
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: menu_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 2
|
10
|
+
version: 0.4.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel Lopes
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-06 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: actionpack
|