mirror of
https://github.com/discourse/discourse.git
synced 2024-12-12 14:05:59 +08:00
49 lines
1.3 KiB
Ruby
49 lines
1.3 KiB
Ruby
#
|
|
# Support delegating after_create to an appropriate helper for that class name.
|
|
# For example, an observer on post will call after_create_post if that method
|
|
# is defined.
|
|
#
|
|
# It does this after_commit by default, and contains a hack to make this work
|
|
# even in test mode.
|
|
#
|
|
class DiscourseObserver < ActiveRecord::Observer
|
|
|
|
def after_create_delegator(model)
|
|
observer_method = :"after_create_#{model.class.name.underscore}"
|
|
send(observer_method, model) if respond_to?(observer_method)
|
|
end
|
|
|
|
def after_destroy_delegator(model)
|
|
observer_method = :"after_destroy_#{model.class.name.underscore}"
|
|
send(observer_method, model) if respond_to?(observer_method)
|
|
end
|
|
|
|
end
|
|
|
|
if Rails.env.test?
|
|
|
|
# In test mode, call the delegator right away
|
|
class DiscourseObserver < ActiveRecord::Observer
|
|
alias_method :after_create, :after_create_delegator
|
|
alias_method :after_destroy, :after_destroy_delegator
|
|
end
|
|
|
|
else
|
|
|
|
# Outside of test mode, use after_commit
|
|
class DiscourseObserver < ActiveRecord::Observer
|
|
def after_commit(model)
|
|
if model.send(:transaction_include_action?, :create)
|
|
after_create_delegator(model)
|
|
end
|
|
|
|
if model.send(:transaction_include_action?, :destroy)
|
|
after_destroy_delegator(model)
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
end
|
|
|