class ActiveModel::BetterErrors::HumanMessageFormatter

HumanMessageFormatter

Public Instance Methods

format_message() click to toggle source
# File lib/active_model/better_errors/human_message_formatter.rb, line 9
def format_message
  return message if message

  keys = i18n_keys
  key  = keys.shift

  options = {
    default: keys,
    model: base.class.model_name.human,
    attribute: base.class.human_attribute_name(attribute),
    value: value
  }.merge(self.options)

  I18n.translate key, options
end

Private Instance Methods

ancestor_keys() click to toggle source
# File lib/active_model/better_errors/human_message_formatter.rb, line 36
def ancestor_keys
  return [] unless base.class.respond_to?(:i18n_scope)
  scope = base.class.i18n_scope

  base.class.lookup_ancestors.map do |klass|
    key_base = "#{scope}.errors.models.#{klass.model_name.i18n_key}"
    [
      :"#{key_base}.attributes.#{attribute}.#{type}",
      :"#{key_base}.#{type}"
    ]
  end
end
i18n_keys() click to toggle source
# File lib/active_model/better_errors/human_message_formatter.rb, line 49
def i18n_keys
  keys = ancestor_keys
  keys << message

  if base.class.respond_to?(:i18n_scope)
    keys << :"#{base.class.i18n_scope}.errors.messages.#{type}"
  end

  keys << :"errors.attributes.#{attribute}.#{type}"
  keys << :"errors.messages.#{type}"

  keys.compact!
  keys.flatten!
  keys
end
type() click to toggle source
# File lib/active_model/better_errors/human_message_formatter.rb, line 27
def type
  error_message.type || :invalid
end
value() click to toggle source
# File lib/active_model/better_errors/human_message_formatter.rb, line 31
def value
  return if attribute == :base
  base.send :read_attribute_for_validation, attribute
end