Class: MenuItem

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
lib/semantic_menu.rb

Direct Known Subclasses

SemanticMenu

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (MenuItem) initialize(title, link, level, link_opts = {})



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

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? ? '' : (: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
   :li, link_to(@title, @link, @link_opts) + child_output, ({:class => 'active'} if active?)
end