2018-03-15 04:13:18 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-10 17:30:44 -04:00
|
|
|
require "active_support/core_ext/class/attribute"
|
|
|
|
|
2018-03-15 04:13:18 -04:00
|
|
|
module ActiveModel
|
|
|
|
# == Active \Model \Error
|
|
|
|
#
|
|
|
|
# Represents one single error
|
|
|
|
class Error
|
|
|
|
CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
|
|
|
|
MESSAGE_OPTIONS = [:message]
|
|
|
|
|
2019-07-10 17:30:44 -04:00
|
|
|
class_attribute :i18n_customize_full_message, default: false
|
2019-07-09 08:40:57 -04:00
|
|
|
|
|
|
|
def self.full_message(attribute, message, base_class) # :nodoc:
|
|
|
|
return message if attribute == :base
|
|
|
|
attribute = attribute.to_s
|
|
|
|
|
|
|
|
if i18n_customize_full_message && base_class.respond_to?(:i18n_scope)
|
|
|
|
attribute = attribute.remove(/\[\d\]/)
|
|
|
|
parts = attribute.split(".")
|
|
|
|
attribute_name = parts.pop
|
|
|
|
namespace = parts.join("/") unless parts.empty?
|
|
|
|
attributes_scope = "#{base_class.i18n_scope}.errors.models"
|
|
|
|
|
|
|
|
if namespace
|
|
|
|
defaults = base_class.lookup_ancestors.map do |klass|
|
|
|
|
[
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.attributes.#{attribute_name}.format",
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.format",
|
|
|
|
]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
defaults = base_class.lookup_ancestors.map do |klass|
|
|
|
|
[
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes.#{attribute_name}.format",
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.format",
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defaults.flatten!
|
|
|
|
else
|
|
|
|
defaults = []
|
|
|
|
end
|
|
|
|
|
|
|
|
defaults << :"errors.format"
|
|
|
|
defaults << "%{attribute} %{message}"
|
|
|
|
|
|
|
|
attr_name = attribute.tr(".", "_").humanize
|
|
|
|
attr_name = base_class.human_attribute_name(attribute, default: attr_name)
|
|
|
|
|
|
|
|
I18n.t(defaults.shift,
|
|
|
|
default: defaults,
|
|
|
|
attribute: attr_name,
|
|
|
|
message: message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.generate_message(attribute, type, base, options) # :nodoc:
|
|
|
|
type = options.delete(:message) if options[:message].is_a?(Symbol)
|
|
|
|
value = (attribute != :base ? base.send(:read_attribute_for_validation, attribute) : nil)
|
|
|
|
|
|
|
|
options = {
|
|
|
|
model: base.model_name.human,
|
|
|
|
attribute: base.class.human_attribute_name(attribute),
|
|
|
|
value: value,
|
|
|
|
object: base
|
|
|
|
}.merge!(options)
|
|
|
|
|
|
|
|
if base.class.respond_to?(:i18n_scope)
|
|
|
|
i18n_scope = base.class.i18n_scope.to_s
|
2019-10-15 15:18:07 -04:00
|
|
|
attribute = attribute.to_s.remove(/\[\d\]/)
|
2019-10-11 16:36:47 -04:00
|
|
|
|
2019-07-09 08:40:57 -04:00
|
|
|
defaults = base.class.lookup_ancestors.flat_map do |klass|
|
2019-10-15 15:18:07 -04:00
|
|
|
[ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
|
|
|
:"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
2019-07-09 08:40:57 -04:00
|
|
|
end
|
|
|
|
defaults << :"#{i18n_scope}.errors.messages.#{type}"
|
|
|
|
|
|
|
|
catch(:exception) do
|
2019-09-03 06:33:18 -04:00
|
|
|
translation = I18n.translate(defaults.first, **options.merge(default: defaults.drop(1), throw: true))
|
2019-07-09 08:40:57 -04:00
|
|
|
return translation unless translation.nil?
|
|
|
|
end unless options[:message]
|
|
|
|
else
|
|
|
|
defaults = []
|
|
|
|
end
|
|
|
|
|
|
|
|
defaults << :"errors.attributes.#{attribute}.#{type}"
|
|
|
|
defaults << :"errors.messages.#{type}"
|
|
|
|
|
|
|
|
key = defaults.shift
|
|
|
|
defaults = options.delete(:message) if options[:message]
|
|
|
|
options[:default] = defaults
|
|
|
|
|
2019-09-03 06:33:18 -04:00
|
|
|
I18n.translate(key, **options)
|
2019-07-09 08:40:57 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
def initialize(base, attribute, type = :invalid, **options)
|
2018-03-15 04:13:18 -04:00
|
|
|
@base = base
|
|
|
|
@attribute = attribute
|
|
|
|
@raw_type = type
|
|
|
|
@type = type || :invalid
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_dup(other)
|
|
|
|
@attribute = @attribute.dup
|
|
|
|
@raw_type = @raw_type.dup
|
|
|
|
@type = @type.dup
|
|
|
|
@options = @options.deep_dup
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :base, :attribute, :type, :raw_type, :options
|
|
|
|
|
|
|
|
def message
|
|
|
|
case raw_type
|
|
|
|
when Symbol
|
2019-07-09 08:40:57 -04:00
|
|
|
self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS))
|
2018-03-15 04:13:18 -04:00
|
|
|
else
|
|
|
|
raw_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detail
|
|
|
|
{ error: raw_type }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS))
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_message
|
2019-07-09 08:40:57 -04:00
|
|
|
self.class.full_message(attribute, message, @base.class)
|
2018-03-15 04:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# See if error matches provided +attribute+, +type+ and +options+.
|
|
|
|
def match?(attribute, type = nil, **options)
|
|
|
|
if @attribute != attribute || (type && @type != type)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
options.each do |key, value|
|
|
|
|
if @options[key] != value
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2018-03-20 10:39:44 -04:00
|
|
|
|
|
|
|
def strict_match?(attribute, type, **options)
|
2019-07-10 11:52:43 -04:00
|
|
|
return false unless match?(attribute, type)
|
2018-03-20 10:39:44 -04:00
|
|
|
|
2019-07-10 11:52:43 -04:00
|
|
|
options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)
|
2018-03-20 10:39:44 -04:00
|
|
|
end
|
2018-04-03 01:06:04 -04:00
|
|
|
|
|
|
|
def ==(other)
|
2019-01-12 08:03:22 -05:00
|
|
|
other.is_a?(self.class) && attributes_for_hash == other.attributes_for_hash
|
2018-04-03 01:06:04 -04:00
|
|
|
end
|
|
|
|
alias eql? ==
|
|
|
|
|
|
|
|
def hash
|
|
|
|
attributes_for_hash.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
def attributes_for_hash
|
2019-07-09 10:14:56 -04:00
|
|
|
[@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)]
|
2018-04-03 01:06:04 -04:00
|
|
|
end
|
2018-03-15 04:13:18 -04:00
|
|
|
end
|
|
|
|
end
|