Class: MenuBuilder::ViewHelpers::Menu
- Inherits:
-
Object
- Object
- MenuBuilder::ViewHelpers::Menu
show all
- Defined in:
- lib/menu_builder/helper.rb
Instance Method Summary
collapse
Constructor Details
#initialize(context, &block) ⇒ Menu
26
27
28
29
30
31
|
# File 'lib/menu_builder/helper.rb', line 26
def initialize(context, &block)
@context = context
= context.instance_variable_get('@menu_items')
@actual_items = []
@block = block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(item, *args, &block) ⇒ Object
33
34
35
36
|
# File 'lib/menu_builder/helper.rb', line 33
def method_missing item, *args, &block
@actual_items << .new(item, args, block)
nil
end
|
Instance Method Details
#first?(item) ⇒ Boolean
66
67
68
|
# File 'lib/menu_builder/helper.rb', line 66
def first? item
@actual_items.first == item
end
|
#html_options_for(item) ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/menu_builder/helper.rb', line 47
def html_options_for item
css_classes = []
css_classes << "current" if included_in_current_items? item
css_classes << "first" if first? item
css_classes << "last" if last? item
options = {}
options[:class] = css_classes.join(" ") if css_classes.any?
options
end
|
#included_in_current_items?(item) ⇒ Boolean
58
59
60
|
# File 'lib/menu_builder/helper.rb', line 58
def included_in_current_items?(item)
.present? && .include?(item.to_sym)
end
|
#last?(item) ⇒ Boolean
62
63
64
|
# File 'lib/menu_builder/helper.rb', line 62
def last? item
@actual_items.last == item
end
|
#render ⇒ Object
38
39
40
41
|
# File 'lib/menu_builder/helper.rb', line 38
def render
@block.call(self)
@actual_items.map { |item| render_one item }.join.html_safe
end
|
#render_one(item) ⇒ Object
43
44
45
|
# File 'lib/menu_builder/helper.rb', line 43
def render_one item
@context.content_tag :li, item.link_to_in_context(@context), html_options_for(item)
end
|