27 Oct, 2008

Published at 04:15PM

Tagged with navigation, plugin, programming, rails, and ruby

This post has 0 comments

navigation_helper plugin update

You can now set the current tab through inheritance. This is something I’ve been meaning to fix for a while now, but forgot it was an issue until recently.

Inheritance is very useful. Here, all of my administrative controllers inherit from an AuthorizedController. This allows me to ensure authorization in a single place, rather than repeat code in each controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Admin::AuthorizedController < ApplicationController
  before_filter :authorize
end

class Admin::PostsController < AuthorizedController
  current_tab :admin
  # ...
end

class Admin::CommentsController < AuthorizedController
  current_tab :admin
  # ...
end

See how I only have to set the before_filter in a single place? Notice anything else? Yeah, me too. While the authorization part is nice and DRY, setting the current tab to :admin is clearly not.

“Well, why don’t you just set it in the super class just like the before_filter?”

Because it originally didn’t work that way. It was based on setting an instance variable (which, by nature, only deals with the “instance” of the controller in question, and does not get passed down the inheritance chain). When I rebuilt my site, I realized this (again) and fixed it. But I forgot to update the stand-alone plugin.

Now, it works just like that before_filter example above:

1
2
3
4
5
6
7
8
9
10
11
12
class Admin::AuthorizedController < ApplicationController
  current_tab :admin
  before_filter :authorize
end

class Admin::PostsController < AuthorizedController
  # ...
end

class Admin::CommentsController < AuthorizedController
  # ...
end

Much better.

Comments

Do you have something to say about this post?
Protected by Defensio Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch