menu_helper 0.0.1 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +31 -0
- data/MIT-LICENSE +2 -2
- data/README +30 -33
- data/Rakefile +9 -8
- data/lib/menu_helper/html_element.rb +45 -47
- data/lib/menu_helper/menu.rb +98 -93
- data/lib/menu_helper/menu_bar.rb +141 -141
- data/lib/menu_helper.rb +72 -73
- data/test/app_root/log/in_memory.log +1 -0
- data/test/test_helper.rb +1 -3
- data/test/unit/html_element_test.rb +73 -49
- data/test/unit/menu_bar_test.rb +31 -19
- data/test/unit/menu_helper_test.rb +5 -5
- data/test/unit/menu_test.rb +28 -23
- metadata +57 -47
data/lib/menu_helper/menu_bar.rb
CHANGED
@@ -1,142 +1,142 @@
|
|
1
|
-
module PluginAWeek #:nodoc:
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
end
|
141
|
-
end
|
1
|
+
module PluginAWeek #:nodoc:
|
2
|
+
module MenuHelper
|
3
|
+
# Represents a group of menus. A menu bar can either be the main menu
|
4
|
+
# bar or a menu bar nested within a menu.
|
5
|
+
class MenuBar < HtmlElement
|
6
|
+
# The menus within this menu bar
|
7
|
+
attr_reader :menus
|
8
|
+
|
9
|
+
def initialize(request_controller, options = {}, html_options = {}, parent_menu = nil) #:nodoc:
|
10
|
+
super(html_options)
|
11
|
+
|
12
|
+
options.assert_valid_keys(:auto_set_ids)
|
13
|
+
options.reverse_merge!(:auto_set_ids => true)
|
14
|
+
@options = options
|
15
|
+
@request_controller = request_controller
|
16
|
+
@parent_menu = parent_menu
|
17
|
+
|
18
|
+
@menus = []
|
19
|
+
|
20
|
+
if @parent_menu
|
21
|
+
self[:id] ||= "#{@parent_menu[:id]}_menubar"
|
22
|
+
else
|
23
|
+
self[:id] ||= 'menubar'
|
24
|
+
end
|
25
|
+
|
26
|
+
yield self if block_given?
|
27
|
+
end
|
28
|
+
|
29
|
+
# Creates a new menu in this bar with the given id. The content
|
30
|
+
# within the menu is, by default, set to a humanized version of the id.
|
31
|
+
#
|
32
|
+
# == URLs with routes
|
33
|
+
#
|
34
|
+
# If you have named routes setup in the application, the menu attempts
|
35
|
+
# to automatically figure out what URL you're trying to link to. It
|
36
|
+
# does this by looking at the id of the menu and the id of its parent.
|
37
|
+
#
|
38
|
+
# For example, a menu_bar with the id 'home' and a menu with the id
|
39
|
+
# 'contact_us' will attempt to look for the following named routes as
|
40
|
+
# the URL to link to (in order of priority):
|
41
|
+
# 1. contact_us_url
|
42
|
+
# 2. home_contact_us_url
|
43
|
+
#
|
44
|
+
# Example routes.rb:
|
45
|
+
# ActionController::Routing::Routes.draw do |map|
|
46
|
+
# map.with_options(:controller => 'home') do |home|
|
47
|
+
# home.home '', :action => 'index'
|
48
|
+
# home.home_search 'search', :action => 'search'
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# map.with_options(:controller => 'about_us') do |about_us|
|
52
|
+
# about_us.about_us 'about_us', :action => 'index'
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# Example menubar:
|
57
|
+
# menu :home do |home|
|
58
|
+
# menu :about_us
|
59
|
+
# #=> Links to about_us_url
|
60
|
+
# menu :search
|
61
|
+
# #=> Links to home_search_url
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# == URLs with url_for
|
65
|
+
#
|
66
|
+
# If neither of these named routes are being used, the url will be based
|
67
|
+
# on the options passed into the menu. The url_options takes the same
|
68
|
+
# values as +url_for+. By default, the name of the controller will be
|
69
|
+
# guessed in the following order:
|
70
|
+
# 1. The id of the menu ('contact_us' => ContactUsController)
|
71
|
+
# 2. The controller of the parent menu/menu bar
|
72
|
+
# 3. The request controller
|
73
|
+
#
|
74
|
+
# To override the default controller being linked to, you can explicitly
|
75
|
+
# define it like so:
|
76
|
+
# menu :contact, 'Contact Us', {}, :controller => 'about_us'
|
77
|
+
#
|
78
|
+
# Examples:
|
79
|
+
# menu :home do |home|
|
80
|
+
# menu :about, 'About Us', :action => 'about_us'
|
81
|
+
# #=> Links to {:controller => 'home', :action => 'about_us'}
|
82
|
+
# menu :who_we_are
|
83
|
+
# #=> Links to {:controller => 'home', :action => 'who_we_are'}
|
84
|
+
# menu :contact_us, 'Contact Us', :controller => 'contact', :action => 'index'
|
85
|
+
# #=> Links to {:controller => 'contact', :action => 'index'}
|
86
|
+
# menu :search
|
87
|
+
# #=> Links to {:controller => 'search'}
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
# You can also link to an explicit URL like so:
|
91
|
+
# menu :search, 'http://www.google.com'
|
92
|
+
#
|
93
|
+
# == Turning off links
|
94
|
+
#
|
95
|
+
# If you don't want a menu to link to a URL, you can turn off linking like so:
|
96
|
+
# menu :contact_us, 'Contact Us', {}, :auto_link => false
|
97
|
+
#
|
98
|
+
# == Defining content and html attributes
|
99
|
+
#
|
100
|
+
# By default, the content within a menu will be set as a humanized
|
101
|
+
# version of the menu's id. Examples of menus which customize the
|
102
|
+
# content and/or html attributes are below:
|
103
|
+
#
|
104
|
+
# menu :contact
|
105
|
+
# #=> <li id="contact"><a href="/contact">Contact</a></li>
|
106
|
+
# menu :contact, 'Contact Us'
|
107
|
+
# #=> <li id="contact"><a href="/contact">Contact Us</a></li>
|
108
|
+
# menu :contact, :class => 'pretty'
|
109
|
+
# #=> <li id="contact" class="pretty"><a href="/contact">Contact</a></li>
|
110
|
+
# menu :contact, 'Contact Us', :class => 'pretty'
|
111
|
+
# #=> <li id="contact" class="pretty"><a href="/contact">Contact Us</a></li>
|
112
|
+
#
|
113
|
+
# == Submenus
|
114
|
+
#
|
115
|
+
# Menus can also have their own submenus by passing in a block. You can
|
116
|
+
# create submenus in the same manner that the main menus are created.
|
117
|
+
# For example,
|
118
|
+
#
|
119
|
+
# menu :about do |about|
|
120
|
+
# about.menu :who_we_are
|
121
|
+
# about.menu :contact_us
|
122
|
+
# end
|
123
|
+
def menu(id, *args, &block)
|
124
|
+
menu = Menu.new(id, @request_controller, @parent_menu, *args, &block)
|
125
|
+
@menus << menu
|
126
|
+
|
127
|
+
menu
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
def tag_name
|
132
|
+
'ul'
|
133
|
+
end
|
134
|
+
|
135
|
+
def content
|
136
|
+
@menus.inject('') do |html, menu|
|
137
|
+
html << menu.html(@menus.last == menu)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
142
|
end
|
data/lib/menu_helper.rb
CHANGED
@@ -2,77 +2,76 @@ require 'menu_helper/html_element'
|
|
2
2
|
require 'menu_helper/menu_bar'
|
3
3
|
require 'menu_helper/menu'
|
4
4
|
|
5
|
-
module PluginAWeek #:nodoc:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
helper PluginAWeek::Helpers::MenuHelper
|
5
|
+
module PluginAWeek #:nodoc:
|
6
|
+
# Provides a builder for generating html menubars. The structure of the
|
7
|
+
# menubars/menus is based on lists and should be styled using css.
|
8
|
+
module MenuHelper
|
9
|
+
# Creates a new 1st-level menu bar. The first parameter is the menubar's
|
10
|
+
# configuration options. The second parameter is the menubar's html
|
11
|
+
# options. Both of these parameters are optional.
|
12
|
+
#
|
13
|
+
# Configuration options:
|
14
|
+
# * +auto_set_id+ - Whether or not to automatically add ids to each menu.
|
15
|
+
#
|
16
|
+
# == Examples
|
17
|
+
#
|
18
|
+
# menu_bar {}, :id => 'menus', :class => 'pretty' do |main|
|
19
|
+
# main.menu :home
|
20
|
+
# main.menu :about_us do |about_us|
|
21
|
+
# about_us.who_we_are
|
22
|
+
# about_us.what_we_do
|
23
|
+
# about_us.where_we_are
|
24
|
+
# about_us.contact, 'Contact', 'mailto:contact@us.com'
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
# #=>
|
28
|
+
# <ul id="menus" class="pretty">
|
29
|
+
# <li id="about_us">About Us
|
30
|
+
# <ul id="about_us_menubar">
|
31
|
+
# <li id="who_we_are"><a href="/about_us/who_we_are">Who We Are</a></li>
|
32
|
+
# <li id="what_we_do"><a href="/about_us/what_we_do">What We Do</a></li>
|
33
|
+
# <li id="contact"><a href="mailto:contact@us.com">Contact</a></li>
|
34
|
+
# </ul>
|
35
|
+
# </li>
|
36
|
+
# </ul>
|
37
|
+
#
|
38
|
+
# == Menu Selection
|
39
|
+
#
|
40
|
+
# The currently selected menu is based on the current page that the user
|
41
|
+
# is currently on. If the url that the menu links to is the same as the
|
42
|
+
# current page, then that menu will be selected. This menu uses
|
43
|
+
# ActionView::Helpers::UrlHelper#current_page? to determine whether or not
|
44
|
+
# it is the currently selected menu.
|
45
|
+
#
|
46
|
+
# If the menu that is selected is nested within another menu, then those
|
47
|
+
# menus will be selected as well.
|
48
|
+
#
|
49
|
+
# A "selected" menu is indicated by an additional class html attribute
|
50
|
+
# that is added to the list item.
|
51
|
+
#
|
52
|
+
# For example, if a submenu is selected, the html generated from the
|
53
|
+
# above full example would look like so:
|
54
|
+
#
|
55
|
+
# <ul id="menus" class="pretty">
|
56
|
+
# <li id="about_us" class="selected">About Us
|
57
|
+
# <ul id="about_us_menubar">
|
58
|
+
# <li id="who_we_are" class="selected"><a href="/about_us/who_we_are">Who We Are</a></li>
|
59
|
+
# <li id="what_we_do"><a href="/about_us/what_we_do">What We Do</a></li>
|
60
|
+
# <li id="contact"><a href="mailto:contact@us.com">Contact</a></li>
|
61
|
+
# </ul>
|
62
|
+
# </li>
|
63
|
+
# </ul>
|
64
|
+
#
|
65
|
+
# == Menu Creation
|
66
|
+
#
|
67
|
+
# For more information about how menus are created, see the documentation
|
68
|
+
# for MenuBar#menu.
|
69
|
+
def menu_bar(*args, &block)
|
70
|
+
MenuBar.new(@controller, *args, &block).html
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
ActionController::Base.class_eval do
|
76
|
+
helper PluginAWeek::MenuHelper
|
78
77
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# Logfile created on Sun May 11 12:49:09 -0400 2008
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
$:.unshift("#{File.dirname(__FILE__)}/../../../ruby/hash/set_or_append/lib")
|
2
|
-
|
3
1
|
# Load the plugin testing framework
|
4
|
-
$:.unshift("#{File.dirname(__FILE__)}
|
2
|
+
$:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
|
5
3
|
require 'rubygems'
|
6
4
|
require 'plugin_test_helper'
|
7
5
|
|
@@ -1,50 +1,74 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class HtmlElementByDefaultTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@element = PluginAWeek::MenuHelper::HtmlElement.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_should_generate_an_empty_tag
|
9
|
+
assert_equal '<></>', @element.html
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_not_have_any_html_options
|
13
|
+
element = PluginAWeek::MenuHelper::HtmlElement.new
|
14
|
+
assert_nil element[:class]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class HtmlElementTest < Test::Unit::TestCase
|
19
|
+
class DivElement < PluginAWeek::MenuHelper::HtmlElement
|
20
|
+
def tag_name
|
21
|
+
'div'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_set_html_options_on_initialization
|
26
|
+
element = PluginAWeek::MenuHelper::HtmlElement.new(:class => 'fancy')
|
27
|
+
assert_equal 'fancy', element[:class]
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_symbolize_html_options
|
31
|
+
element = PluginAWeek::MenuHelper::HtmlElement.new('class' => 'fancy')
|
32
|
+
assert_equal 'fancy', element[:class]
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_generate_entire_element_if_content_and_tag_name_specified
|
36
|
+
element = DivElement.new
|
37
|
+
element.instance_eval do
|
38
|
+
def content
|
39
|
+
'hello world'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
assert_equal '<div>hello world</div>', element.html
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_include_html_options_in_element_tag
|
47
|
+
element = DivElement.new
|
48
|
+
element[:class] = 'fancy'
|
49
|
+
|
50
|
+
assert_equal '<div class="fancy"></div>', element.html
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_save_changes_in_html_options
|
54
|
+
element = PluginAWeek::MenuHelper::HtmlElement.new
|
55
|
+
element[:float] = 'left'
|
56
|
+
assert_equal 'left', element[:float]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class HtmlElementWithNoContentTest < Test::Unit::TestCase
|
61
|
+
class DivElement < PluginAWeek::MenuHelper::HtmlElement
|
62
|
+
def tag_name
|
63
|
+
'div'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def setup
|
68
|
+
@element = DivElement.new
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_should_generate_empty_tags
|
72
|
+
assert_equal '<div></div>', @element.html
|
73
|
+
end
|
50
74
|
end
|