class MenuBlock
Attributes
context[R]
html_options[R]
items[R]
Public Class Methods
new(view_context, html_options = {})
click to toggle source
# File lib/components/menu_block.rb, line 6 def initialize(view_context, html_options = {}) @context = view_context @html_options = html_options @items = [] end
Public Instance Methods
add_item(name, default: false, match_params: {}, &route_block)
click to toggle source
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) }
# 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)
click to toggle source
# 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 = html_options.dup || {} if item.active? || (items.none?(&:active?) && item.default?) opts[:class] = [html_options[:class], 'active'].compact.join(' ') end html[placeholder(i)] = context.link_to(item.name, item.route, opts) end html end
Private Instance Methods
placeholder(number)
click to toggle source
# File lib/components/menu_block.rb, line 52 def placeholder(number) # Placeholder for delayed rendering. We need this so that we can work # with all of the menu items so that by the time we render the first one, # which could be the default, we know whether or not any others are active. "[[menu_block:#{number}]]" end