Class: MenuItem
- Inherits:
-
Object
- Object
- MenuItem
- Includes:
- ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
- Defined in:
- lib/semantic_menu.rb
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) children
Returns the value of attribute children.
-
- (Object) link
Returns the value of attribute link.
Instance Method Summary (collapse)
- - (Boolean) active?
- - (Object) add(title, link, link_opts = {}, &block)
- - (Object) child_output
-
- (MenuItem) initialize(title, link, level, link_opts = {})
constructor
A new instance of MenuItem.
- - (Object) level_class
- - (Boolean) on_current_page?
- - (Object) to_s
Constructor Details
- (MenuItem) initialize(title, link, level, link_opts = {})
A new instance of MenuItem
12 13 14 15 |
# File 'lib/semantic_menu.rb', line 12 def initialize(title, link, level, link_opts={}) @title, @link, @level, @link_opts = title, link, level, link_opts @children = [] end |
Instance Attribute Details
- (Object) children
Returns the value of attribute children
9 10 11 |
# File 'lib/semantic_menu.rb', line 9 def children @children end |
- (Object) link
Returns the value of attribute link
9 10 11 |
# File 'lib/semantic_menu.rb', line 9 def link @link end |
Instance Method Details
- (Boolean) active?
36 37 38 |
# File 'lib/semantic_menu.rb', line 36 def active? children.any?(&:active?) || on_current_page? end |
- (Object) add(title, link, link_opts = {}, &block)
17 18 19 20 21 22 |
# File 'lib/semantic_menu.rb', line 17 def add(title, link, link_opts={}, &block) returning(MenuItem.new(title, link, @level +1, link_opts)) do |adding| @children << adding yield adding if block_given? end end |
- (Object) child_output
32 33 34 |
# File 'lib/semantic_menu.rb', line 32 def child_output children.empty? ? '' : content_tag(:ul, @children.collect(&:to_s).join, :class => level_class) end |
- (Object) level_class
28 29 30 |
# File 'lib/semantic_menu.rb', line 28 def level_class "menu_level_#{@level}" end |
- (Boolean) on_current_page?
40 41 42 43 |
# File 'lib/semantic_menu.rb', line 40 def on_current_page? @controller = @@controller # set it for current_page? defined in UrlHelper current_page?(@link) end |
- (Object) to_s
24 25 26 |
# File 'lib/semantic_menu.rb', line 24 def to_s content_tag :li, link_to(@title, @link, @link_opts) + child_output, ({:class => 'active'} if active?) end |