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.
This commit is contained in:
Ryuta Kamizono 2021-03-25 02:41:44 +09:00
parent 7702ce8243
commit 2eb5458978
5 changed files with 13 additions and 13 deletions

View File

@ -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*

View File

@ -233,9 +233,9 @@ module ActiveRecord
# Reloads the attributes of the object as usual and clears <tt>marked_for_destruction</tt> 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

View File

@ -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

View File

@ -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]

View File

@ -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