Rails : select ActiveSupport::Callbacks 选项

Rails : select ActiveSupport::Callbacks on options

我有一个控制器,我必须在其中访问我的回调列表。

我用它来过滤它们:

_process_action_callbacks.select{|f| f.kind != :around}

我也在尝试在选项上过滤它们(即在 'if' 和 'unless' 数组上)。

我试过了

_process_action_callbacks.select{|f| f.if.any? }

但显然没有 'if' 方法。

我很困惑,因为我有这个:

_process_action_callbacks.select{
  |f| f.kind != :around
}.first
=> #<ActiveSupport::Callbacks::Callback:0x007fd1a16295d8
 @chain_config=
  {:scope=>[:kind],
   :terminator=>
    #<Proc:0x007fd1928375c8@/Users/name/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/actionpack-4.1.0/lib/abstract_controller/callbacks.rb:12 (lambda)>,
   :skip_after_callbacks_if_terminated=>true},
 @filter=:foo,
 @if=["action_name == 'create'"],        # 'if' seems present
 ...

还有这个:

_process_action_callbacks.select{|f| f.kind != :around}.first.instance_variables
=> [:@chain_config, :@name, :@kind, :@filter, :@key, :@if, :@unless]

有没有办法获取回调的选项或'if'?

是的,@if 没有 accessorreader 方法,已检查 here. But you can still get it using instance_variable_get 方法。

_process_action_callbacks.select do |f|
  f.instance_variable_get(:@if).any?
end