Skip to content

Provides easy ways of specifying navigation for RoR application

Notifications You must be signed in to change notification settings

ratnikov/navlinks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

Provides easy ways of specifying navigation for RoR application

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages