Class: MenuBlock
- Inherits:
-
Object
- Object
- MenuBlock
- Defined in:
- lib/components/menu_block.rb
Defined Under Namespace
Classes: MenuItem
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#add_item(name, default: false, match_params: {}, &route_block) ⇒ Object
Adds a menu item to the menu block.
-
#initialize(view_context, html_options = {}) ⇒ MenuBlock
constructor
A new instance of MenuBlock.
- #render(&block) ⇒ Object
Constructor Details
#initialize(view_context, html_options = {}) ⇒ MenuBlock
Returns a new instance of MenuBlock.
6 7 8 9 10 |
# File 'lib/components/menu_block.rb', line 6 def initialize(view_context, = {}) @context = view_context @html_options = @items = [] end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/components/menu_block.rb', line 3 def context @context end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
4 5 6 |
# File 'lib/components/menu_block.rb', line 4 def @html_options end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
2 3 4 |
# File 'lib/components/menu_block.rb', line 2 def items @items end |
Instance Method Details
#add_item(name, default: false, match_params: {}, &route_block) ⇒ Object
Adds a menu item to the menu block
Options
-
default: false
- Whether or not this item is default if nothing else is active. -
match_params: {}
- Pass a hash to compare with params. Leave blank for defaults matchers. To use:{action: [:new, :create], id: '12345'}
.
Examples
For a regular default menu item
= menu.add_item('Profile', default: true) { edit_person_path(@person, page: 'profile') }
For a subitem you can so something like this
.subitems
= menu.add_item('New Custom Profile', match_params: {action: [:new, :create]}) { new_person_custom_profile_path(@person) }
28 29 30 31 32 |
# File 'lib/components/menu_block.rb', line 28 def add_item(name, default: false, match_params: {}, &route_block) items << MenuItem.new(context, name, default: default, match_params: match_params, &route_block) placeholder(items.length - 1) end |
#render(&block) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/components/menu_block.rb', line 34 def render(&block) # Grab the HTML from the block so that custom HTML can be injected into the menu html = context.capture(self, &block) items.each_with_index do |item, i| opts = .dup || {} if item.active? || (items.none?(&:active?) && item.default?) opts[:class] = [[:class], 'active'].compact.join(' ') end html[placeholder(i)] = context.link_to(item.name, item.route, opts) end html end |