How can I remove callbacks inserted by vendor code?

被刻印的时光 ゝ 提交于 2019-12-22 09:47:02

问题


A gem I am using inserts an after_save callback that I would like to remove. It seems to me it would be cleaner to delete a symbol from an array than to fix the problem with a monkeypatch. How can I access the array of callbacks?


回答1:


class UserSession < Authlogic::Session::Base
  # Don't use cookie AuthLogic behaviour
  skip_callback :persist, :persist_by_cookie
  skip_callback :after_save, :save_cookie
  skip_callback :after_destroy, :destroy_cookie
end



回答2:


the after_save array is accessible via Model.after_save, it is an array of ActiveSupport::Callbacks::Callback objects. You could run this from within the model

self.after_save.delete_if{|callback| callback.method == :do_something_callback}


来源:https://stackoverflow.com/questions/3240914/how-can-i-remove-callbacks-inserted-by-vendor-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!