Skip to content

Commit

Permalink
Merge pull request #4829 from rafaelfranca/restrict-fix
Browse files Browse the repository at this point in the history
Use human attribute name to show the dependent destroy message
  • Loading branch information
tenderlove committed Feb 1, 2012
2 parents 403cce7 + e778e22 commit 33d8b62
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def define_restrict_dependency_method
raise ActiveRecord::DeleteRestrictionError.new(name)
else
key = association(name).reflection.macro == :has_one ? "one" : "many"
errors.add(:base, :"restrict_dependent_destroy.#{key}", :record => name)
errors.add(:base, :"restrict_dependent_destroy.#{key}",
:record => self.class.human_attribute_name(name).downcase)
return false
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ en:
taken: "has already been taken"
record_invalid: "Validation failed: %{errors}"
restrict_dependent_destroy:
one: "Cannot delete record because a dependent %{record} exists"
one: "Cannot delete record because a dependent %{record} exist"
many: "Cannot delete record because dependent %{record} exist"
# Append your own errors here or at the model/attributes scope.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,37 @@ def test_dependence_with_restrict_with_dependent_restrict_raises_config_set_to_f
firm.destroy

assert !firm.errors.empty?
assert_equal "Cannot delete record because a dependent account exists", firm.errors[:base].first
assert_equal "Cannot delete record because a dependent account exist", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.account.present?
ensure
ActiveRecord::Base.dependent_restrict_raises = option_before
end

def test_dependence_with_restrict_with_dependent_restrict_raises_config_set_to_false_and_attribute_name
old_backend = I18n.backend
I18n.backend = I18n::Backend::Simple.new
I18n.backend.store_translations 'en', :activerecord => {:attributes => {:restricted_firm => {:account => "account model"}}}

option_before = ActiveRecord::Base.dependent_restrict_raises
ActiveRecord::Base.dependent_restrict_raises = false

firm = RestrictedFirm.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 account model exist", firm.errors[:base].first
assert RestrictedFirm.exists?(:name => 'restrict')
assert firm.account.present?
ensure
ActiveRecord::Base.dependent_restrict_raises = option_before
I18n.backend = old_backend
end

def test_successful_build_association
firm = Firm.new("name" => "GlobalMegaCorp")
firm.save
Expand Down
1 change: 0 additions & 1 deletion railties/test/application/paths_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def setup
app.config.session_store nil
end
RUBY
use_frameworks [:action_controller, :action_view, :action_mailer, :active_record]
require "#{app_path}/config/environment"
@paths = Rails.application.config.paths
end
Expand Down
2 changes: 1 addition & 1 deletion railties/test/isolation/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def use_frameworks(arr)
:activemodel,
:activerecord,
:activeresource] - arr
remove_from_config "config.active_record.identity_map = true" if to_remove.include? :activerecord
remove_from_config "config.active_record.dependent_restrict_raises = false" if to_remove.include? :activerecord
$:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
end

Expand Down

0 comments on commit 33d8b62

Please sign in to comment.