diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-09-28 03:52:57 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-09-28 03:52:57 +0000 |
commit | efaf2af07c4238e911899988871e575d8ef5805b (patch) | |
tree | a70ca8cfb227cb6aa47ac68fea84641fc4bab28d /activerecord/test | |
parent | 1e87d6e887639650e23db5fbba0ccdf0536f289c (diff) | |
download | rails-efaf2af07c4238e911899988871e575d8ef5805b.tar.gz rails-efaf2af07c4238e911899988871e575d8ef5805b.tar.bz2 rails-efaf2af07c4238e911899988871e575d8ef5805b.zip |
r3653@asus: jeremy | 2005-09-28 00:23:49 -0700
Ticket 2221 - model.association.clear should destroy associated objects if :dependent => true instead of nullifying their foreign keys
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2384 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/associations_test.rb | 65 | ||||
-rwxr-xr-x | activerecord/test/fixtures/company.rb | 16 |
2 files changed, 69 insertions, 12 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 2bbccb5c9e..d0b560d112 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -23,7 +23,7 @@ raise "ActiveRecord should have barked on bad collection keys" unless bad_collec class AssociationsTest < Test::Unit::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects, :computers - + def test_force_reload firm = Firm.new("name" => "A New Firm, Inc") firm.save @@ -257,11 +257,15 @@ end class HasManyAssociationsTest < Test::Unit::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects, :topics - + + def setup + Client.destroyed_client_ids.clear + end + def force_signal37_to_load_all_clients_of_firm companies(:first_firm).clients_of_firm.each {|f| } end - + def test_counting assert_equal 2, Firm.find(:first).clients.count end @@ -316,7 +320,6 @@ class HasManyAssociationsTest < Test::Unit::TestCase else assert false, "belongs_to failed unless check" end - end def test_find_ids @@ -499,19 +502,57 @@ class HasManyAssociationsTest < Test::Unit::TestCase force_signal37_to_load_all_clients_of_firm companies(:first_firm).clients_of_firm.create("name" => "Another Client") assert_equal 2, companies(:first_firm).clients_of_firm.size - #companies(:first_firm).clients_of_firm.clear companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]]) assert_equal 0, companies(:first_firm).clients_of_firm.size assert_equal 0, companies(:first_firm).clients_of_firm(true).size end - def test_deleting_a_association_collection - force_signal37_to_load_all_clients_of_firm - companies(:first_firm).clients_of_firm.create("name" => "Another Client") - assert_equal 2, companies(:first_firm).clients_of_firm.size - companies(:first_firm).clients_of_firm.clear - assert_equal 0, companies(:first_firm).clients_of_firm.size - assert_equal 0, companies(:first_firm).clients_of_firm(true).size + def test_clearing_an_association_collection + firm = companies(:first_firm) + client_id = firm.clients_of_firm.first.id + assert_equal 1, firm.clients_of_firm.size + + firm.clients_of_firm.clear + + assert_equal 0, firm.clients_of_firm.size + assert_equal 0, firm.clients_of_firm(true).size + assert_equal [], Client.destroyed_client_ids[firm.id] + + # Should not be destroyed since the association is not dependent. + assert_not_nil Client.find_by_id(client_id) + end + + def test_clearing_a_dependent_association_collection + firm = companies(:first_firm) + client_id = firm.dependent_clients_of_firm.first.id + assert_equal 1, firm.dependent_clients_of_firm.size + + # :dependent means destroy is called on each client + firm.dependent_clients_of_firm.clear + + assert_equal 0, firm.dependent_clients_of_firm.size + assert_equal 0, firm.dependent_clients_of_firm(true).size + assert_equal [client_id], Client.destroyed_client_ids[firm.id] + + # Should be destroyed since the association is dependent. + assert Client.find_by_id(client_id).nil? + end + + def test_clearing_an_exclusively_dependent_association_collection + firm = companies(:first_firm) + client_id = firm.exclusively_dependent_clients_of_firm.first.id + assert_equal 1, firm.exclusively_dependent_clients_of_firm.size + + # :exclusively_dependent means each client is deleted directly from + # the database without looping through them calling destroy. + firm.exclusively_dependent_clients_of_firm.clear + + assert_equal 0, firm.exclusively_dependent_clients_of_firm.size + assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size + assert_equal [], Client.destroyed_client_ids[firm.id] + + # Should be destroyed since the association is exclusively dependent. + assert Client.find_by_id(client_id).nil? end def test_deleting_a_item_which_is_not_in_the_collection diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb index 5af39c1297..748d43eb2e 100755 --- a/activerecord/test/fixtures/company.rb +++ b/activerecord/test/fixtures/company.rb @@ -10,6 +10,8 @@ class Firm < Company has_many :clients, :order => "id", :dependent => true, :counter_sql => "SELECT COUNT(*) FROM companies WHERE firm_id = 1 AND (type = 'Client' OR type = 'SpecialClient' OR type = 'VerySpecialClient' )" has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC" has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id" + has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => true + has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :exclusively_dependent => true has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id" has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}' has_many :clients_using_counter_sql, :class_name => "Client", @@ -30,6 +32,20 @@ class Client < Company belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id" belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of" belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => "1 = 1" + + # Record destruction so we can test whether firm.clients.clear has + # is calling client.destroy, deleting from the database, or setting + # foreign keys to NULL. + def self.destroyed_client_ids + @destroyed_client_ids ||= Hash.new { |h,k| h[k] = [] } + end + + before_destroy do |client| + if client.firm + Client.destroyed_client_ids[client.firm.id] << client.id + end + true + end end |