Tiny Navigation
TinyNavigation provides an easy-to-use DSL for defining navigation structures.
Usage
TinyNavigation’s structures are defined in config/tiny_navigation.rb and are accessed via a single method: navigation
. For example, provided the following configuration:
:main do
item "Store", :to => "products#index"
item "Blog", :to => "blog#index"
end
the code for accessing this structure would be:
:main
The resulting structure could be used to generate the markup. For example:
content_tag :ul, :class => :tabs do
((:main).items.map do |item|
content_tag :li do
link_to item.name, item.url, :class => item.selected? ? :selected : ""
end
end).join("")
end
What 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 thefoos_controller
simply do::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 toindex
.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::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:
: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::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.
What 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.
Helpful Links
-
Repository: github.com/coroutine/tiny_navigation
-
Authors: coroutine.com
Installation & Generators (Rails 3)
Install me from RubyGems.org by adding a gem dependency to your Gemfile. Bundler does the rest.
gem “tiny_navigation”
$ bundle install
Then generate the required config file.
$ rails g tiny_navigation
Installation & Generators (Rails 2)
Install as a gem from RubyGems.org and add a gem dependency in the appropriate file.
$ gem install tiny_navigation
Or install as a plugin.
$ script/plugin install git://github.com/coroutine/tiny_navigation.git
Either way, then generate the required config file.
$ script/generate tiny_navigation
Gemroll
Other gems by Coroutine include:
License
Copyright © 2010 Coroutine LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.