提交 70869931 编写于 作者: T Tobias Lütke

Adds :nullify option to :depends. Closes #2015 (Robby Russell)


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2595 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 64fcb752
*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.
......
......@@ -265,7 +265,9 @@ module ClassMethods
# * <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 @@ def has_many(association_id, options = {})
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 @@ def has_many(association_id, options = {})
# 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 @@ def has_one(association_id, options = {})
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)
......
......@@ -132,10 +132,11 @@ def test_assignment_without_replacement_on_create
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 @@ def test_clearing_an_exclusively_dependent_association_collection
# 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 @@ def test_dependence_with_transaction_support_on_failure
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 @@ def test_replace_with_less
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)
......
......@@ -79,10 +79,11 @@ def test_has_many_dependence_with_transaction_support_on_failure
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 @@ def test_has_one
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
......
......@@ -6,3 +6,8 @@ signals37:
unknown:
id: 2
credit_limit: 50
rails_core_account:
id: 3
firm_id: 6
credit_limit: 50
......@@ -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
......@@ -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
......@@ -57,7 +57,7 @@ def test_alt_inheritance_save
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册