开发者

Handling :only and :except in Rails

开发者 https://www.devze.com 2023-02-11 21:38 出处:网络
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'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
0

精彩评论

暂无评论...
验证码 换一张
取 消