menu_helper 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,47 +1,107 @@
1
1
  module PluginAWeek #:nodoc:
2
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.
3
+ # Represents a group of menus. A menu bar can either be the main menu bar
4
+ # or a menu bar nested within a menu.
5
5
  class MenuBar < HtmlElement
6
+ # The css class to apply for all menu bars
7
+ cattr_accessor :menu_bar_class
8
+ @@menu_bar_class = 'menubar'
9
+
10
+ # The css class to apply when a sub-menu bar is selected
11
+ cattr_accessor :selected_class
12
+ @@selected_class = 'menubar-selected'
13
+
14
+ # The request context in which this menu bar is being rendered
15
+ attr_reader :request_controller
16
+
6
17
  # The menus within this menu bar
7
18
  attr_reader :menus
8
19
 
9
- def initialize(request_controller, options = {}, html_options = {}, parent_menu = nil) #:nodoc:
20
+ # The configuration options for this menu bar
21
+ attr_reader :options
22
+
23
+ def initialize(request_controller, options = {}, html_options = {}) #:nodoc:
10
24
  super(html_options)
11
25
 
12
- options.assert_valid_keys(:auto_set_ids)
13
- options.reverse_merge!(:auto_set_ids => true)
26
+ # Set up default options
27
+ options.assert_valid_keys(:parent_menu, :auto_set_ids, :attach_active_submenus, :content_for)
28
+ options.reverse_merge!(:auto_set_ids => true, :attach_active_submenus => true, :content_for => 'menu_bar')
14
29
  @options = options
30
+
31
+ # Set context of the menu bar (the request and any parent)
15
32
  @request_controller = request_controller
16
- @parent_menu = parent_menu
17
33
 
34
+ # No menus initially associated
18
35
  @menus = []
19
36
 
20
- if @parent_menu
21
- self[:id] ||= "#{@parent_menu[:id]}_menubar"
22
- else
23
- self[:id] ||= 'menubar'
24
- end
37
+ # Set up default html options
38
+ self[:class] = "#{self[:class]} #{menu_bar_class} #{menu_bar_class}-#{level}".strip
25
39
 
26
40
  yield self if block_given?
27
41
  end
28
42
 
43
+ # Gets the nesting level of this menu bar. The top-level menu bar will
44
+ # always have a nesting level of 1.
45
+ def level
46
+ @level ||= begin
47
+ level = 1
48
+
49
+ # Keep walking up the tree, until first-level menus are encountered
50
+ menu = parent_menu
51
+ while menu
52
+ level += 1
53
+ menu = menu.parent_menu
54
+ end
55
+
56
+ level
57
+ end
58
+ end
59
+
60
+ # The menu in which this menu bar is being displayed. This will be nil if
61
+ # this is the main menu bar.
62
+ def parent_menu
63
+ @options[:parent_menu]
64
+ end
65
+
66
+ # Shoulds elements have default ids automatically applied to them?
67
+ def auto_set_ids?
68
+ @options[:auto_set_ids]
69
+ end
70
+
71
+ # Should menu bars in sub-menus be attached to this menu bar?
72
+ def attach_active_submenus?
73
+ @options[:attach_active_submenus]
74
+ end
75
+
76
+ # The instance variable to use when rendering the current active sub-menu
77
+ # bar
78
+ def content_for_variable
79
+ "@content_for_#{@options[:content_for]}_level_#{level}"
80
+ end
81
+
82
+ # Is this menu bar selected? A menu bar is considered selected if it has
83
+ # a parent menu and that parent menu is selected.
84
+ def selected?
85
+ parent_menu && menus.any? {|menu| menu.selected?}
86
+ end
87
+
29
88
  # Creates a new menu in this bar with the given id. The content
30
89
  # within the menu is, by default, set to a humanized version of the id.
31
90
  #
32
91
  # == URLs with routes
33
92
  #
34
- # If you have named routes setup in the application, the menu attempts
93
+ # If you have named routes set up in the application, the menu attempts
35
94
  # to automatically figure out what URL you're trying to link to. It
36
95
  # does this by looking at the id of the menu and the id of its parent.
37
96
  #
38
- # For example, a menu_bar with the id 'home' and a menu with the id
97
+ # For example, a menu bar with the id 'home' and a menu with the id
39
98
  # 'contact_us' will attempt to look for the following named routes as
40
99
  # the URL to link to (in order of priority):
41
- # 1. contact_us_url
42
- # 2. home_contact_us_url
100
+ # 1. home_contact_us_url
101
+ # 2. contact_us_url
43
102
  #
44
103
  # Example routes.rb:
104
+ #
45
105
  # ActionController::Routing::Routes.draw do |map|
46
106
  # map.with_options(:controller => 'home') do |home|
47
107
  # home.home '', :action => 'index'
@@ -53,12 +113,11 @@ module PluginAWeek #:nodoc:
53
113
  # end
54
114
  # end
55
115
  #
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
116
+ # Example menu bar:
117
+ #
118
+ # menu_bar :home do |home|
119
+ # home.menu :about_us # => Links to about_us_url
120
+ # home.menu :search # => Links to home_search_url
62
121
  # end
63
122
  #
64
123
  # == URLs with url_for
@@ -73,27 +132,27 @@ module PluginAWeek #:nodoc:
73
132
  #
74
133
  # To override the default controller being linked to, you can explicitly
75
134
  # define it like so:
76
- # menu :contact, 'Contact Us', {}, :controller => 'about_us'
135
+ # menu :contact, 'Contact Us', :controller => 'about_us'
77
136
  #
78
137
  # Examples:
138
+ #
79
139
  # 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'}
140
+ # home.menu :about, 'About Us', :action => 'about_us' # => Links to {:controller => 'home', :action => 'about_us'}
141
+ # home.menu :who_we_are # => Links to {:controller => 'home', :action => 'who_we_are'}
142
+ # home.menu :contact_us, :controller => 'contact', :action => 'index' # => Links to {:controller => 'contact', :action => 'index'}
143
+ # home.menu :search # => Links to {:controller => 'search'}
88
144
  # end
89
145
  #
90
146
  # You can also link to an explicit URL like so:
91
- # menu :search, 'http://www.google.com'
147
+ #
148
+ # home.menu :search, 'http://www.google.com'
92
149
  #
93
150
  # == Turning off links
94
151
  #
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
152
+ # If you don't want a menu to link to a URL, you can turn off linking like
153
+ # so:
154
+ #
155
+ # home.menu :contact_us, {}, :link => false
97
156
  #
98
157
  # == Defining content and html attributes
99
158
  #
@@ -101,41 +160,45 @@ module PluginAWeek #:nodoc:
101
160
  # version of the menu's id. Examples of menus which customize the
102
161
  # content and/or html attributes are below:
103
162
  #
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>
163
+ # home.menu :contact # => <li id="contact"><a href="/contact">Contact</a></li>
164
+ # home.menu :contact, 'Contact Us' # => <li id="contact"><a href="/contact">Contact Us</a></li>
165
+ # home.menu :contact, {}, :class => 'pretty' # => <li id="contact" class="pretty"><a href="/contact">Contact</a></li>
166
+ # home.menu :contact, 'Get in touch!', {}, :class => 'pretty' # => <li id="contact" class="pretty"><a href="/contact">Contact Us</a></li>
112
167
  #
113
- # == Submenus
168
+ # == Sub-menus
114
169
  #
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.
170
+ # Menus can also have their own sub-menus by passing in a block. You can
171
+ # create sub-menus in the same manner that the main menus are created.
117
172
  # For example,
118
173
  #
119
- # menu :about do |about|
174
+ # home.menu :about do |about|
120
175
  # about.menu :who_we_are
121
176
  # about.menu :contact_us
122
177
  # end
123
- def menu(id, *args, &block)
124
- menu = Menu.new(id, @request_controller, @parent_menu, *args, &block)
178
+ def menu(id, content = nil, url_options = {}, html_options = {}, &block)
179
+ menu = Menu.new(self, id, content, url_options, html_options, &block)
125
180
  @menus << menu
126
181
 
127
182
  menu
128
183
  end
129
184
 
185
+ # Builds the actual html of the menu bar
186
+ def html
187
+ html_options = @html_options.dup
188
+ html_options[:class] = "#{html_options[:class]} #{selected_class}".strip if selected?
189
+
190
+ content_tag(tag_name, content, html_options)
191
+ end
192
+
130
193
  private
194
+ # Unordered list
131
195
  def tag_name
132
196
  'ul'
133
197
  end
134
198
 
199
+ # Generate the html for the menu bar
135
200
  def content
136
- @menus.inject('') do |html, menu|
137
- html << menu.html(@menus.last == menu)
138
- end
201
+ @menus.inject('') {|html, menu| html << menu.html(@menus.last == menu)}
139
202
  end
140
203
  end
141
204
  end
@@ -1,15 +1,3 @@
1
1
  ActionController::Routing::Routes.draw do |map|
2
- # Home
3
- map.with_options(:controller => 'home') do |home|
4
- home.home '', :action => 'index'
5
- home.home_search 'search_stuff', :action => 'search'
6
- end
7
-
8
- # Contact
9
- map.with_options(:controller => 'contact') do |contact|
10
- contact.contact 'contact', :action => 'index'
11
- end
12
-
13
- map.connect ':controller/:action/:id.:format'
14
2
  map.connect ':controller/:action/:id'
15
3
  end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ class MenuHelperTest < ActionView::TestCase
4
+ tests PluginAWeek::MenuHelper
5
+
6
+ def test_should_build_menu_bar
7
+ menu_bar_html = menu_bar({}, :class => 'pretty') do |main|
8
+ main.menu :home do |home|
9
+ home.menu :browse
10
+ home.menu :search
11
+ end
12
+ main.menu :contact, 'Contact Us'
13
+ main.menu :about_us
14
+ end
15
+
16
+ expected = <<-eos
17
+ <ul class="pretty menubar menubar-1">
18
+ <li><a href="http://test.host/home"><span>Home</span></a>
19
+ <ul class="menubar menubar-2">
20
+ <li><a href="http://test.host/home/browse"><span>Browse</span></a></li>
21
+ <li class="menubar-last"><a href="http://test.host/home/search"><span>Search</span></a></li>
22
+ </ul>
23
+ </li>
24
+ <li class="menubar-selected"><a href="http://test.host/contact"><span>Contact Us</span></a></li>
25
+ <li class="menubar-last"><a href="http://test.host/about_us"><span>About Us</span></a></li>
26
+ </ul>
27
+ eos
28
+ assert_equal expected.gsub(/\n\s*/, ''), menu_bar_html
29
+ end
30
+ end
data/test/test_helper.rb CHANGED
@@ -3,14 +3,19 @@ $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
3
3
  require 'rubygems'
4
4
  require 'plugin_test_helper'
5
5
 
6
- class Test::Unit::TestCase
6
+ Test::Unit::TestCase.class_eval do
7
7
  def setup
8
8
  request = ActionController::TestRequest.new
9
9
  request.request_uri = '/contact'
10
10
  request.path_parameters = {:action => 'index', :controller => 'contact'}
11
- @controller = HomeController.new
11
+
12
+ @controller = ContactController.new
12
13
  @controller.request = request
13
14
  @controller.instance_eval {@_params = request.path_parameters}
14
15
  @controller.send(:initialize_current_url)
15
16
  end
17
+
18
+ def teardown
19
+ ActionController::Routing::Routes.load!
20
+ end
16
21
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class HtmlElementByDefaultTest < Test::Unit::TestCase
4
4
  def setup
@@ -1,72 +1,315 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
2
 
3
3
  class MenuBarByDefaultTest < Test::Unit::TestCase
4
4
  def setup
5
5
  super
6
+
6
7
  @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
7
8
  end
8
9
 
9
- def test_should_have_no_menus_by_default
10
+ def test_should_have_no_menus
10
11
  assert_equal [], @menu_bar.menus
11
12
  end
12
13
 
13
- def test_should_set_default_id_if_no_parent_specified
14
- assert_equal 'menubar', @menu_bar[:id]
14
+ def test_should_have_a_request_controller
15
+ assert_equal @controller, @menu_bar.request_controller
16
+ end
17
+
18
+ def test_should_have_options
19
+ expected = {
20
+ :auto_set_ids => true,
21
+ :attach_active_submenus => true,
22
+ :content_for => 'menu_bar'
23
+ }
24
+ assert_equal expected, @menu_bar.options
25
+ end
26
+
27
+ def test_should_have_a_level
28
+ assert_equal 1, @menu_bar.level
29
+ end
30
+
31
+ def test_should_not_have_a_parent_menu
32
+ assert_nil @menu_bar.parent_menu
33
+ end
34
+
35
+ def test_should_auto_set_ids
36
+ assert @menu_bar.auto_set_ids?
37
+ end
38
+
39
+ def test_should_attach_active_submenus
40
+ assert @menu_bar.attach_active_submenus?
41
+ end
42
+
43
+ def test_should_have_a_content_for_variable
44
+ assert_equal '@content_for_menu_bar_level_1', @menu_bar.content_for_variable
45
+ end
46
+
47
+ def test_should_not_be_selected
48
+ assert !@menu_bar.selected?
49
+ end
50
+
51
+ def test_should_set_css_classes
52
+ assert_equal 'menubar menubar-1', @menu_bar[:class]
53
+ end
54
+
55
+ def test_should_allow_css_classes_to_be_customized
56
+ @original_menu_bar_class = PluginAWeek::MenuHelper::MenuBar.menu_bar_class
57
+ PluginAWeek::MenuHelper::MenuBar.menu_bar_class = 'menus'
58
+
59
+ menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
60
+ assert_equal 'menus menus-1', menu_bar[:class]
61
+ ensure
62
+ PluginAWeek::MenuHelper::MenuBar.menu_bar_class = @original_menu_bar_class
63
+ end
64
+
65
+ def test_should_not_set_id
66
+ assert_nil @menu_bar[:id]
15
67
  end
16
68
  end
17
69
 
18
70
  class MenuBarTest < Test::Unit::TestCase
71
+ def setup
72
+ super
73
+
74
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
75
+ end
76
+
19
77
  def test_should_raise_exception_if_invalid_option_specified
20
- assert_raise(ArgumentError) {create_menu_bar(:invalid => true)}
78
+ assert_raise(ArgumentError) {PluginAWeek::MenuHelper::MenuBar.new(@controller, :invalid => true)}
21
79
  end
22
80
 
23
81
  def test_should_accept_block
24
82
  in_block = false
25
- menu_bar = create_menu_bar do |main_menu|
83
+ menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller) do |menu_bar|
26
84
  in_block = true
27
85
  end
28
86
 
29
87
  assert in_block
30
88
  end
31
89
 
32
- def test_should_create_menus
33
- menu_bar = create_menu_bar do |main|
90
+ def test_should_not_modify_html_options_after_building_hml
91
+ @menu_bar.html
92
+ assert_equal 'menubar menubar-1', @menu_bar[:class]
93
+ end
94
+
95
+ def test_should_allow_menus_to_be_created
96
+ @menu = @menu_bar.menu(:home) do |menu_bar|
97
+ @sub_menu_bar = menu_bar
98
+ end
99
+
100
+ assert_equal @menu, @sub_menu_bar.parent_menu
101
+ assert_equal [@menu], @menu_bar.menus
102
+ end
103
+ end
104
+
105
+ class MenuBarWithoutMenusTest < Test::Unit::TestCase
106
+ def setup
107
+ super
108
+
109
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
110
+ end
111
+
112
+ def test_should_not_have_any_menus
113
+ assert @menu_bar.menus.empty?
114
+ end
115
+
116
+ def test_should_not_be_selected
117
+ assert !@menu_bar.selected?
118
+ end
119
+
120
+ def test_should_not_render_menus
121
+ assert_equal '<ul class="menubar menubar-1"></ul>', @menu_bar.html
122
+ end
123
+ end
124
+
125
+ class MenuBarWithCustomHtmlOptionsTest < Test::Unit::TestCase
126
+ def setup
127
+ super
128
+
129
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, {}, :id => 'menus', :class => 'pretty')
130
+ end
131
+
132
+ def test_should_render_with_custom_options
133
+ assert_equal '<ul class="pretty menubar menubar-1" id="menus"></ul>', @menu_bar.html
134
+ end
135
+ end
136
+
137
+ class MenuBarWithCustomContentForTest < Test::Unit::TestCase
138
+ def setup
139
+ super
140
+
141
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :content_for => 'menus')
142
+ end
143
+
144
+ def test_should_have_a_content_for_variable_based_on_options
145
+ assert_equal '@content_for_menus_level_1', @menu_bar.content_for_variable
146
+ end
147
+ end
148
+
149
+ class MenuBarWithMenusTest < Test::Unit::TestCase
150
+ def setup
151
+ super
152
+
153
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, {}, :id => 'menus') do |main|
34
154
  main.menu :home
35
- main.menu :contact
155
+ main.menu :about_us, 'About'
36
156
  end
157
+ end
158
+
159
+ def test_should_render_menus
160
+ expected = <<-eos
161
+ <ul class="menubar menubar-1" id="menus">
162
+ <li id="menus-home"><a href="http://test.host/home"><span>Home</span></a></li>
163
+ <li class="menubar-last" id="menus-about_us"><a href="http://test.host/about_us"><span>About</span></a></li>
164
+ </ul>
165
+ eos
166
+ assert_equal expected.gsub(/\n\s*/, ''), @menu_bar.html
167
+ end
168
+ end
169
+
170
+ class MenuBarWithSelectedMenuTest < Test::Unit::TestCase
171
+ def setup
172
+ super
37
173
 
38
- assert_equal 2, menu_bar.menus.size
174
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller) do |main|
175
+ main.menu :contact
176
+ end
177
+ end
178
+
179
+ def test_should_not_be_selected
180
+ assert !@menu_bar.selected?
39
181
  end
40
182
 
41
- def test_should_build_menu_bar_and_menus
42
- menu_bar = create_menu_bar do |main|
183
+ def test_should_not_include_selected_css_class_in_html
184
+ expected = <<-eos
185
+ <ul class="menubar menubar-1">
186
+ <li class="menubar-selected menubar-last"><a href="http://test.host/contact"><span>Contact</span></a></li>
187
+ </ul>
188
+ eos
189
+ assert_equal expected.gsub(/\n\s*/, ''), @menu_bar.html
190
+ end
191
+ end
192
+
193
+ class MenuBarWithoutAutoIdSettingTest < Test::Unit::TestCase
194
+ def setup
195
+ super
196
+
197
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, {:auto_set_ids => false}, :id => 'menus') do |main|
43
198
  main.menu :home
44
- main.menu :contact, 'Contact Us'
45
199
  end
200
+ end
201
+
202
+ def test_should_not_set_default_id_for_menus
203
+ expected = <<-eos
204
+ <ul class="menubar menubar-1" id="menus">
205
+ <li class="menubar-last"><a href="http://test.host/home"><span>Home</span></a></li>
206
+ </ul>
207
+ eos
208
+ assert_equal expected.gsub(/\n\s*/, ''), @menu_bar.html
209
+ end
210
+ end
211
+
212
+ class MenuBarUnselectedWithDetachedActiveSubmenus < Test::Unit::TestCase
213
+ def setup
214
+ super
46
215
 
216
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :attach_active_submenus => false) do |main|
217
+ main.menu :home do |home|
218
+ home.menu :about_us
219
+ end
220
+ end
221
+ end
222
+
223
+ def test_should_render_sub_menu_bars
47
224
  expected = <<-eos
48
- <ul id="menubar">
49
- <li id="home"><a href="http://test.host/">Home</a></li>
50
- <li class="selected last" id="contact"><a href="http://test.host/contact">Contact Us</a></li>
225
+ <ul class="menubar menubar-1">
226
+ <li class="menubar-last"><a href="http://test.host/home"><span>Home</span></a>
227
+ <ul class="menubar menubar-2">
228
+ <li class="menubar-last"><a href="http://test.host/about_us"><span>About Us</span></a></li>
229
+ </ul>
230
+ </li>
51
231
  </ul>
52
232
  eos
53
- assert_equal expected.gsub(/\n\s*/, ''), menu_bar.html
233
+ assert_equal expected.gsub(/\n\s*/, ''), @menu_bar.html
54
234
  end
55
235
 
56
- private
57
- def create_menu_bar(*args, &block)
58
- PluginAWeek::MenuHelper::MenuBar.new(@controller, *args, &block)
236
+ def test_should_not_store_a_menu_bar_in_content_variable
237
+ assert !@controller.instance_variable_defined?('@content_for_menu_bar_level_2')
238
+ end
239
+ end
240
+
241
+ class MenuBarSelectedWithDetachedActiveSubmenus < Test::Unit::TestCase
242
+ def setup
243
+ super
244
+
245
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :attach_active_submenus => false) do |main|
246
+ main.menu :contact do |contact|
247
+ contact.menu :investors
248
+ end
59
249
  end
250
+ end
251
+
252
+ def test_should_not_render_sub_menu_bars
253
+ expected = <<-eos
254
+ <ul class="menubar menubar-1">
255
+ <li class="menubar-selected menubar-last"><a href="http://test.host/contact"><span>Contact</span></a></li>
256
+ </ul>
257
+ eos
258
+ assert_equal expected.gsub(/\n\s*/, ''), @menu_bar.html
259
+ end
260
+
261
+ def test_should_store_a_menu_bar_in_content_variable
262
+ # Generate the html to store it in the variable
263
+ @menu_bar.html
264
+
265
+ expected = <<-eos
266
+ <ul class="menubar menubar-2">
267
+ <li class="menubar-last"><a href="http://test.host/contact/investors"><span>Investors</span></a></li>
268
+ </ul>
269
+ eos
270
+ assert_equal expected.gsub(/\n\s*/, ''), @controller.instance_variable_get('@content_for_menu_bar_level_2')
271
+ end
60
272
  end
61
273
 
62
- class MenuBarWithParentTest < Test::Unit::TestCase
274
+ class MenuBarWithParentMenuTest < Test::Unit::TestCase
63
275
  def setup
64
276
  super
65
- parent = PluginAWeek::MenuHelper::Menu.new(:home, @controller)
66
- @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, {}, {}, parent)
277
+
278
+ @parent_menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
279
+ @parent_menu = PluginAWeek::MenuHelper::Menu.new(@parent_menu_bar, :home)
280
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :parent_menu => @parent_menu)
281
+ end
282
+
283
+ def test_should_have_a_parent_menu
284
+ assert_equal @parent_menu, @menu_bar.parent_menu
67
285
  end
68
286
 
69
- def test_should_set_default_id_based_on_parent
70
- assert_equal 'home_menubar', @menu_bar[:id]
287
+ def test_should_have_level
288
+ assert_equal 2, @menu_bar.level
289
+ end
290
+ end
291
+
292
+ class MenuBarWithParentMenuAndSelectedMenuTest < Test::Unit::TestCase
293
+ def setup
294
+ super
295
+
296
+ @parent_menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller)
297
+ @parent_menu = PluginAWeek::MenuHelper::Menu.new(@parent_menu_bar, :about_us)
298
+ @menu_bar = PluginAWeek::MenuHelper::MenuBar.new(@controller, :parent_menu => @parent_menu) do |about_us|
299
+ about_us.menu :contact
300
+ end
301
+ end
302
+
303
+ def test_should_be_selected
304
+ assert @menu_bar.selected?
305
+ end
306
+
307
+ def test_should_include_selected_css_class_in_html
308
+ expected = <<-eos
309
+ <ul class="menubar menubar-2 menubar-selected">
310
+ <li class="menubar-selected menubar-last"><a href="http://test.host/contact"><span>Contact</span></a></li>
311
+ </ul>
312
+ eos
313
+ assert_equal expected.gsub(/\n\s*/, ''), @menu_bar.html
71
314
  end
72
315
  end