From 2eb5458978f3f993ccc414b321b35fb1aef1efd2 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Thu, 25 Mar 2021 02:41:44 +0900 Subject: [PATCH] Rename internal `@saving` state to `@_saving` Usually we add `_` prefix for newly added short term living (used) internal state (e.g. ae02898, d1107f4, dcb8259), and also `@saving` might be already used in users' code. --- activerecord/CHANGELOG.md | 6 +++--- activerecord/lib/active_record/autosave_association.rb | 8 ++++---- activerecord/lib/active_record/core.rb | 2 +- activerecord/lib/active_record/transactions.rb | 2 +- activerecord/test/cases/autosave_association_test.rb | 8 ++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index a9f4b449d6..90c86e741c 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,7 +1,7 @@ -* Prevent double saves in autosave of cyclic associations +* Prevent double saves in autosave of cyclic associations. - Adds a @saving state which tracks if a record is currently being saved. - If @saving is set to true, the record won't be saved by the autosave callbacks. + Adds an internal saving state which tracks if a record is currently being saved. + If a state is set to true, the record won't be saved by the autosave callbacks. *Petrik de Heus* diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 867cf35edf..5ce6dc4cdc 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -233,9 +233,9 @@ module ActiveRecord # Reloads the attributes of the object as usual and clears marked_for_destruction flag. def reload(options = nil) + @_saving = false @marked_for_destruction = false @destroyed_by_association = nil - @saving = false super end @@ -284,17 +284,17 @@ module ActiveRecord protected def _can_save? # :nodoc: - !destroyed? && !@saving + !destroyed? && !@_saving end private # Track if this record is being saved. If it is being saved we # can skip saving it in the autosave callbacks. def _saving - previously_saving, @saving = @saving, true + previously_saving, @_saving = @_saving, true yield ensure - @saving = previously_saving + @_saving = previously_saving end # Returns the record for an association collection that should be validated diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 1c194f91de..7691b01fcd 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -825,8 +825,8 @@ module ActiveRecord @readonly = false @previously_new_record = false @destroyed = false + @_saving = false @marked_for_destruction = false - @saving = false @destroyed_by_association = nil @_start_transaction_state = nil @strict_loading = self.class.strict_loading_by_default diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 8d48a8b6af..9080c1ea01 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -404,7 +404,7 @@ module ActiveRecord attr = attr.with_value_from_user(value) if attr.value != value attr end - @saving = false + @_saving = false @mutations_from_database = nil @mutations_before_last_save = nil if @attributes.fetch_value(@primary_key) != restore_state[:id] diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 5bde1ea8d7..b4ecff3b92 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -1996,15 +1996,15 @@ class TestCyclicAutosaveAssociationsOnlySaveOnce < ActiveRecord::TestCase child = Ship.new(name: "Nights Dirty Lightning") child.build_pirate assert_not child.save - assert_not child.instance_variable_get(:@saving) + assert_not child.instance_variable_get(:@_saving) end test "saving? is set to false after multiple nested saves" do autosave_saving_stack = [] ship_with_saving_stack = Class.new(Ship) do - before_save { autosave_saving_stack << @saving } - after_save { autosave_saving_stack << @saving } + before_save { autosave_saving_stack << @_saving } + after_save { autosave_saving_stack << @_saving } end pirate_with_callbacks = Class.new(Pirate) do @@ -2017,6 +2017,6 @@ class TestCyclicAutosaveAssociationsOnlySaveOnce < ActiveRecord::TestCase child.pirate = pirate_with_callbacks.new(catchphrase: "Aye") child.save! assert_equal [true] * 8, autosave_saving_stack - assert_not child.instance_variable_get(:@saving) + assert_not child.instance_variable_get(:@_saving) end end