Class: ActiveModel::Error
- Inherits:
-
Object
- Object
- ActiveModel::Error
- Defined in:
- lib/active_model/error.rb
Overview
Active Model Error
Represents one single error
Direct Known Subclasses
Constant Summary collapse
- CALLBACKS_OPTIONS =
[:if, :unless, :on, :allow_nil, :allow_blank, :strict]
- MESSAGE_OPTIONS =
[:message]
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
The attribute of
base
which the error belongs to. -
#base ⇒ Object
readonly
The object which the error belongs to.
-
#options ⇒ Object
readonly
The options provided when calling errors#add.
-
#raw_type ⇒ Object
readonly
The raw value provided as the second parameter when calling errors#add.
-
#type ⇒ Object
readonly
The type of error, defaults to
:invalid
unless specified.
Class Method Summary collapse
-
.full_message(attribute, message, base) ⇒ Object
:nodoc:.
-
.generate_message(attribute, type, base, options) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
:nodoc:.
-
#details ⇒ Object
(also: #detail)
Returns the error details.
-
#full_message ⇒ Object
Returns the full error message.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(base, attribute, type = :invalid, **options) ⇒ Error
constructor
A new instance of Error.
-
#initialize_dup(other) ⇒ Object
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#match?(attribute, type = nil, **options) ⇒ Boolean
See if error matches provided
attribute
,type
andoptions
. -
#message ⇒ Object
Returns the error message.
-
#strict_match?(attribute, type, **options) ⇒ Boolean
See if error matches provided
attribute
,type
andoptions
exactly.
Constructor Details
#initialize(base, attribute, type = :invalid, **options) ⇒ Error
Returns a new instance of Error.
103 104 105 106 107 108 109 |
# File 'lib/active_model/error.rb', line 103 def initialize(base, attribute, type = :invalid, **) @base = base @attribute = attribute @raw_type = type @type = type || :invalid @options = end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
The attribute of base
which the error belongs to
121 122 123 |
# File 'lib/active_model/error.rb', line 121 def attribute @attribute end |
#base ⇒ Object (readonly)
The object which the error belongs to
119 120 121 |
# File 'lib/active_model/error.rb', line 119 def base @base end |
#options ⇒ Object (readonly)
The options provided when calling errors#add
127 128 129 |
# File 'lib/active_model/error.rb', line 127 def @options end |
#raw_type ⇒ Object (readonly)
The raw value provided as the second parameter when calling errors#add
125 126 127 |
# File 'lib/active_model/error.rb', line 125 def raw_type @raw_type end |
#type ⇒ Object (readonly)
The type of error, defaults to :invalid
unless specified
123 124 125 |
# File 'lib/active_model/error.rb', line 123 def type @type end |
Class Method Details
.full_message(attribute, message, base) ⇒ Object
:nodoc:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File '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.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) ⇒ Object
:nodoc:
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_model/error.rb', line 64 def self.(attribute, type, base, ) # :nodoc: type = .delete(:message) if [:message].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 [:message] else defaults = [] end defaults << :"errors.attributes.#{attribute}.#{type}" defaults << :"errors.messages.#{type}" key = defaults.shift defaults = .delete(:message) if [:message] [:default] = defaults I18n.translate(key, **) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
:nodoc:
189 190 191 |
# File 'lib/active_model/error.rb', line 189 def ==(other) # :nodoc: other.is_a?(self.class) && attributes_for_hash == other.attributes_for_hash end |
#details ⇒ Object 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 }
148 149 150 |
# File 'lib/active_model/error.rb', line 148 def details { error: raw_type }.merge(.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)) end |
#full_message ⇒ Object
Returns the full error message.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.
# => "Name is too short (minimum is 5 characters)"
158 159 160 |
# File 'lib/active_model/error.rb', line 158 def self.class.(attribute, , @base) end |
#hash ⇒ Object
:nodoc:
194 195 196 |
# File 'lib/active_model/error.rb', line 194 def hash # :nodoc: attributes_for_hash.hash end |
#initialize_dup(other) ⇒ Object
:nodoc:
111 112 113 114 115 116 |
# File '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 ⇒ Object
:nodoc:
198 199 200 |
# File 'lib/active_model/error.rb', line 198 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.
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/active_model/error.rb', line 165 def match?(attribute, type = nil, **) if @attribute != attribute || (type && @type != type) return false end .each do |key, value| if @options[key] != value return false end end true end |
#message ⇒ Object
Returns the error message.
error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.
# => "is too short (minimum is 5 characters)"
134 135 136 137 138 139 140 141 |
# File 'lib/active_model/error.rb', line 134 def case raw_type when Symbol self.class.(attribute, raw_type, @base, .except(*CALLBACKS_OPTIONS)) else raw_type end end |
#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.
183 184 185 186 187 |
# File 'lib/active_model/error.rb', line 183 def strict_match?(attribute, type, **) return false unless match?(attribute, type) == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS) end |