Class: ActiveModel::Error
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activemodel/lib/active_model/error.rb |
Overview
Represents one single error
Constant Summary
-
CALLBACKS_OPTIONS =
# File 'activemodel/lib/active_model/error.rb', line 10[:if, :unless, :on, :allow_nil, :allow_blank, :strict]
-
MESSAGE_OPTIONS =
# File 'activemodel/lib/active_model/error.rb', line 11[: ]
Class Attribute Summary
Class Method Summary
- .new(base, attribute, type = :invalid, **options) ⇒ Error constructor
- .full_message(attribute, message, base) Internal use only
- .generate_message(attribute, type, base, options) Internal use only
Instance Attribute Summary
-
#attribute
readonly
The attribute of #base which the error belongs to.
-
#base
readonly
The object which the error belongs to.
- #i18n_customize_full_message rw
- #i18n_customize_full_message? ⇒ Boolean rw
-
#options
readonly
The options provided when calling
errors#add
. -
#raw_type
readonly
The raw value provided as the second parameter when calling
errors#add
. -
#type
readonly
The type of error, defaults to
:invalid
unless specified.
Instance Method Summary
-
#detail
Alias for #details.
-
#details
(also: #detail)
Returns the error details.
-
#eql?(other)
Alias for #==.
-
#full_message
Returns the full error message.
-
#match?(attribute, type = nil, **options) ⇒ Boolean
See if error matches provided #attribute, #type, and #options.
-
#message
Returns the error message.
-
#strict_match?(attribute, type, **options) ⇒ Boolean
See if error matches provided #attribute, #type, and #options exactly.
- #attributes_for_hash protected
- #==(other) (also: #eql?) Internal use only
- #hash Internal use only
- #initialize_dup(other) Internal use only
- #inspect Internal use only
Constructor Details
.new(base, attribute, type = :invalid, **options) ⇒ Error
Class Attribute Details
.i18n_customize_full_message (rw)
[ GitHub ]# File 'activemodel/lib/active_model/error.rb', line 13
class_attribute :, default: false
.i18n_customize_full_message? ⇒ Boolean
(rw)
[ GitHub ]
# File 'activemodel/lib/active_model/error.rb', line 13
class_attribute :, default: false
Class Method Details
.full_message(attribute, message, base)
# File 'activemodel/lib/active_model/error.rb', line 15
def self. (attribute, , base) # :nodoc: return if attribute == :base base_class = base.class attribute = attribute.to_s if && 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.remove(/\.base\z/).tr(".", "_").humanize attr_name = base_class.human_attribute_name(attribute, { default: attr_name, base: base, }) I18n.t(defaults.shift, default: defaults, attribute: attr_name, message: ) end
.generate_message(attribute, type, base, options)
# File 'activemodel/lib/active_model/error.rb', line 64
def self. (attribute, type, base, ) # :nodoc: type = .delete(: ) if [: ].is_a?(Symbol) value = (attribute != :base ? base.read_attribute_for_validation(attribute) : nil) = { model: base.model_name.human, attribute: base.class.human_attribute_name(attribute, { base: base }), value: value, object: base }.merge!( ) if base.class.respond_to?(:i18n_scope) i18n_scope = base.class.i18n_scope.to_s attribute = attribute.to_s.remove(/\[\d+\]/) defaults = base.class.lookup_ancestors.flat_map do |klass| [ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}", :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ] end defaults << :"#{i18n_scope}.errors.messages.#{type}" catch(:exception) do translation = I18n.translate(defaults.first, ** .merge(default: defaults.drop(1), throw: true)) return translation unless translation.nil? end unless [: ] else defaults = [] end defaults << :"errors.attributes.#{attribute}.#{type}" defaults << :"errors.messages.#{type}" key = defaults.shift defaults = .delete(: ) if [: ] [:default] = defaults I18n.translate(key, ** ) end
Instance Attribute Details
#attribute (readonly)
The attribute of #base which the error belongs to
# File 'activemodel/lib/active_model/error.rb', line 121
attr_reader :attribute
#base (readonly)
The object which the error belongs to
# File 'activemodel/lib/active_model/error.rb', line 119
attr_reader :base
#i18n_customize_full_message (rw)
[ GitHub ]# File 'activemodel/lib/active_model/error.rb', line 13
class_attribute :, default: false
#i18n_customize_full_message? ⇒ Boolean
(rw)
[ GitHub ]
# File 'activemodel/lib/active_model/error.rb', line 13
class_attribute :, default: false
#options (readonly)
The options provided when calling errors#add
# File 'activemodel/lib/active_model/error.rb', line 128
attr_reader :
#raw_type (readonly)
The raw value provided as the second parameter when calling errors#add
# File 'activemodel/lib/active_model/error.rb', line 126
attr_reader :raw_type
#type (readonly)
The type of error, defaults to :invalid
unless specified
# File 'activemodel/lib/active_model/error.rb', line 123
attr_reader :type
Instance Method Details
#==(other) Also known as: #eql?
# File 'activemodel/lib/active_model/error.rb', line 190
def ==(other) # :nodoc: other.is_a?(self.class) && attributes_for_hash == other.attributes_for_hash end
#attributes_for_hash (protected)
[ GitHub ]# File 'activemodel/lib/active_model/error.rb', line 204
def attributes_for_hash [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)] end
#detail
Alias for #details.
# File 'activemodel/lib/active_model/error.rb', line 152
alias_method :detail, :details
#details Also known as: #detail
Returns the error details.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.details
# => { error: :too_short, count: 5 }
# File 'activemodel/lib/active_model/error.rb', line 149
def details { error: raw_type }.merge( .except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)) end
#eql?(other)
Alias for #==.
# File 'activemodel/lib/active_model/error.rb', line 193
alias eql? ==
#full_message
Returns the full error message.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.
# => "Name is too short (minimum is 5 characters)"
# File 'activemodel/lib/active_model/error.rb', line 159
def self.class. (attribute, , @base) end
#hash
# File 'activemodel/lib/active_model/error.rb', line 195
def hash # :nodoc: attributes_for_hash.hash end
#initialize_dup(other)
# File 'activemodel/lib/active_model/error.rb', line 111
def initialize_dup(other) # :nodoc: @attribute = @attribute.dup @raw_type = @raw_type.dup @type = @type.dup @options = @options.deep_dup end
#inspect
# File 'activemodel/lib/active_model/error.rb', line 199
def inspect # :nodoc: "#<#{self.class.name} attribute=#{@attribute}, type=#{@type}, options=#{@options.inspect}>" end
#match?(attribute, type = nil, **options) ⇒ Boolean
See if error matches provided #attribute, #type, and #options.
Omitted params are not checked for a match.
#message
Returns the error message.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.
# => "is too short (minimum is 5 characters)"
#strict_match?(attribute, type, **options) ⇒ Boolean
See if error matches provided #attribute, #type, and #options exactly.
All params must be equal to Error’s own attributes to be considered a strict match.
# File 'activemodel/lib/active_model/error.rb', line 184
def strict_match?(attribute, type, ** ) return false unless match?(attribute, type) == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end