Skip to content

Commit

Permalink
Merge pull request #40434 from filipe-sabella/pass-in-base-in-validat…
Browse files Browse the repository at this point in the history
…ion-messages

Pass in base to Error.human_attribute_names
  • Loading branch information
rafaelfranca committed Nov 2, 2020
2 parents f8a1a8a + ac677fb commit af91d9a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
8 changes: 8 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* Pass in `base` instead of `base_class` to Error.human_attribute_name

*Filipe Sabella*

This is useful in cases where the `human_attribute_name` method depends
on other attributes' values of the class under validation to derive what the
attirbute name should be.

* Deprecate marshalling load from legacy attributes format.

*Ryuta Kamizono*
Expand Down
13 changes: 9 additions & 4 deletions activemodel/lib/active_model/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Error

class_attribute :i18n_customize_full_message, default: false

def self.full_message(attribute, message, base_class) # :nodoc:
def self.full_message(attribute, message, base) # :nodoc:
return message if attribute == :base

base_class = base.class
attribute = attribute.to_s

if i18n_customize_full_message && base_class.respond_to?(:i18n_scope)
Expand Down Expand Up @@ -48,7 +50,10 @@ def self.full_message(attribute, message, base_class) # :nodoc:
defaults << "%{attribute} %{message}"

attr_name = attribute.tr(".", "_").humanize
attr_name = base_class.human_attribute_name(attribute, default: attr_name)
attr_name = base_class.human_attribute_name(attribute, {
default: attr_name,
base: base,
})

I18n.t(defaults.shift,
default: defaults,
Expand All @@ -62,7 +67,7 @@ def self.generate_message(attribute, type, base, options) # :nodoc:

options = {
model: base.model_name.human,
attribute: base.class.human_attribute_name(attribute),
attribute: base.class.human_attribute_name(attribute, { base: base }),
value: value,
object: base
}.merge!(options)
Expand Down Expand Up @@ -151,7 +156,7 @@ def details
# error.full_message
# # => "Name is too short (minimum is 5 characters)"
def full_message
self.class.full_message(attribute, message, @base.class)
self.class.full_message(attribute, message, @base)
end

# See if error matches provided +attribute+, +type+ and +options+.
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def messages_for(attribute)
#
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
def full_message(attribute, message)
Error.full_message(attribute, message, @base.class)
Error.full_message(attribute, message, @base)
end

# Translates an error message in its default scope
Expand Down
2 changes: 1 addition & 1 deletion activemodel/test/cases/validations/i18n_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_full_message_encoding

def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
@person.errors.add(:name, "not found")
assert_called_with(person_class, :human_attribute_name, ["name", default: "Name"], returns: "Person's name") do
assert_called_with(person_class, :human_attribute_name, ["name", default: "Name", base: @person], returns: "Person's name") do
assert_equal ["Person's name not found"], @person.errors.full_messages
end
end
Expand Down

0 comments on commit af91d9a

Please sign in to comment.