Skip to content

Commit

Permalink
protected :can_save?
Browse files Browse the repository at this point in the history
And tweak tests added at #41714.
  • Loading branch information
kamipo committed Mar 24, 2021
1 parent 35d3923 commit 8cd3b65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
7 changes: 4 additions & 3 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -288,9 +288,10 @@ def saving? #:nodoc:
@saving
end

def can_save? #:nodoc:
!destroyed? && !saving?
end
protected
def can_save? # :nodoc:
!destroyed? && !saving?
end

private
# Track if this record is being saved. If it is being saved we
Expand Down
33 changes: 15 additions & 18 deletions activerecord/test/cases/autosave_association_test.rb
Expand Up @@ -1953,10 +1953,6 @@ def test_should_update_children_when_association_redefined_in_subclass
end

class TestCyclicAutosaveAssociationsOnlySaveOnce < ActiveRecord::TestCase
teardown do
$autosave_saving_stack = []
end

test "child is saved only once if child is the inverse has_one of parent" do
ship_reflection = Ship.reflect_on_association(:pirate)
pirate_reflection = Pirate.reflect_on_association(:ship)
Expand All @@ -1965,35 +1961,35 @@ class TestCyclicAutosaveAssociationsOnlySaveOnce < ActiveRecord::TestCase
child = Ship.new(name: "Nights Dirty Lightning")
parent = child.build_pirate(catchphrase: "Aye")
child.save!
assert child.previously_new_record?
assert parent.previously_new_record?
assert_predicate child, :previously_new_record?
assert_predicate parent, :previously_new_record?
end

test "child is saved only once if child is an inverse has_many of parent" do
child = FamousShip.new(name: "Poison Orchid")
parent = child.build_famous_pirate(catchphrase: "Aye")
child.save!
assert child.previously_new_record?
assert parent.previously_new_record?
assert_predicate child, :previously_new_record?
assert_predicate parent, :previously_new_record?
end

test "similar children are saved in the autosave" do
child1 = FamousShip.new(name: "Poison Orchid")
parent = child1.build_famous_pirate(catchphrase: "Aye")
child2 = parent.famous_ships.build(name: "Red Messenger")
child1.save!
assert child2.persisted?
assert child1.previously_new_record?
assert parent.previously_new_record?
assert_predicate child2, :persisted?
assert_predicate child1, :previously_new_record?
assert_predicate parent, :previously_new_record?
assert_equal [child2, child1], parent.reload.famous_ships
end

test "parent is saved only once" do
child = Ship.new(name: "Nights Dirty Lightning")
parent = child.build_pirate(catchphrase: "Aye")
parent.save!
assert child.previously_new_record?
assert parent.previously_new_record?
assert_predicate child, :previously_new_record?
assert_predicate parent, :previously_new_record?
end

test "saving? is reset to false if validations fail" do
Expand All @@ -2004,9 +2000,11 @@ class TestCyclicAutosaveAssociationsOnlySaveOnce < ActiveRecord::TestCase
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
Expand All @@ -2017,9 +2015,8 @@ class TestCyclicAutosaveAssociationsOnlySaveOnce < ActiveRecord::TestCase

child = ship_with_saving_stack.new(name: "Nights Dirty Lightning")
child.pirate = pirate_with_callbacks.new(catchphrase: "Aye")
$autosave_saving_stack = []
child.save!
assert_equal [true] * 8, $autosave_saving_stack
assert_equal false, child.saving?
assert_equal [true] * 8, autosave_saving_stack
assert_not_predicate child, :saving?
end
end

0 comments on commit 8cd3b65

Please sign in to comment.