class ActiveModel::Error
Active Model Error
Represents one single error
Constants
- CALLBACKS_OPTIONS
- MESSAGE_OPTIONS
Attributes
attribute[R]
The attribute of base which the error belongs to
base[R]
The object which the error belongs to
options[R]
The options provided when calling `errors#add`
raw_type[R]
The raw value provided as the second parameter when calling `errors#add`
type[R]
The type of error, defaults to `:invalid` unless specified
Public Class Methods
# File activemodel/lib/active_model/error.rb, line 103 def initialize(base, attribute, type = :invalid, **options) @base = base @attribute = attribute @raw_type = type @type = type || :invalid @options = options end
Public Instance Methods
detail()
Alias for: details
# File activemodel/lib/active_model/error.rb, line 148
def details
{ error: raw_type }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS))
end Returns the error details.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.details
# => { error: :too_short, count: 5 }
Also aliased as: detail
# File activemodel/lib/active_model/error.rb, line 158 def full_message self.class.full_message(attribute, message, @base) end
Returns the full error message.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5) error.full_message # => "Name is too short (minimum is 5 characters)"
# File activemodel/lib/active_model/error.rb, line 165
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 See if error matches provided attribute, type and options.
Omitted params are not checked for a match.
# File activemodel/lib/active_model/error.rb, line 134
def message
case raw_type
when Symbol
self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS))
else
raw_type
end
end Returns the error message.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5) error.message # => "is too short (minimum is 5 characters)"
# File activemodel/lib/active_model/error.rb, line 183 def strict_match?(attribute, type, **options) return false unless match?(attribute, type) options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end
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.
Protected Instance Methods
# File activemodel/lib/active_model/error.rb, line 203 def attributes_for_hash [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)] end
© 2004–2020 David Heinemeier Hansson
Licensed under the MIT License.