Handling :only and :except in Rails
I'm trying to write a method that takes :only
and :except
as an options hash and do some work based on those values. Quickly my code is bloated.
I can't find how Rails handles those options in let's say controller before filters?
Both only
and except
can be a symbol or an a开发者_如何转开发rray eg. :only => :show
or :only => [:show, :destroy]
Suggestions?
Check out the _normalize_callback_options
method in abstract_controller/callbacks.rb (this is rails 3).
Basically what it does is create a string containing code for conditions you wanna impose. After that you'll just need to eval this code, like
str = "foo == :bar || foo == :baz"
if eval(str)
# you'd better do somethin
end
精彩评论