Skip to content

Commit

Permalink
Prefix the method name with the class name
Browse files Browse the repository at this point in the history
instance.errors.full_messages will now produce something like:
["SomeClass: The method can't be blank"]
  • Loading branch information
bdunne committed Jul 24, 2018
1 parent d549a31 commit e385a95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ class ApplicationRecord < ActiveRecord::Base
def self.display_name(number = 1)
n_(model_name.singular.titleize, model_name.plural.titleize, number)
end

def self.human_attribute_name(attribute, *args)
"#{name}: #{super}"
end
end
13 changes: 13 additions & 0 deletions spec/models/application_record_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe ApplicationRecord do
context ".human_attribute_name puts the class name in the validation error" do
it "single level" do
Zone.create!(:name => "example", :description => "example")
expect { Zone.create!(:name => "example", :description => "example") }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Zone: Name is not unique within region #{MiqRegion.my_region_number}")
end

it "nested" do
Zone.create!(:name => "example", :description => "example")
expect { MiqSchedule.create!(:zone => Zone.create!(:name => "example", :description => "example")) }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Zone: Name is not unique within region #{MiqRegion.my_region_number}")
end
end
end

0 comments on commit e385a95

Please sign in to comment.