Tiny Navigation

Installation

As a Plugin

  • For Rails 2.x

    script/generate plugin install [email protected]:coroutine/tiny_navigation.git
    

As a gem

gem install tiny_navigation

Then, simply call:

script/generate tiny_navigation

This will place the tiny_navigation.rb config file into your config directory.

Usage

TinyNavigation provides an easy-to-use DSL for defining navigation structures; these structures are defined in config/tiny_navigation.rb and are accessed via a single method: navigation. For example, provided the following configuration:

navigation :main do
    item "Store", :to => "products#index"
    item "Blog", :to => "blog#index"
end

the code for accessing this structure would be:

navigation :main

The resulting structure could be used to generate the markup. For example:

 :ul, :class => :tabs do
  (navigation(:main).items.map do |item|
     :li do
      link_to item.name, item.url, :class => item.selected? ? :selected : ""
    end
  end).join("")
end

Here are a few things TinyNavigation WILL do:

  • It provides the ability to define and map menu items to resources using the convention set forth in the Rails 3 router. For example, to map a menu item Foo to the show action of the foos_controller simply do:

    navigation :top_tabs do
      item "Foo", :to => "foos#show"
    end
    

    If one were to omit the specified action from the :to option, the navigation item’s action would default to index.

    The URL generated from this mapping can be accessed via the url method of the navigation item.

  • It provides a selected method for getting the selected menu items of a navigational structure. For example, for this definition:

    navigation :main do
      item "Foo", :to => "foos#index"
      item "Bar", :to => "bars#index" do
        item "Baz", :to => "bazzs#index"
      end
    end
    

    If the menu item Foo is selected, an array containing that menu item is returned. However, if the menu item Baz is selected, an array containing the Bar and the Baz menu items. This is useful for generating bread-crumbs or simply highlighting both the main nav item and its selected sub-nav item.

  • It allows for the declaration of custom attributes on nav items. For instance, given the configuration in the previous example, we want to right-align the Bar navigation item. To do this we could simply add another option to the item:

    navigation :main do
      item "Foo", :to => "foos#index", :align => :left
      item "Bar", :to => "bars#index", :align => :right do
        item "Baz", :to => "bazzs#index"
      end
    end
    

    Now, when we render the navigation items we can call align on the item to get its value:

    navigation(:main).items.each do |item|
      if item.align == :right
        ...
      end
    end
    
  • It delegates controller method calls made from within the config file to the current controller. For instance, let’s say you’re using Ryan Bates’ fantastic CanCan gem for authorization–which adds a some methods to the controller, namely the can? method–and you want to show or hide navigation items based upon a user’s ability. You can do that! Check it:

    navigation :main do
      item("Foo", :to => "foos#index") if can? :read, Foo
      item "Bar", :to => "bars#index" do
        item "Baz", :to => "bazzs#index"
      end
    end
    

    IMPORTANT if a custom attribute is defined on an item, as mentioned earlier, it will take precedence over a controller attribute of the same name, thus hiding access to the controller attribute from within the config file.

Here are a couple things that TinyNavigation WILL NOT do:

  • TinyNavigation makes no attempt at rendering the navigation. That’s up to you. You may want to render your nav items into div tags, while I may want to use an unordered list. That’s fine, go for it.

  • TinyNavigation does not provide authorization logic for limiting access to navigation items; that’s a separate concern. It’s easy enough to use an authorization gem that does that job quite well, and by allowing for calls to the current controller from within config/tiny_navigation.rb, you can do that.

Copyright © 2010 Coroutine, released under the MIT license