Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Provides easy ways of specifying navigation for RoR application

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 navlinks.gemspec
README.rdoc

Very light-weight implementation for navigation links

Simple navigation (HOW-TO)

Install

gem install navlinks --source http://gemcutter.org

First add navigation areas to the layout

app/views/layouts/application.html.erb

<html>
  <head>
  </head>
  <body>
    <ul id="nav">
      <li><%= nav_link_to :home, root_path %></li>
      <li><%= nav_link_to :foo, foo_path %></li>
    </ul>

    <div id="content">
      <%= yield %>
    </div>
  </body>
</html>

Next, label what views belong to what nav areas

Suppose we have following pages:

1. A home page that is rendered by +pages/home.html.erb+ template and that we want to belong to 'home' nav area.
2. A page where we show a Foo resource that is rendered by +foos/show.html.erb+ template and that we want to belong to 'foo' nav area.

Easy! Just assign the nav-areas at the top of the views like this:

app/views/pages/home.html.erb

<%- self.nav_area = :home -%>

app/views/foos/show.html.erb

<%- self.nav_area = :foo -%>

Done!

Now when someone is viewing the pages/home.html.erb template, the 'home' anchor tag will have a class current. If someone is viewing a foos/show.html.erb template, then the 'foo' anchor tag will have a class current. When viewing any other page, however, both of the navigation anchors will not have any classes.

Localization

navlinks supports common localization but also allows some tricks. Suppose the localization file is as follows:

config/locate/en.yml

en:
  navigation:
    home: Go home
    home_current: At home

This causes the 'home' area description to be usually 'Go home'. However if the user is looking at a page that belongs to 'home' nav area, it will display 'At home'. Cool, huh?

Decoration Override

By default the plugin decorates the 'current' area with brackets. It is possible to override this behavior by overriding decorate_current_label method:

app/helpers/application_helper.rb

module ApplicationHelper

  # causes the current navigation label to be described as:
  # * home *
  def decorate_current_label( current_label )
    "* #{current_label} *"
  end
end

Now instead of [ home ] you will see * home *

Contact

Feel free to contact me with any questions/comments!

Something went wrong with that request. Please try again.