• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rails plugin: provides automatic navigation for RESTful architectures, and has simple support for those that aren't.

Navigation Helper

Web applications always use navigation. And the majority of the apps built enjoy keeping track of the current tab (or link or section or whatever). This plugin attempts to aid that process a bit.

You get two methods with this plugin:

  • navigation(sections, options={}) ā€“ this is the helper method that is used in your views
    • sections ā€“ a :symbol array of sections (i.e. [:home, :about, :contact])
      • note: each section must map to a named route (see below in ā€œAssumptionsā€ for more details)
    • options ā€“ used to override default behavior
  • current_tab(name) ā€“ used in any controller to override the current tab for that controller
    • name ā€“ must be a symbol matching the symbol passed in the navigation helper

Configuration Options:

  • authorize => [:links] ā€“ specifies which of the sections require authorization before showing up
    • note: use :authorize => [:all] to do all links at once (i.e. for an ā€œadminā€ menu)
  • with => :authorization_method ā€“ specifies the method to use to authorize against (defaults to logged_in? method)
    • note: requires the authorize option to actually work
  • hover_text => (true|false) ā€“ specifies to use the subtitles as hovertext instead of showing up as spanā€™s under the links
  • authorized_css => 'custom_css_class' ā€“ specifies the css class that goes on all authorized tabs (defaults to ā€˜authorized_nav_linkā€™)

Installation: Get it at GitHub

You can install this as a plugin. Navigation to your project root and type:

script/plugin install git://github.com/rpheath/navigation_helper.git

Assumptions (er, Conventions)

Use Symbols for Sections

You cannot pass strings to the navigation helper and expect it to work properly. Meaning:

# incorrect
<%= navigation ['home','about','contact_me'] %>

# correct
<%= navigation [:home, :about, :contact_me] %>

This is because of the subtitles. The plugin understands strings and symbols differently, so just make
sure youā€™re using symbols for the sections and strings for the subtitles.

Subtitles Must Follow Respective Section

If you choose to use subtitles, just make sure you keep them ā€œgroupedā€ together in their respective pairs.

<%= navigation([
  :home,    'Start Here',
  :about,   'Learn More',
  :contact, 'Get In Touch'
]) %>

Named Routes matching Sections

One thing to make note of is each symbolized link you pass to the navigation helper, it is expected that a matched named route exist
(unless youā€™re using custom routes ā€“ see below). For instance:

# calling this
<%= navigation [:home, :about, :contact_me] %>

# would expect these named routes to exist
home_path
about_path
contact_me_path

And by default, an ā€œunderscoredā€ link will result in capitalized words. So :contact_me would
result in ā€˜Contact Meā€™ link text. If you wish to have all lowercase or all uppercase, just use css to do that.

Custom Routes

If you need to deviate from the RESTful routes and break from consistency, you can. To set a custom route for one of your tabs, just pass a
key/value pair in place of the section, like so:

<%= navigation [{:home => some_custom_path}, :about, :contact] %>

Now your ā€˜Homeā€™ tab will go to some_custom_path instead of home_path.

The examples below provide some help on how to use this plugin.

Example Usage

There are several different ways you can use this plugin. Below are a few examples showing how.

Example 1

The most basic usageā€¦

<%= navigation [:home, :about, :contact] %>

ā€¦which will renderā€¦

<ul class="navigation">
  <li class="current"><a href="/home">Home</a></li>
  <li><a href="/about">About</a></li>
  <li><a href="/contact">Contact</a></li>
</ul>

The above example assumes that the user is actually on the home page (hence the ā€œcurrentā€ css class on the Home list item).

Example 1a (custom routes)

<%= navigation [{:home => some_custom_path}, :about, :contact] %>

ā€¦which will renderā€¦

<ul class="navigation">
  <li class="current"><a href="/some/custom/path">Home</a></li>
  <li><a href="/about">About</a></li>
  <li><a href="/contact">Contact</a></li>
</ul>

Example 2

Sometimes you need tabs to show up only based on some condition (such as a user being logged in). Picture a blog application. A Blog may have public tabs (such as Home, About, etc), but a logged in admin may want another tab to easily get to the admin interface. You can specify this behavior in the navigation helper by passing an array of links to the :authorize optionā€¦

</code><%= navigation [:home, :about, :contact, :admin], :authorize => [:admin] %>

Now, based on that setup, the ā€œAdminā€ tab will require authorization before showing up.

By default, a logged_in? method will be checked, but this can be overridden (as weā€™ll see next).

Example 3

By default, this plugin will check against a logged_in? method to ensure an authorized user. However, if you already have a custom method youā€™re using to limit access and donā€™t want to call it
logged_in? (or maybe you need to check against a certain role), you can specify that by passing the method to use using the :with option:

<%= navigation [:home, :about, :contact, :admin], :authorize => [:admin], :with => :authorize_first %>

Now the plugin will check against an authorize_first method instead of logged_in?.

Just for completeness, you can specify multiple links to be ā€œauthorizedā€.

<%= navigation [:home, :about, :users, :reports], :authorize => [:users, :reports] %>

Also worth noting, each ā€œauthorized linkā€ will have a default css class. A lot of the time, I want my ā€œauthorizedā€ tabs to be positioned to the right of the screen, while my regular tabs are to the left, which is why Iā€™ve added the css class. Also, this css class can be overriden using the :authorized_css option (defaults to ā€˜authorized_nav_linkā€™).

Example 4

Now, letā€™s say you want to use the navigation helper for an entire section of admin links, maybe to show up in an admin sidebar or something. Well, we donā€™t want to have to repeat all of those links in the :authorize option, so you can pass a single value of :all to show/hide the entire menu based on an authorized method:

<%= navigation [:dashboard, :users, :reports], :authorize => [:all] %>

Again, by default that will look for a logged_in? method, but you can override
that by passing your own (as shown above) using the :with option. The navigation
helper will return nothing if the authorization doesnā€™t pass.

Example 5

Just a quick example of how to add custom css for authorized links:

<%= navigation [:about, :admin], :authorize => [:admin], :authorized_css => 'admin' %>

This would render (assuming admin tab is the current)ā€¦

<ul class="navigation">
  <li><a href="/about">About</a></li>
  <li class="current admin"><a href="/admin">Admin</a></li>
</ul>

Example 6

Now, sometimes I want to place a subtitle under my links. Maybe a brief description or something. This plugin also supports that by passing the link followed by its subtitle, like so:

<%= navigation [:home, 'Start Here', :about, 'Learn More'] %>

This would render:

<ul class="navigation">
  <li class="current">
    <a href="/home">Home</a>
    <span>Start Here</span>
  </li>
  <li>
    <a href="/about">About</a>
    <span>Learn More</span>
  </li>
</ul>

The css is up to you, but the markup is definitely flexible enough for some nice handywork.

Example 7

Maybe you want your subtitles to be a little less obtrusive and not actually show up as markup. By setting the :hover_text option to true, the subtitles will then become hover text on the links instead. Redoing the above example with hover text, we get:

<%= navigation [:home, 'Start Here', :about, 'Learn More'], :hover_text => true %>

Notice the :hover_text => true option. This would renderā€¦

<ul class="navigation">
  <li class="current">
    <a href="/home" title="Start Here">Home</a>
  </li>
  <li>
    <a href="/about" title="Learn More">About</a>
  </li>
</ul>

The spanā€™s get replaced with link titleā€™s instead.

Example 8

By default this plugin will use the name of the current controller to determine the current tab. But what if you donā€™t want to name your controllers in the context of your navigation, and vice versa (which is a very practical need)? No problem.

Letā€™s say you have the following controllers:


def PublicController < ApplicationController
end

def AboutController < ApplicationController
end

def ContactController < ApplicationController
end

And you wanted your navigation links to be setup like so:

<%= navigation [:home, :about, :contact] %>

According to how the navigation helper works, you would have to replace :home with :public. But that would be confusing. This is where the current_tab method comes into play. Just do this to associate a controller with a tab:


def PublicController < ApplicationController
  current_tab :home
end

And youā€™re set. Now whenever youā€™re on any action within the PublicController, the navigation helper
will match the current tab up against :home instead of :public.

License

Copyright Ā© 2008 Ryan Heath (http://rpheath.com), released under the mit license

More Repositories

1

searchbox

A live search plugin for jQuery.
JavaScript
97
star
2

google_ajax_libraries_api

Rails plugin: implements Google's Content Delivery Network for all javascript libraries currently supported by their library API.
Ruby
52
star
3

jquery-flickr

Implements (some of) the Flickr API using jQuery.
JavaScript
45
star
4

input_css

Rails plugin: provides a default CSS class on all INPUT fields based on the type attribute - perfect for designers.
Ruby
42
star
5

form_assistant

Rails plugin: a custom, template-based form builder.
Ruby
36
star
6

pretty_flash

Rails plugin: provides reusable (and pretty) flash messages.
Ruby
29
star
7

navigation

New and improved navigation gem for Rails.
Ruby
24
star
8

acts_as_lookup

Rails plugin: provides a convenient way to deal with lookup tables and drop downs menus.
Ruby
18
star
9

ballparkit

Simple tool built in Ruby on Rails for creating project estimates.
Ruby
15
star
10

easy_search

Provides a nice and easy DSL to search ActiveRecord models.
Ruby
13
star
11

authorize

Rails plugin: action-level roles and permissions for any Rails application.
Ruby
10
star
12

tag-toggler

jQuery plugin: click the tag once to add it; click it again to remove it.
JavaScript
8
star
13

acts_as_configurable

Rails plugin: supports dynamic configuration for any class or model (does not depend on ActiveRecord).
Ruby
5
star
14

coffee-slideshow

A jQuery content slider written in CoffeeScript.
CoffeeScript
4
star
15

restful_controller

Rails plugin: a very bare bones abstraction of the RESTful actions in a Rails' resource controller.
Ruby
3
star
16

has_params

Rails plugin: allows for checking the params hash in a cleaner fashion by using methods instead of the hash directly.
Ruby
3
star
17

devise_password_history

Extension for Devise to support password history and validations thereof.
Ruby
2
star
18

rpheath.github.com

Just because.
2
star
19

ritzylunch

The Ritzy Lunch
JavaScript
1
star