Project

current

0.0
No commit activity in last 3 years
No release in over 3 years
Picks up where Rails current_page? helper leaves off
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Current¶ ↑

Gives you a couple of simple helpers which check the current request in a Rails application. It’s sort of a current_page? on steroids

Installation¶ ↑

gem install current

Usage¶ ↑

Let’s say you have a PostsController with a new action and you are currently on that page

controller_is('posts')
# => true

This method also accepts multiple arguments and will return true if any of them match

controller_is('posts','users')
# => true

controller_is('sessions','users')
# => false

The same logic applies to actions

action_is('new')
# => true

action_is('new','edit')
# => true

action_is('edit','index')
# => false

If you want to be specific:

controller_action_is('posts','new')
# => true

controller_action_is('posts','index')
# => false

Along with these methods you get a navigational link_to helper:

nav_link_to("Posts", posts_path, controller_is('posts'), options={})

# => "<a href="/posts" class="active"></a>"

nav_link_to("Users", users_path, controller_is('users'), options={})

# => "<a href="/posts" class="inactive"></a>"

Notice that the third argument is a condition which we conveniently slap our helper into This will add a class of “active” or “inactive” to your link depending on whether that condition is true.

Note: An alternative for styling would be to include the controller and action names in your body id/class and use plain old CSS inheritance.

In your application layout

%body{:id => controller_name, :class => action_name}
  %ul#navigation
    %li= link_to "Posts", posts_path
    %li= link_to "Users", users_path

In your stylesheet

ul#navigation li a {
  background:white
}
body#posts ul#navigation li a,
body#users ul#navigation li a {
  background:red
}

I prefer to explicitly use the “active” classes for cleaner CSS, but whatever floats your boat.

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Steven Garcia. See LICENSE for details.