From 34f075fe5666dcf924606f8af2537b83b7b5139f Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Wed, 22 Aug 2018 20:00:19 +0900 Subject: Mutation tracker should be cleared before continuing around callbacks `changes_applied` should be called before continuing around callback chain. Otherwise the mutation tracker returns old value for methods like `changed`? or `id_in_database` in around callbacks. Also methods depend on `id_in_database`, like `update_column`, are not working in `around_create` callbacks. ``` class Foo < ActiveRecord::Base around_create :around_create_callback def around_create_callback ... yield p id_in_database # => nil update_column(:generated_column, generate_value) # silently fails end ... end ``` --- activerecord/test/cases/dirty_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activerecord/test/cases/dirty_test.rb') diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 83cc2aa319..1f0e770a93 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -879,6 +879,26 @@ class DirtyTest < ActiveRecord::TestCase raise "changed? should be false" if changed? raise "has_changes_to_save? should be false" if has_changes_to_save? raise "saved_changes? should be true" unless saved_changes? + raise "id_in_database should not be nil" if id_in_database.nil? + end + end + + person = klass.create!(first_name: "Sean") + assert_not_predicate person, :changed? + end + + test "changed? in around callbacks after yield returns false" do + klass = Class.new(ActiveRecord::Base) do + self.table_name = "people" + + around_create :check_around + + def check_around + yield + raise "changed? should be false" if changed? + raise "has_changes_to_save? should be false" if has_changes_to_save? + raise "saved_changes? should be true" unless saved_changes? + raise "id_in_database should not be nil" if id_in_database.nil? end end -- cgit v1.2.3 From 12fadea8aee3981654149d6e8ff5099bca31c679 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Fri, 31 Aug 2018 16:19:58 +0900 Subject: Remove redundant `travel_back` Since #29860, `travel_back` automatically called at the end of the test. --- activerecord/test/cases/dirty_test.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activerecord/test/cases/dirty_test.rb') diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 1f0e770a93..b1ebd20d6b 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -567,8 +567,6 @@ class DirtyTest < ActiveRecord::TestCase assert_not_nil pirate.previous_changes["updated_on"][1] assert_not pirate.previous_changes.key?("parrot_id") assert_not pirate.previous_changes.key?("created_on") - ensure - travel_back end class Testings < ActiveRecord::Base; end -- cgit v1.2.3