1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

suggested fixes for :dependent => :restrict deprecation.

This commit is contained in:
Manoj 2012-01-31 13:46:07 +05:30
parent 3053f52671
commit 23074c81a5
5 changed files with 16 additions and 36 deletions

View file

@ -1097,7 +1097,8 @@ module ActiveRecord
# alongside this object by calling their +destroy+ method. If set to <tt>:delete_all</tt> all associated
# objects are deleted *without* calling their +destroy+ method. If set to <tt>:nullify</tt> all associated
# objects' foreign keys are set to +NULL+ *without* calling their +save+ callbacks. If set to
# <tt>:restrict</tt> this object cannot be deleted if it has any associated objects.
# <tt>:restrict</tt> an error will be added to the object, preventing its deletion, if any associated
# objects are present.
#
# If using with the <tt>:through</tt> option, the association on the join model must be
# a +belongs_to+, and the records which get deleted are the join records, rather than
@ -1250,7 +1251,8 @@ module ActiveRecord
# If set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to
# <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method.
# If set to <tt>:nullify</tt>, the associated object's foreign key is set to +NULL+.
# If set to <tt>:restrict</tt>, this object cannot be deleted if it has any associated object.
# If set to <tt>:restrict</tt>, an error will be added to the object, preventing its deletion, if an
# associated object is present.
# [:foreign_key]
# 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

View file

@ -74,7 +74,7 @@ module ActiveRecord::Associations::Builder
if dependent_restrict_raises?
raise ActiveRecord::DeleteRestrictionError.new(name)
else
errors.add(:base, :restrict_dependent_destroy, :model => name)
errors.add(:base, :restrict_dependent_destroy, :model => name.to_s.singularize)
return false
end
end

View file

@ -10,7 +10,7 @@ en:
messages:
taken: "has already been taken"
record_invalid: "Validation failed: %{errors}"
restrict_dependent_destroy: "Cannot delete record because dependent %{model} exist"
restrict_dependent_destroy: "Cannot delete record because dependent %{model} exists"
# Append your own errors here or at the model/attributes scope.
# You can define own errors for models or model attributes.

View file

@ -1170,7 +1170,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert !firm.errors.empty?
assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
assert_equal "Cannot delete record because dependent company exists", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.companies.exists?(:name => 'child')
ensure
@ -1676,20 +1676,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_building_has_many_association_with_restrict_dependency
assert_deprecated do
class_eval <<-EOF
class RestrictedFirm < ActiveRecord::Base
has_many :companies, :dependent => :restrict
end
EOF
end
assert_not_deprecated do
class_eval <<-EOF
class Firm < ActiveRecord::Base
has_many :companies
end
EOF
end
klass = Class.new(ActiveRecord::Base)
assert_deprecated { klass.has_many :companies, :dependent => :restrict }
assert_not_deprecated { klass.has_many :companies }
end
end

View file

@ -183,7 +183,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
firm.destroy
assert !firm.errors.empty?
assert_equal "Cannot delete record because dependent account exist", firm.errors[:base].first
assert_equal "Cannot delete record because dependent account exists", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.account.present?
ensure
@ -484,20 +484,9 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
end
def test_building_has_one_association_with_dependent_restrict
assert_deprecated do
class_eval <<-EOF
class RestrictedFirm < ActiveRecord::Base
has_one :account, :dependent => :restrict
end
EOF
end
assert_not_deprecated do
class_eval <<-EOF
class Firm < ActiveRecord::Base
has_one :account
end
EOF
end
klass = Class.new(ActiveRecord::Base)
assert_deprecated { klass.has_one :account, :dependent => :restrict }
assert_not_deprecated { klass.has_one :account }
end
end