Permalink
Browse files

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
  • Loading branch information...
jeremy committed Sep 28, 2005
1 parent 1e87d6e commit efaf2af07c4238e911899988871e575d8ef5805b
View
@@ -1,5 +1,7 @@
*SVN*
+* model.association.clear should destroy associated objects if :dependent => true instead of nullifying their foreign keys. #2221 [joergd@pobox.com, ObieFernandez <obiefernandez@gmail.com>]
+
* Returning false from before_destroy should cancel the action. #1829 [Jeremy Huffman]
* Recognize PostgreSQL NOW() default as equivalent to CURRENT_TIMESTAMP or CURRENT_DATE, depending on the column's type. #2256 [mat <mat@absolight.fr>]
@@ -227,7 +227,9 @@ module ClassMethods
# This will also destroy the objects if they're declared as belongs_to and dependent on this model.
# * <tt>collection=objects</tt> - replaces the collections content by deleting and adding objects as appropriate.
# * <tt>collection_singular_ids=ids</tt> - replace the collection by the objects identified by the primary keys in +ids+
- # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects.
+ # * <tt>collection.clear</tt> - removes every object from the collection. This destroys the associated objects if they
+ # are <tt>:dependent</tt>, deletes them directly from the database if they are <tt>:exclusively_dependent</tt>,
+ # and sets their foreign keys to NULL otherwise.
# * <tt>collection.empty?</tt> - returns true if there are no associated objects.
# * <tt>collection.size</tt> - returns the number of associated objects.
# * <tt>collection.find</tt> - finds an associated object according to the same rules as Base.find.
@@ -87,7 +87,13 @@ def find(*args)
# Removes all records from this association. Returns +self+ so
# method calls may be chained.
def clear
- @association_class.update_all("#{@association_class_primary_key_name} = NULL", "#{@association_class_primary_key_name} = #{@owner.quoted_id}")
+ if @options[:dependent]
+ each { |associate| associate.destroy }
+ elsif @options[:exclusively_dependent]
+ @association_class.delete_all("#{@association_class_primary_key_name} = #{@owner.quoted_id}")
+ else
+ @association_class.update_all("#{@association_class_primary_key_name} = NULL", "#{@association_class_primary_key_name} = #{@owner.quoted_id}")
+ end
@target = []
self
end
@@ -23,7 +23,7 @@ class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
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 @@ def test_assignment_before_either_saved
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 @@ def test_belongs_to_sanity
else
assert false, "belongs_to failed unless check"
end
-
end
def test_find_ids
@@ -499,19 +502,57 @@ def test_deleting_a_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
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
@@ -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

0 comments on commit efaf2af

Please sign in to comment.