Rails Observer Alternatives for 4.0

前端 未结 12 821
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 05:29

With Observers officially removed from Rails 4.0, I\'m curious what other developers are using in their place. (Other than using the extracted gem.) While Observers were cer

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 05:38

    In some cases I simply use Active Support Instrumentation

    ActiveSupport::Notifications.instrument "my.custom.event", this: :data do
      # do your stuff here
    end
    
    ActiveSupport::Notifications.subscribe "my.custom.event" do |*args|
      data = args.extract_options! # {:this=>:data}
    end
    

提交回复
热议问题