aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG5
-rwxr-xr-xactiverecord/lib/active_record/associations.rb36
-rwxr-xr-xactiverecord/test/associations_test.rb31
-rwxr-xr-xactiverecord/test/deprecated_associations_test.rb11
-rw-r--r--activerecord/test/fixtures/accounts.yml5
-rw-r--r--activerecord/test/fixtures/companies.yml12
-rwxr-xr-xactiverecord/test/fixtures/company.rb8
-rwxr-xr-xactiverecord/test/inheritance_test.rb2
8 files changed, 88 insertions, 22 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 9e5d24d9c6..ae9e5668be 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,4 +1,7 @@
-*SVN*
+*SVN*
+=======
+
+* :dependent now accepts :nullify option. Sets the foreign key of the related objects to NULL instead of deleting them. #2015 [Robby Russell <robby@planetargon.com>]
* Introduce read-only records. If you call object.readonly! then it will mark the object as read-only and raise ReadOnlyRecord if you call object.save. object.readonly? reports whether the object is read-only. Passing :readonly => true to any finder method will mark returned records as read-only. The :joins option now implies :readonly, so if you use this option, saving the same record will now fail. Use find_by_sql to work around.
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 075d3d8c44..39060ae972 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -265,7 +265,9 @@ module ActiveRecord
# * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
# of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_many association will use "person_id"
# as the default foreign_key.
- # * <tt>:dependent</tt> - if set to true all the associated object are destroyed alongside this object.
+ # * <tt>:dependent</tt> - if set to :destroy (or true) all the associated objects are destroyed
+ # alongside this object. Also accepts :nullify which will set the associated objects foriegn key
+ # field to NULL.
# May not be set if :exclusively_dependent is also set.
# * <tt>:exclusively_dependent</tt> - if set to true all the associated object are deleted in one SQL statement without having their
# before_destroy callback run. This should only be used on associations that depend solely on this class and don't need to do any
@@ -297,13 +299,22 @@ module ActiveRecord
require_association_class(association_class_name)
- if options[:dependent] and options[:exclusively_dependent]
- raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options. You may specify one or the other.' # ' ruby-mode
+ raise ArgumentError, ':dependent and :exclusively_dependent are mutually exclusive options. You may specify one or the other.' if options[:dependent] and options[:exclusively_dependent]
+
# See HasManyAssociation#delete_records. Dependent associations
# delete children, otherwise foreign key is set to NULL.
- elsif options[:dependent]
- module_eval "before_destroy '#{association_name}.each { |o| o.destroy }'"
- elsif options[:exclusively_dependent]
+ case options[:dependent]
+ when :destroy, true
+ module_eval "before_destroy '#{association_name}.each { |o| o.destroy }'"
+ when :nullify
+ module_eval "before_destroy { |record| #{association_class_name}.update_all(%(#{association_class_primary_key_name} = NULL), %(#{association_class_primary_key_name} = \#{record.quoted_id})) }"
+ when nil, false
+ # pass
+ else
+ raise ArgumentError, 'The :dependent option expects either true, :destroy or :nullify'
+ end
+
+ if options[:exclusively_dependent]
module_eval "before_destroy { |record| #{association_class_name}.delete_all(%(#{association_class_primary_key_name} = \#{record.quoted_id})) }"
end
@@ -353,7 +364,7 @@ module ActiveRecord
# sql fragment, such as "rank = 5".
# * <tt>:order</tt> - specify the order from which the associated object will be picked at the top. Specified as
# an "ORDER BY" sql fragment, such as "last_name, first_name DESC"
- # * <tt>:dependent</tt> - if set to true, the associated object is destroyed when this object is. It's also destroyed if another
+ # * <tt>:dependent</tt> - if set to :destroy (or true) all the associated object is destroyed when this object is. Also
# association is assigned.
# * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
# of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_one association will use "person_id"
@@ -386,7 +397,16 @@ module ActiveRecord
association_constructor_method(:build, association_name, association_class_name, association_class_primary_key_name, options, HasOneAssociation)
association_constructor_method(:create, association_name, association_class_name, association_class_primary_key_name, options, HasOneAssociation)
- module_eval "before_destroy '#{association_name}.destroy unless #{association_name}.nil?'" if options[:dependent]
+ case options[:dependent]
+ when :destroy, true
+ module_eval "before_destroy '#{association_name}.destroy unless #{association_name}.nil?'"
+ when :nullify
+ module_eval "before_destroy '#{association_name}.update_attribute(\"#{association_class_primary_key_name}\", nil)'"
+ when nil, false
+ # pass
+ else
+ raise ArgumentError, "The :dependent option expects either :destroy or :nullify."
+ end
# deprecated api
deprecated_has_association_method(association_name)
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 5cafa010f9..bfe797b167 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -132,10 +132,11 @@ class HasOneAssociationsTest < Test::Unit::TestCase
end
def test_dependence
+ num_accounts = Account.count
firm = Firm.find(1)
assert !firm.account.nil?
- firm.destroy
- assert_equal 1, Account.count
+ firm.destroy
+ assert_equal num_accounts - 1, Account.count
end
def test_succesful_build_association
@@ -563,7 +564,7 @@ class HasManyAssociationsTest < Test::Unit::TestCase
# Should be destroyed since the association is exclusively dependent.
assert Client.find_by_id(client_id).nil?
- end
+ end
def test_deleting_a_item_which_is_not_in_the_collection
force_signal37_to_load_all_clients_of_firm
@@ -634,9 +635,26 @@ class HasManyAssociationsTest < Test::Unit::TestCase
end
def test_dependence_on_account
- assert_equal 2, Account.count
+ num_accounts = Account.count
companies(:first_firm).destroy
- assert_equal 1, Account.count
+ assert_equal num_accounts - 1, Account.count
+ end
+
+
+ def test_depends_and_nullify
+ num_accounts = Account.count
+ num_companies = Company.count
+
+ core = companies(:rails_core)
+ assert_equal accounts(:rails_core_account), core.account
+ assert_equal [companies(:leetsoft), companies(:jadedpixel)], core.companies
+ core.destroy
+ assert_nil accounts(:rails_core_account).reload.firm_id
+ assert_nil companies(:leetsoft).reload.client_of
+ assert_nil companies(:jadedpixel).reload.client_of
+
+
+ assert_equal num_accounts, Account.count
end
def test_included_in_collection
@@ -658,7 +676,8 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert firm.save, "Could not save firm"
firm.reload
assert_equal 1, firm.clients.length
- end
+ end
+
def test_replace_with_new
firm = Firm.find(:first)
diff --git a/activerecord/test/deprecated_associations_test.rb b/activerecord/test/deprecated_associations_test.rb
index 1d5908d774..d96a587dc0 100755
--- a/activerecord/test/deprecated_associations_test.rb
+++ b/activerecord/test/deprecated_associations_test.rb
@@ -79,10 +79,11 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
end
def test_has_one_dependence
+ num_accounts = Account.count
firm = Firm.find(1)
assert firm.has_account?
- firm.destroy
- assert_equal 1, Account.find_all.length
+ firm.destroy
+ assert_equal num_accounts - 1, Account.count
end
def test_has_one_dependence_with_missing_association
@@ -124,10 +125,10 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase
assert !Account.find(2).firm?(companies(:first_firm)), "Unknown isn't linked"
end
- def test_has_many_dependence_on_account
- assert_equal 2, Account.find_all.length
+ def test_has_many_dependence_on_account
+ num_accounts = Account.count
companies(:first_firm).destroy
- assert_equal 1, Account.find_all.length
+ assert_equal num_accounts - 1, Account.count
end
def test_find_in
diff --git a/activerecord/test/fixtures/accounts.yml b/activerecord/test/fixtures/accounts.yml
index 21a0aab52a..8cff4389fd 100644
--- a/activerecord/test/fixtures/accounts.yml
+++ b/activerecord/test/fixtures/accounts.yml
@@ -6,3 +6,8 @@ signals37:
unknown:
id: 2
credit_limit: 50
+
+rails_core_account:
+ id: 3
+ firm_id: 6
+ credit_limit: 50
diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml
index 7d13bc6b75..29b949e16b 100644
--- a/activerecord/test/fixtures/companies.yml
+++ b/activerecord/test/fixtures/companies.yml
@@ -33,3 +33,15 @@ another_client:
client_of: 4
name: Ex Nihilo
ruby_type: Client
+
+rails_core:
+ id: 6
+ type: DependentFirm
+
+leetsoft:
+ id: 7
+ client_of: 6
+
+jadedpixel:
+ id: 8
+ client_of: 6 \ No newline at end of file
diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb
index 748d43eb2e..4a0c96bdd3 100755
--- a/activerecord/test/fixtures/company.rb
+++ b/activerecord/test/fixtures/company.rb
@@ -27,6 +27,12 @@ class Firm < Company
has_one :account, :foreign_key => "firm_id", :dependent => true
end
+class DependentFirm < Company
+ has_one :account, :foreign_key => "firm_id", :dependent => :nullify
+ has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify
+end
+
+
class Client < Company
belongs_to :firm, :foreign_key => "client_of"
belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
@@ -62,4 +68,4 @@ class Account < ActiveRecord::Base
def validate
errors.add_on_empty "credit_limit"
end
-end
+end \ No newline at end of file
diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb
index 11a2c3d7a9..a3b9ce25ad 100755
--- a/activerecord/test/inheritance_test.rb
+++ b/activerecord/test/inheritance_test.rb
@@ -57,7 +57,7 @@ class InheritanceTest < Test::Unit::TestCase
end
def test_inheritance_condition
- assert_equal 5, Company.count
+ assert_equal 8, Company.count
assert_equal 2, Firm.count
assert_equal 3, Client.count
end