Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add after_{create,update,delete}_commit callbacks to observers #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/rails/observers/activerecord/observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ module ActiveRecord
#
class Observer < ActiveModel::Observer

CALLBACKS = ActiveRecord::Callbacks::CALLBACKS.dup

if Rails.version >= '5'
CALLBACKS.push(:after_create_commit, :after_update_commit, :after_destroy_commit)
end

protected

def observed_classes
Expand All @@ -110,7 +116,7 @@ def define_callbacks(klass)
observer = self
observer_name = observer.class.name.underscore.gsub('/', '__')

ActiveRecord::Callbacks::CALLBACKS.each do |callback|
CALLBACKS.each do |callback|
next unless respond_to?(callback)
callback_meth = :"_notify_#{observer_name}_for_#{callback}"
unless klass.respond_to?(callback_meth)
Expand Down
15 changes: 15 additions & 0 deletions test/active_record_observer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'minitest/autorun'
require 'active_record'
require 'rails/version'
require 'rails/observers/active_model/observing'
require 'rails/observers/activerecord/observer'

class ActiveRecordObservingTest < ActiveSupport::TestCase
def test_observers_have_correct_callbacks
expected_callbacks = ActiveRecord::Callbacks::CALLBACKS.dup
if Rails.version >= '5'
expected_callbacks.push(:after_create_commit, :after_update_commit, :after_destroy_commit)
end
assert_equal expected_callbacks, ActiveRecord::Observer::CALLBACKS
end
end