Skip to content

hardpixel/smart-navigation

Repository files navigation

SmartNavigation

View helpers for navigation menus. Build navigation menus from hash objects.

Gem Version Build Status Maintainability

Installation

Add this line to your application's Gemfile:

gem 'smart_navigation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install smart_navigation

Usage

Define the menu items in a hash:

@items = {
  dashboard: {
    label:        'Dashboard',
    url:          :root_path,
    icon:         'dashboard',
    root:         true,
    html:         { target: :_blank },
    wrapper_html: { class: 'dashboard' }
  },
  pages: {
    label: 'Pages',
    url:   :admin_pages_path,
    icon:  'pages',
    children: {
      index: {
        label: 'All Pages',
        url:   :pages_path
      },
      new: {
        label: 'New Page',
        url:   :new_page_path
      }
    }
  },
  system: {
    label:     'System',
    separator: true
  },
  profile: {
    label: 'Profile',
    url:   -> { edit_user_path(current_user) },
    icon:  'user',
    if:    :user_signed_in?
  }
}

The url key can be a String, a Symbol or a Proc. Keys if and unless can be a Symbol or a Proc. Symbols and Procs are executed in the view context.

To render a menu you can use the navigation_for or smart_navigation_for helper in your views:

<%= smart_navigation_for(@items) %>

There are a number of options you can use to customize the navigation. The default options are:

options = {
  menu_class:           'menu',
  menu_html:            {},
  menu_icons:           true,
  item_class:           'menu-item',
  separator_class:      'separator',
  submenu_parent_class: 'has-submenu',
  submenu_class:        'submenu',
  submenu_item_class:   'submenu-item',
  active_class:         'active',
  active_submenu_class: 'open',
  submenu_icons:        false,
  submenu_toggle:       nil,
  icon_prefix:          'icon icon-',
  icon_default:         'missing',
  icon_position:        'left',
  keep_defaults:        true
}

smart_navigation_for(@items, options)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/smart-navigation.

License

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