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

fix has_one, has_many restrict error message

This commit is contained in:
Manoj 2012-02-01 23:55:01 +05:30
parent 049dc500b6
commit 907d19343e
4 changed files with 7 additions and 4 deletions

View file

@ -74,7 +74,8 @@ module ActiveRecord::Associations::Builder
if dependent_restrict_raises?
raise ActiveRecord::DeleteRestrictionError.new(name)
else
errors.add(:base, :restrict_dependent_destroy, :model => name.to_s.singularize)
key = association(name).reflection.macro == :has_one ? "one" : "many"
errors.add(:base, :"restrict_dependent_destroy.#{key}", :record => name)
return false
end
end

View file

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

View file

@ -1171,7 +1171,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert !firm.errors.empty?
assert_equal "Cannot delete record because dependent company exists", firm.errors[:base].first
assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.companies.exists?(:name => 'child')
ensure

View file

@ -184,7 +184,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
firm.destroy
assert !firm.errors.empty?
assert_equal "Cannot delete record because dependent account exists", firm.errors[:base].first
assert_equal "Cannot delete record because a dependent account exists", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.account.present?
ensure