Class: MenuHelper::Menu

Inherits:
HtmlElement show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
lib/menu_helper/menu.rb

Overview

Represents a single menu within a menu bar

Constant Summary collapse

'ui-menubar-menu'
@@selected_class =
'ui-state-active ui-menubar-selected'
@@last_class =
'ui-menubar-last'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_menu_bar, name, content = nil, url_options = {}, html_options = {}) {|@menu_bar| ... } ⇒ Menu

:nodoc

Yields:



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
# File 'lib/menu_helper/menu.rb', line 36

def initialize(parent_menu_bar, name, content = nil, url_options = {}, html_options = {}) #:nodoc
  # Allow the content parameter to be skipped
  content, url_options, html_options = nil, content, url_options if content.is_a?(Hash)
  
  # Remove non-html options that are specific to this element and shouldn't
  # be rendered as an html attribute
  @options = html_options.slice(:link)
  html_options.except!(:link)
  
  super(html_options)
  
  @parent_menu_bar = parent_menu_bar
  @name = name.to_s
  
  # Set context of the menu for url generation
  @controller = request_controller
  
  # Generate the text-based content of the menu
  @content = (:span, content || @name.underscore.titleize)
  
  # Set up url
  url, @url_options = build_url(url_options)
  @content = link_to(@content, url) if @options[:link] != false
  
  # Set up default html options
  id_prefix = parent_menu_bar[:id] || parent_menu && parent_menu[:id]
  self[:id] ||= "#{id_prefix}-#{@name}" if auto_set_ids? && id_prefix
  self[:class] = "#{self[:class]} #{menu_class} #{menu_class}-#{level}".strip
  
  # Create the menu bar for sub-menus in case any are generated.  Use the
  # same configuration as the parent menu bar.
  @menu_bar = MenuBar.new(request_controller, parent_menu_bar.options.merge(:parent_menu => self))
  
  yield @menu_bar if block_given?
end

Instance Attribute Details

#nameObject (readonly)

The unique name assigned to this menu



19
20
21
# File 'lib/menu_helper/menu.rb', line 19

def name
  @name
end

#parent_menu_barObject (readonly)

The menu bar in which this menu exists



26
27
28
# File 'lib/menu_helper/menu.rb', line 26

def parent_menu_bar
  @parent_menu_bar
end

#url_optionsObject (readonly)

The url where this menu is linked to. This can either be a hash of url options or a string representing the actual url



23
24
25
# File 'lib/menu_helper/menu.rb', line 23

def url_options
  @url_options
end

Instance Method Details

#html(last = false) ⇒ Object

Builds the actual html of the menu



79
80
81
82
83
84
85
# File 'lib/menu_helper/menu.rb', line 79

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

#selected?Boolean

Is this menu selected? A menu is considered selected if it or any of its sub-menus are selected

Returns:

  • (Boolean)


74
75
76
# File 'lib/menu_helper/menu.rb', line 74

def selected?
  current_page?(url_options) || @menu_bar.selected?
end