Using before_all with :except

1 view
Skip to first unread message

Phrogz

unread,
Sep 19, 2008, 12:39:28 PM9/19/08
to Ramaze
(This post is largely to archive the information on the Web for future
people searching for information.)

I have a Ramaze simple app with a single controller. I want to use
user authorization on every action EXCEPT the action that lets users
login.

I was hoping for this:
before_all( :except=>:login ){ login_required }

Unfortunately, the before_all method can't take a hash like that. I
wrote up a patch to do it, and then decided not to submit it when I
discovered that each controller can only have a single before_all
handler. For example:

class MainController < Ramaze::Controller
# This code will never be run...
before_all{ destroy_the_universe }

# ...because this code gets run instead
before_all{ pat_the_bunnies }
end

While it might be nice to extend the Aspect helper in Ramaze to allow
multiple before_all blocks per controller, I realized that the easy
way around my problem is this:

class MainController < Ramaze::Controller
before_all do
# some things that I want done for all actions
pat_the_bunnies

# and now some things with exceptions
@action = Ramaze::Action.current.name.to_sym
login_required unless @action == :login
write_to_log unless @action == :secret_stuff
end
end

As a Ramaze newbie, I think that I also could have worked around this
problem by making a LoginController that would (I assume) be exempt
from the before_all block in MainController. The above simply shows
one way of exempting certain actions from before_all code in the same
controller.
Reply all
Reply to author
Forward
0 new messages