ActiveModel::Error (original) (raw)

Active Model Error

Represents one single error

Methods

A

D

F

M

N

S

Constants

CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
MESSAGE_OPTIONS = [:message]

Attributes

[R] attribute The attribute of base which the error belongs to
[R] base The object which the error belongs to
[R] options The options provided when calling errors#add
[R] raw_type The raw value provided as the second parameter when calling errors#add
[R] type The type of error, defaults to :invalid unless specified

Class Public methods

Source: show | on GitHub

def initialize(base, attribute, type = :invalid, **options) @base = base @attribute = attribute @raw_type = type @type = type || :invalid @options = options end

Instance Public methods

Returns the error details.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.details
# => { error: :too_short, count: 5 }

Source: show | on GitHub

def details { error: raw_type }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)) 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)"

Source: show | on GitHub

def full_message self.class.full_message(attribute, message, @base) end

See if error matches provided attribute, type, and options.

Omitted params are not checked for a match.

Source: show | on GitHub

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

Returns the error message.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.message
# => "is too short (minimum is 5 characters)"

Source: show | on GitHub

def message case raw_type when Symbol self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS)) else raw_type end 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.

Source: show | on GitHub

def strict_match?(attribute, type, **options) return false unless match?(attribute, type)

options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end

Instance Protected methods

Source: show | on GitHub

def attributes_for_hash [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)] end