Class: MenuHelper::MenuBar
- Inherits:
-
HtmlElement
- Object
- HtmlElement
- MenuHelper::MenuBar
- 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
-
#menus ⇒ Object
readonly
The menus within this menu bar.
-
#options ⇒ Object
readonly
The configuration options for this menu bar.
-
#request_controller ⇒ Object
readonly
The request context in which this menu bar is being rendered.
Instance Method Summary collapse
-
#attach_active_submenus? ⇒ Boolean
Should menu bars in sub-menus be attached to this menu bar?.
-
#auto_set_ids? ⇒ Boolean
Shoulds elements have default ids automatically applied to them?.
-
#content_for_variable ⇒ Object
The instance variable to use when rendering the current active sub-menu bar.
-
#html ⇒ Object
Builds the actual html of the menu bar.
-
#initialize(request_controller, options = {}, html_options = {}) {|_self| ... } ⇒ MenuBar
constructor
:nodoc:.
-
#level ⇒ Object
Gets the nesting level of this menu bar.
-
#menu(id, content = nil, url_options = {}, html_options = {}, &block) ⇒ Object
Creates a new menu in this bar with the given id.
-
#parent_menu ⇒ Object
The menu in which this menu bar is being displayed.
-
#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.
Constructor Details
#initialize(request_controller, options = {}, html_options = {}) {|_self| ... } ⇒ MenuBar
:nodoc:
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, = {}, = {}) #:nodoc: super() # Set up default options .assert_valid_keys(:parent_menu, :auto_set_ids, :attach_active_submenus, :content_for) .reverse_merge!(:auto_set_ids => true, :attach_active_submenus => true, :content_for => 'menu_bar') @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]} #{} #{}-#{level}".strip yield self if block_given? end |
Instance Attribute Details
#menus ⇒ Object (readonly)
The menus within this menu bar
17 18 19 |
# File 'lib/menu_helper/menu_bar.rb', line 17 def @menus end |
#options ⇒ Object (readonly)
The configuration options for this menu bar
20 21 22 |
# File 'lib/menu_helper/menu_bar.rb', line 20 def @options end |
#request_controller ⇒ Object (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?
71 72 73 |
# File 'lib/menu_helper/menu_bar.rb', line 71 def @options[:attach_active_submenus] end |
#auto_set_ids? ⇒ Boolean
Shoulds elements have default ids automatically applied to them?
66 67 68 |
# File 'lib/menu_helper/menu_bar.rb', line 66 def auto_set_ids? @options[:auto_set_ids] end |
#content_for_variable ⇒ Object
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 |
#html ⇒ Object
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.dup [:class] = "#{[:class]} #{selected_class}".strip if selected? content_tag(tag_name, content, ) end |
#level ⇒ Object
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 = while level += 1 = . end level end end |
#menu(id, content = nil, url_options = {}, html_options = {}, &block) ⇒ Object
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):
-
home_contact_us_url
-
contact_us_url
Example routes.rb:
ActionController::Routing::Routes.draw do |map|
map.(:controller => 'home') do |home|
home.home '', :action => 'index'
home.home_search 'search', :action => 'search'
end
map.(:controller => 'about_us') do |about_us|
about_us.about_us 'about_us', :action => 'index'
end
end
Example menu bar:
:home do |home|
home. :about_us # => Links to about_us_url
home. :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:
-
The id of the menu (‘contact_us’ => ContactUsController)
-
The controller of the parent menu/menu bar
-
The request controller
To override the default controller being linked to, you can explicitly define it like so:
:contact, 'Contact Us', :controller => 'about_us'
Examples:
:home do |home|
home. :about, 'About Us', :action => 'about_us' # => Links to {:controller => 'home', :action => 'about_us'}
home. :who_we_are # => Links to {:controller => 'home', :action => 'who_we_are'}
home. :contact_us, :controller => 'contact', :action => 'index' # => Links to {:controller => 'contact', :action => 'index'}
home. :search # => Links to {:controller => 'search'}
end
You can also link to an explicit URL like so:
home. :search, 'http://www.google.com'
Turning off links
If you don’t want a menu to link to a URL, you can turn off linking like so:
home. :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. :contact # => <li id="contact" class="ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Contact</a></li>
home. :contact, 'Contact Us' # => <li id="contact" class="ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Contact Us</a></li>
home. :contact, {}, :class => 'pretty' # => <li id="contact" class="pretty ui-menubar-menu ui-menubar-menu-1"><a href="/contact">Contact</a></li>
home. :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. :about do |about|
about. :who_we_are
about. :contact_us
end
177 178 179 180 181 182 |
# File 'lib/menu_helper/menu_bar.rb', line 177 def (id, content = nil, = {}, = {}, &block) = Menu.new(self, id, content, , , &block) @menus << end |
#parent_menu ⇒ Object
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 @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.
83 84 85 |
# File 'lib/menu_helper/menu_bar.rb', line 83 def selected? && .any? {|| .selected?} end |