Gem Version

PartialMenu

Generating hierarchical menus in Rails should utilize Rails' built-in partial views instead of using some kind on HTML code generators. This gem just do that.

Usage

After installing the gem you can generate the views and the config file to create your own menus, even more than one can be generated by using name prefixes.

rails generate partial_menu:yaml

This will generate an example menu description yaml file in config/main_menu.yaml

You can have multiple menu definitiions in an app. For this you can use the --type parameter to set the name of the file

rails generate partial_menu:yaml -t side

This will create config/side_menu.yaml

You can define your menu structure in the generated yaml file:

---
menu:                                   # Marks the beginnin of the menu
  - first_item:                         # Adds a menu item. Text here will be parsed as id for the item.
    title: "First item"                 # Optional title of the item. If not set, ID will be used
    uri: root                           # Optional URI, in any format url_for would parse
    icon: test                          # Optional icon for the item.
  - second_item: separator              # A second menu item, specifing that its just a separator.
  - third_item:
    icontype: glyph                     # You can add any arbitarty property to an item, it will be parsed and can be used in templates.
    menu:                               # Here comes the submenu
    - first_submenu: link               # First submenu, with specifing its link as a shortcut.
    - second_submenu:
      title: menu.second_submenu.title  # You can use any text as title. In your template you can use it as you whish, like translate it with I18N::t()
      uri: second

Customizing views:

    rails generate partial_menu:views -d side

This will create:

  • app/views/side_menu/_menu.html.erb -- menu root template
  • app/views/side_menu/_item.html.erb -- menu items template
  • app/views/side_menu/_separator.html.erb -- separator template
  • app/views/side_menu/_submenu_item.html.erb -- submenu root template
  • app/views/side_menu/_item.html.erb -- submenu items template

Installation

Add this line to your application's Gemfile:

gem 'partial_menu'

And then execute:

$ bundle

Or install it yourself as:

$ gem install partial_menu

License

The gem is available as open source under the terms of the MIT License.