DRYing up between Helper and Controller

I have a few small methods as part of my authorisation scheme, These
mostly get used by controllers, but sometimes I want a template to
call them too.

For example, there are methods called is_editor?, is_author? and
is_publisher? currently in application.rb.

I am including/excluding elements for navigation in the user interface
based upon these roles. Therefore I want one definition of these
methods that “everyone” can use.

Whats the right rails way to achieve this?

Cheers, --Kip

Kip wrote:

I have a few small methods as part of my authorisation scheme, These
mostly get used by controllers, but sometimes I want a template to
call them too.

For example, there are methods called is_editor?, is_author? and
is_publisher? currently in application.rb.

I am including/excluding elements for navigation in the user interface
based upon these roles. Therefore I want one definition of these
methods that “everyone” can use.

Whats the right rails way to achieve this?

I’d issue “helper_method :is_editor?” in my application controller.
Others prefer a construction like so:

class ApplicationHelper
def for_editors
yield if is_editor?
end
end

Now you can do:

<% for_editors do -%>
Editors only!
<% end -%>

Whichever is prettier is a matter of preference I suppose.


Roderick van Domburg