Navtastic

Build Status Code Coverage Inline docs MIT License

Navtastic is way to create and render complex navigation menus. It allows for runtime configurations of menus.

  • Keep menu content and rendering logic separate
  • Automatically highlight the item for the current page

Table of Contents

Installation

Add it to your Gemfile:

gem 'navtastic'

Run the following command to install it:

bundle install

Example

Define a menu somwhere:

Navtastic.define :main_menu do |menu|
  menu.item "Home", '/'
  menu.item "Posts", '/posts'
  menu.item "About", '/about'
end

Render it in your partials:

<%= Navtastic.render :main_menu, current_url %>

Using the default renderer, assuming that /posts is the current url, will result in:

<ul>
  <li>
    <a href="/">Home</a>
  </li>
  <li class="current">
    <a href="/posts">Posts</a>
  </li>
  <li>
    <a href="/about">About</a>
  </li>
</ul>

Usage

You can pass runtime parameters when defining a menu. For example, passing the current user and change the menu accordingly.

# Define the menu
Navtastic.define :main_menu do |menu, params|
  menu.item "Home", "/"

  if params[:current_user]
    menu.item "Profile", "/users/#{params[:current_user].id}"
    menu.item "Logout", "/logout"
  else
    menu.item "Login", "/login"
  end
end

# Render it with the current user as a parameter
Navtastic.render :main_menu, current_url, current_user: User.current