Class: MenuHelper::MenuBar

Inherits:
HtmlElement show all
Defined in:
lib/menu_helper/menu_bar.rb

Overview

Represents a group of menus. A menu bar can either be the main menu bar or a menu bar nested within a menu.

Constant Summary collapse

'ui-menubar'
@@selected_class =
'ui-state-active ui-menubar-selected'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_controller, options = {}, html_options = {}) {|_self| ... } ⇒ MenuBar

:nodoc:

Yields:

  • (_self)

Yield Parameters:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/menu_helper/menu_bar.rb', line 22

def initialize(request_controller, options = {}, html_options = {}) #:nodoc:
  super(html_options)
  
  # Set up default options
  options.assert_valid_keys(:parent_menu, :auto_set_ids, :attach_active_submenus, :content_for)
  options.reverse_merge!(:auto_set_ids => true, :attach_active_submenus => true, :content_for => 'menu_bar')
  @options = options
  
  # Set context of the menu bar (the request and any parent)
  @request_controller = request_controller
  
  # No menus initially associated
  @menus = []
  
  # Set up default html options
  self[:class] = "#{self[:class]} #{menu_bar_class} #{menu_bar_class}-#{level}".strip
  
  yield self if block_given?
end

Instance Attribute Details

The menus within this menu bar



17
18
19
# File 'lib/menu_helper/menu_bar.rb', line 17

def menus
  @menus
end

#optionsObject (readonly)

The configuration options for this menu bar



20
21
22
# File 'lib/menu_helper/menu_bar.rb', line 20

def options
  @options
end

#request_controllerObject (readonly)

The request context in which this menu bar is being rendered



14
15
16
# File 'lib/menu_helper/menu_bar.rb', line 14

def request_controller
  @request_controller
end

Instance Method Details

#attach_active_submenus?Boolean

Should menu bars in sub-menus be attached to this menu bar?

Returns:

  • (Boolean)


71
72
73
# File 'lib/menu_helper/menu_bar.rb', line 71

def attach_active_submenus?
  @options[:attach_active_submenus]
end

#auto_set_ids?Boolean

Shoulds elements have default ids automatically applied to them?

Returns:

  • (Boolean)


66
67
68
# File 'lib/menu_helper/menu_bar.rb', line 66

def auto_set_ids?
  @options[:auto_set_ids]
end

#content_for_variableObject

The instance variable to use when rendering the current active sub-menu bar



77
78
79
# File 'lib/menu_helper/menu_bar.rb', line 77

def content_for_variable
  "@content_for_#{@options[:content_for]}_level_#{level}"
end

#htmlObject

Builds the actual html of the menu bar



185
186
187
188
189
190
# File 'lib/menu_helper/menu_bar.rb', line 185

def html
  html_options = @html_options.dup
  html_options[:class] = "#{html_options[:class]} #{selected_class}".strip if selected?
  
  (tag_name, content, html_options)
end

#levelObject

Gets the nesting level of this menu bar. The top-level menu bar will always have a nesting level of 1.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/menu_helper/menu_bar.rb', line 44

def level
  @level ||= begin
    level = 1
    
    # Keep walking up the tree, until first-level menus are encountered
    menu = parent_menu
    while menu
      level += 1
      menu = menu.parent_menu
    end
    
    level
  end
end

Creates a new menu in this bar with the given id. The content within the menu is, by default, set to a humanized version of the id.

URLs with routes

If you have named routes set up in the application, the menu attempts to automatically figure out what URL you’re trying to link to. It does this by looking at the id of the menu and the id of its parent.

For example, a menu bar with the id ‘home’ and a menu with the id ‘contact_us’ will attempt to look for the following named routes as the URL to link to (in order of priority):

  1. home_contact_us_url

  2. contact_us_url

Example routes.rb:

ActionController::Routing::Routes.draw do |map|
  map.with_options(:controller => 'home') do |home|
    home.home '', :action => 'index'
    home.home_search 'search', :action => 'search'
  end

  map.with_options(:controller => 'about_us') do |about_us|
    about_us.about_us 'about_us', :action => 'index'
  end
end

Example menu bar:

menu_bar :home do |home|
  home.menu :about_us   # => Links to about_us_url
  home.menu :search     # => Links to home_search_url
end

URLs with url_for

If neither of these named routes are being used, the url will be based on the options passed into the menu. The url_options takes the same values as url_for. By default, the name of the controller will be guessed in the following order:

  1. The id of the menu (‘contact_us’ => ContactUsController)

  2. The controller of the parent menu/menu bar

  3. The request controller

To override the default controller being linked to, you can explicitly define it like so:

menu :contact, 'Contact Us', :controller => 'about_us'

Examples:

menu :home do |home|
  home.menu :about, 'About Us', :action => 'about_us'                       # => Links to {:controller => 'home', :action => 'about_us'}
  home.menu :who_we_are                                                     # => Links to {:controller => 'home', :action => 'who_we_are'}
  home.menu :contact_us, :controller => 'contact', :action => 'index'  # => Links to {:controller => 'contact', :action => 'index'}
  home.menu :search                                                         # => Links to {:controller => 'search'}
end

You can also link to an explicit URL like so:

home.menu :search, 'http://www.google.com'

If you don’t want a menu to link to a URL, you can turn off linking like so:

home.menu :contact_us, {}, :link => false

Defining content and html attributes

By default, the content within a menu will be set as a humanized version of the menu’s id. Examples of menus which customize the content and/or html attributes are below:

home.menu :contact                                          # => <li id="contact" class="ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Contact</a></li>
home.menu :contact, 'Contact Us'                            # => <li id="contact" class="ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Contact Us</a></li>
home.menu :contact, {}, :class => 'pretty'                  # => <li id="contact" class="pretty ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Contact</a></li>
home.menu :contact, 'Get in touch!', {}, :class => 'pretty' # => <li id="contact" class="pretty ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Get in touch!</a></li>

Sub-menus

Menus can also have their own sub-menus by passing in a block. You can create sub-menus in the same manner that the main menus are created. For example,

home.menu :about do |about|
  about.menu :who_we_are
  about.menu :contact_us
end


177
178
179
180
181
182
# File 'lib/menu_helper/menu_bar.rb', line 177

def menu(id, content = nil, url_options = {}, html_options = {}, &block)
  menu = Menu.new(self, id, content, url_options, html_options, &block)
  @menus << menu
  
  menu
end

#parent_menuObject

The menu in which this menu bar is being displayed. This will be nil if this is the main menu bar.



61
62
63
# File 'lib/menu_helper/menu_bar.rb', line 61

def parent_menu
  @options[:parent_menu]
end

#selected?Boolean

Is this menu bar selected? A menu bar is considered selected if it has a parent menu and that parent menu is selected.

Returns:

  • (Boolean)


83
84
85
# File 'lib/menu_helper/menu_bar.rb', line 83

def selected?
  parent_menu && menus.any? {|menu| menu.selected?}
end