Skip to content

Commit

Permalink
Explicitely setting `autosave => false' should override new_record au…
Browse files Browse the repository at this point in the history
…tosaving. [#2214 state:resolved]

Original author is Jacob.
  • Loading branch information
alloy committed Sep 12, 2009
1 parent 55bc0c7 commit 2420d62
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/autosave_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def save_collection_association(reflection)
records.each do |record|
if autosave && record.marked_for_destruction?
association.destroy(record)
elsif @new_record_before_save || record.new_record?
elsif autosave != false && (@new_record_before_save || record.new_record?)
if autosave
association.send(:insert_record, record, false, false)
else
Expand Down Expand Up @@ -318,7 +318,7 @@ def save_has_one_association(reflection)

if autosave && association.marked_for_destruction?
association.destroy
elsif new_record? || association.new_record? || association[reflection.primary_key_name] != id || autosave
elsif autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != id || autosave)
association[reflection.primary_key_name] = id
association.save(!autosave)
end
Expand All @@ -339,7 +339,7 @@ def save_belongs_to_association(reflection)

if autosave && association.marked_for_destruction?
association.destroy
else
elsif autosave != false
association.save(!autosave) if association.new_record? || autosave

if association.updated?
Expand Down
64 changes: 64 additions & 0 deletions activerecord/test/cases/autosave_association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,70 @@ def test_replace_on_new_object
end
end

class TestDefaultAutosaveAssociationOnNewRecord < ActiveRecord::TestCase
def test_autosave_new_record_on_belongs_to_can_be_disabled_per_relationship
new_account = Account.new("credit_limit" => 1000)
new_firm = Firm.new("name" => "some firm")

assert new_firm.new_record?
new_account.firm = new_firm
new_account.save!

assert !new_firm.new_record?

new_account = Account.new("credit_limit" => 1000)
new_autosaved_firm = Firm.new("name" => "some firm")

assert new_autosaved_firm.new_record?
new_account.unautosaved_firm = new_autosaved_firm
new_account.save!

assert new_autosaved_firm.new_record?
end

def test_autosave_new_record_on_has_one_can_be_disabled_per_relationship
firm = Firm.new("name" => "some firm")
account = Account.new("credit_limit" => 1000)

assert account.new_record?
firm.account = account
firm.save!

assert !account.new_record?

firm = Firm.new("name" => "some firm")
account = Account.new("credit_limit" => 1000)

firm.unautosaved_account = account

assert account.new_record?
firm.unautosaved_account = account
firm.save!

assert account.new_record?
end

def test_autosave_new_record_on_has_many_can_be_disabled_per_relationship
firm = Firm.new("name" => "some firm")
account = Account.new("credit_limit" => 1000)

assert account.new_record?
firm.accounts << account

firm.save!
assert !account.new_record?

firm = Firm.new("name" => "some firm")
account = Account.new("credit_limit" => 1000)

assert account.new_record?
firm.unautosaved_accounts << account

firm.save!
assert account.new_record?
end
end

class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
self.use_transactional_fixtures = false

Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/reflection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def test_association_reflection_in_modules

def test_reflection_of_all_associations
# FIXME these assertions bust a lot
assert_equal 30, Firm.reflect_on_all_associations.size
assert_equal 23, Firm.reflect_on_all_associations(:has_many).size
assert_equal 7, Firm.reflect_on_all_associations(:has_one).size
assert_equal 33, Firm.reflect_on_all_associations.size
assert_equal 25, Firm.reflect_on_all_associations(:has_many).size
assert_equal 8, Firm.reflect_on_all_associations(:has_one).size
assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
end

Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class Firm < Company
has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account"
has_one :deletable_account, :foreign_key => "firm_id", :class_name => "Account", :dependent => :delete

has_one :unautosaved_account, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
has_many :accounts
has_many :unautosaved_accounts, :foreign_key => "firm_id", :class_name => 'Account', :autosave => false
end

class DependentFirm < Company
Expand Down Expand Up @@ -136,6 +140,7 @@ class VerySpecialClient < SpecialClient

class Account < ActiveRecord::Base
belongs_to :firm
belongs_to :unautosaved_firm, :foreign_key => "firm_id", :class_name => "Firm", :autosave => false

def self.destroyed_account_ids
@destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] }
Expand Down

0 comments on commit 2420d62

Please sign in to comment.