mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #21581 from ronakjangir47/restrict_with_error
`restrict_with_error` message will now respect owner’s human name
This commit is contained in:
commit
9feda8f211
5 changed files with 44 additions and 2 deletions
|
@ -1,3 +1,8 @@
|
|||
* Lookup the attribute name for `restrict_with_error` messages on the
|
||||
model class that defines the association.
|
||||
|
||||
*kuboon*, *Ronak Jangir*
|
||||
|
||||
* Correct query for PostgreSQL 8.2 compatibility.
|
||||
|
||||
*Ben Murphy*, *Matthew Draper*
|
||||
|
|
|
@ -15,7 +15,7 @@ module ActiveRecord
|
|||
|
||||
when :restrict_with_error
|
||||
unless empty?
|
||||
record = klass.human_attribute_name(reflection.name).downcase
|
||||
record = owner.class.human_attribute_name(reflection.name).downcase
|
||||
message = owner.errors.generate_message(:base, :'restrict_dependent_destroy.many', record: record, raise: true) rescue nil
|
||||
if message
|
||||
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
|
||||
|
|
|
@ -11,7 +11,7 @@ module ActiveRecord
|
|||
|
||||
when :restrict_with_error
|
||||
if load_target
|
||||
record = klass.human_attribute_name(reflection.name).downcase
|
||||
record = owner.class.human_attribute_name(reflection.name).downcase
|
||||
message = owner.errors.generate_message(:base, :'restrict_dependent_destroy.one', record: record, raise: true) rescue nil
|
||||
if message
|
||||
ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
|
||||
|
|
|
@ -1482,6 +1482,25 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
assert firm.companies.exists?(:name => 'child')
|
||||
end
|
||||
|
||||
def test_restrict_with_error_with_locale
|
||||
I18n.backend = I18n::Backend::Simple.new
|
||||
I18n.backend.store_translations 'en', activerecord: {attributes: {restricted_with_error_firm: {companies: 'client companies'}}}
|
||||
firm = RestrictedWithErrorFirm.create!(name: 'restrict')
|
||||
firm.companies.create(name: 'child')
|
||||
|
||||
assert !firm.companies.empty?
|
||||
|
||||
firm.destroy
|
||||
|
||||
assert !firm.errors.empty?
|
||||
|
||||
assert_equal "Cannot delete record because dependent client companies exist", firm.errors[:base].first
|
||||
assert RestrictedWithErrorFirm.exists?(name: 'restrict')
|
||||
assert firm.companies.exists?(name: 'child')
|
||||
ensure
|
||||
I18n.backend.reload!
|
||||
end
|
||||
|
||||
def test_included_in_collection
|
||||
assert_equal true, companies(:first_firm).clients.include?(Client.find(2))
|
||||
end
|
||||
|
|
|
@ -219,6 +219,24 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
|
|||
assert firm.account.present?
|
||||
end
|
||||
|
||||
def test_restrict_with_error_with_locale
|
||||
I18n.backend = I18n::Backend::Simple.new
|
||||
I18n.backend.store_translations 'en', activerecord: {attributes: {restricted_with_error_firm: {account: 'firm account'}}}
|
||||
firm = RestrictedWithErrorFirm.create!(name: 'restrict')
|
||||
firm.create_account(credit_limit: 10)
|
||||
|
||||
assert_not_nil firm.account
|
||||
|
||||
firm.destroy
|
||||
|
||||
assert !firm.errors.empty?
|
||||
assert_equal "Cannot delete record because a dependent firm account exists", firm.errors[:base].first
|
||||
assert RestrictedWithErrorFirm.exists?(name: 'restrict')
|
||||
assert firm.account.present?
|
||||
ensure
|
||||
I18n.backend.reload!
|
||||
end
|
||||
|
||||
def test_successful_build_association
|
||||
firm = Firm.new("name" => "GlobalMegaCorp")
|
||||
firm.save
|
||||
|
|
Loading…
Reference in a new issue