/navigation

A simple, powerful navigation helper for Rails.

Primary LanguageRubyMIT LicenseMIT

Navigation

A simple, powerful navigation plugin for Rails 3. The power of the plugin lies in controlling multiple navigation menus.

This plugin is used in Pockify.

Basic Usage

In your controller:

class PagesController < ApplicationController
  navigation :content, :pages
end

class PostsController < ApplicationController
  navigation :content, :posts
end

In your view:

<%= navigate :content, :pages do %>
  <%= link_to 'Pages' ... %>
<% end %>

<%= navigate :content, :posts do %>
  <%= link_to 'Posts' ... %>
<% end %>

When the view is rendered, the “current” navigational element will be selected. Assuming the request routed to the PagesController, the view is rendered as:

<span class="selected">
  <a href="...">Pages</a>
</span>

<span>
  <a href="...">Posts</a>
</span>

Multiple Navigations

The real power of this plugin lies in the ability to easily manage multiple navigations. In our view, imagine that we have main- and sub- navigation menus.

In our controller:

class ThemeGalleryController
  navigation :main_menu, :themes
  navigation :sub_menu,  :gallery
end

class ThemeEditorController
  navigation :main_menu, :themes
  navigation :sub_menu,  :editor
end

class ThemeUploadController
  navigation :main_menu, :themes
  navigation :sub_menu,  :upload
end

Navigate Independently

Controller actions can navigate independently of one another:

class PostsController
  navigation :posts, :all,     :only => :index
  navigation :posts, :active,  :only => :active
  navigation :posts, :drafts,  :only => :drafts
end

Conditional Rendering

In your view, you can control when to render a navigation item using the :if or :unless options:

<%= navigate :admin_menu, :users, :if => administrator? do %>
  ...
<% end %>

<%= navigate :admin_menu, :users, :unless => hacker? do %>
  ...
<% end %>

Customize Navigation Items

HTML Tag

You can specify a different HTML tag to wrap your navigation items using the :tag option:

<%= navigate :content_menu, :pages, :tag => :li do %>
  ...
<% end %>

CSS Class

You can specify CSS class names using the :class option:

<%= navigate :content_menu, :pages, :class => 'blue' do %>
  ...
<% end %>

Will render as:

<span class="blue">
  ...
</span>

License

Copyright © 2011 Jonathan Sutherland. Released under the MIT license.