1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Test callback behavior when update_attribute in callback

As mentioned in https://github.com/rails/rails/pull/42002#issuecomment-822699276,
https://github.com/rails/rails/pull/41860 introduced a behavior change
in callbacks whenever `update_attribute` is called within a `before_*`
callback triggered by a `create` which caused `after_update` callbacks
to be triggered by the create.

This ensures once the revert is re-reverted we'll have properly
addressed the fix
This commit is contained in:
Leo Correa 2021-04-29 10:34:45 -04:00
parent 06e547d2ef
commit 52c2fa992f
No known key found for this signature in database
GPG key ID: 73232F06E7D320B7

View file

@ -486,6 +486,35 @@ class PersistenceTest < ActiveRecord::TestCase
assert_equal("David", topic_reloaded.author_name)
end
def test_update_attribute_in_before_validation_respects_callback_chain
klass = Class.new(Topic) do
def self.name; "Topic"; end
before_validation :set_author_name
after_create :track_create
after_update :call_once, if: :saved_change_to_author_name?
attr_reader :counter
def set_author_name
update_attribute :author_name, "David"
end
def track_create
call_once if saved_change_to_author_name?
end
def call_once
@counter ||= 0
@counter += 1
end
end
comment = klass.create(title: "New Topic", author_name: "Not David")
assert_equal 1, comment.counter
end
def test_update_attribute_does_not_run_sql_if_attribute_is_not_changed
topic = Topic.create(title: "Another New Topic")
assert_no_queries do