Module: PartialMenu::ViewHelpers

Defined in:
lib/partial_menu/view_helpers.rb

Overview

PartialMenu view helpers

This module serves for availability in ActionView templates. It also adds a new view helper: partial_menu.

Instance Method Summary collapse

Instance Method Details

#partial_menu(type = 'main', options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength

== Using the helper

If the helper is called without passing in the type, it will render the default menu using the default view partials.

Example:

<%= partial_menu %>

… will result in menu.yaml getting displayed using partials from app/views/partial_menu:

<ul>
  <li><a href="/">First item</a></li>
  ...
</ul>

Any options defined in the call will be send to render as local variables

Example:

<%= partial_menu 'menu', {menu_id:'sidemenu'} %>

You can leave out menu type parameter:

Example:

<%= partial_menu {menu_id:'sidemenu'} %>


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/partial_menu/view_helpers.rb', line 40

def partial_menu(type = 'main', options = {})
  unless (type.is_a? String) || (type.is_a? Hash)
    raise ::ArgumentError, "Expected a String or Hash, got #{type.class}"
  end
  unless options.is_a? Hash
    raise ::ArgumentError, "Expected a Hash, got #{options.class}"
  end
  if type.is_a? Hash
    options = type
    type = 'main'
  end
  options[:menu] = PartialMenu::Menu.new(
    load_menu_from_yaml(
      get_yaml_prefix(type, options)
    ),
    type
  )
  options.deep_symbolize_keys!
  render partial: "#{type}_menu/menu", locals: options
end