2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:02 -04:00
|
|
|
require "active_support/core_ext/array/conversions"
|
|
|
|
require "active_support/core_ext/string/inflections"
|
|
|
|
require "active_support/core_ext/object/deep_dup"
|
|
|
|
require "active_support/core_ext/string/filters"
|
2009-06-08 21:32:08 -04:00
|
|
|
|
2008-03-31 19:40:34 -04:00
|
|
|
module ActiveModel
|
2012-10-21 02:26:01 -04:00
|
|
|
# == Active \Model \Errors
|
2010-06-14 05:10:57 -04:00
|
|
|
#
|
2012-02-07 13:53:03 -05:00
|
|
|
# Provides a modified +Hash+ that you can include in your object
|
2013-12-24 06:39:41 -05:00
|
|
|
# for handling error messages and interacting with Action View helpers.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# A minimal implementation could be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# class Person
|
|
|
|
# # Required dependency for ActiveModel::Errors
|
|
|
|
# extend ActiveModel::Naming
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# def initialize
|
|
|
|
# @errors = ActiveModel::Errors.new(self)
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :name
|
|
|
|
# attr_reader :errors
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# def validate!
|
2015-01-04 03:18:03 -05:00
|
|
|
# errors.add(:name, :blank, message: "cannot be nil") if name.nil?
|
2010-01-31 18:08:20 -05:00
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# # The following methods are needed to be minimally implemented
|
|
|
|
#
|
|
|
|
# def read_attribute_for_validation(attr)
|
|
|
|
# send(attr)
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-03-22 18:10:28 -04:00
|
|
|
# def self.human_attribute_name(attr, options = {})
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-03-22 18:10:28 -04:00
|
|
|
# def self.lookup_ancestors
|
2010-01-31 18:08:20 -05:00
|
|
|
# [self]
|
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-05-29 16:27:23 -04:00
|
|
|
# The last three methods are required in your object for +Errors+ to be
|
2010-01-31 18:08:20 -05:00
|
|
|
# able to generate error messages correctly and also handle multiple
|
2015-05-29 16:27:23 -04:00
|
|
|
# languages. Of course, if you extend your object with <tt>ActiveModel::Translation</tt>
|
2011-05-23 19:39:10 -04:00
|
|
|
# you will not need to implement the last two. Likewise, using
|
2015-05-29 16:27:23 -04:00
|
|
|
# <tt>ActiveModel::Validations</tt> will handle the validation related methods
|
2010-01-31 18:08:20 -05:00
|
|
|
# for you.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# The above allows you to do:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2013-06-29 12:17:50 -04:00
|
|
|
# person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.validate! # => ["cannot be nil"]
|
|
|
|
# person.errors.full_messages # => ["name cannot be nil"]
|
2010-01-31 18:08:20 -05:00
|
|
|
# # etc..
|
2011-02-08 20:40:49 -05:00
|
|
|
class Errors
|
|
|
|
include Enumerable
|
|
|
|
|
2011-08-17 10:26:00 -04:00
|
|
|
CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
|
2015-01-04 03:18:03 -05:00
|
|
|
MESSAGE_OPTIONS = [:message]
|
2010-06-21 06:17:24 -04:00
|
|
|
|
2018-05-22 17:20:14 -04:00
|
|
|
class << self
|
|
|
|
attr_accessor :i18n_full_message # :nodoc:
|
|
|
|
end
|
|
|
|
self.i18n_full_message = false
|
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
attr_reader :messages, :details
|
2011-02-08 20:40:49 -05:00
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Pass in the instance of the object that is using the errors object.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# class Person
|
|
|
|
# def initialize
|
|
|
|
# @errors = ActiveModel::Errors.new(self)
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-19 19:28:59 -04:00
|
|
|
def initialize(base)
|
2011-02-08 20:40:49 -05:00
|
|
|
@base = base
|
2016-05-30 14:00:14 -04:00
|
|
|
@messages = apply_default_array({})
|
|
|
|
@details = apply_default_array({})
|
2011-02-08 20:40:49 -05:00
|
|
|
end
|
|
|
|
|
2012-10-22 15:27:55 -04:00
|
|
|
def initialize_dup(other) # :nodoc:
|
2012-01-17 02:07:15 -05:00
|
|
|
@messages = other.messages.dup
|
2015-01-24 05:58:52 -05:00
|
|
|
@details = other.details.deep_dup
|
2012-01-17 02:07:15 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-12-03 13:18:57 -05:00
|
|
|
# Copies the errors from <tt>other</tt>.
|
|
|
|
#
|
|
|
|
# other - The ActiveModel::Errors instance.
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# person.errors.copy!(other)
|
|
|
|
def copy!(other) # :nodoc:
|
|
|
|
@messages = other.messages.dup
|
|
|
|
@details = other.details.dup
|
|
|
|
end
|
|
|
|
|
2017-07-07 13:16:06 -04:00
|
|
|
# Merges the errors from <tt>other</tt>.
|
|
|
|
#
|
|
|
|
# other - The ActiveModel::Errors instance.
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# person.errors.merge!(other)
|
|
|
|
def merge!(other)
|
|
|
|
@messages.merge!(other.messages) { |_, ary1, ary2| ary1 + ary2 }
|
|
|
|
@details.merge!(other.details) { |_, ary1, ary2| ary1 + ary2 }
|
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Clear the error messages.
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.full_messages # => ["name cannot be nil"]
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.clear
|
|
|
|
# person.errors.full_messages # => []
|
2011-02-08 20:40:49 -05:00
|
|
|
def clear
|
|
|
|
messages.clear
|
2015-01-04 03:18:03 -05:00
|
|
|
details.clear
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns +true+ if the error messages include an error for the given key
|
2012-06-22 17:48:50 -04:00
|
|
|
# +attribute+, +false+ otherwise.
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.messages # => {:name=>["cannot be nil"]}
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.include?(:name) # => true
|
2012-10-22 15:27:55 -04:00
|
|
|
# person.errors.include?(:age) # => false
|
2012-06-22 13:42:32 -04:00
|
|
|
def include?(attribute)
|
2016-02-14 11:41:59 -05:00
|
|
|
attribute = attribute.to_sym
|
2016-03-24 10:27:31 -04:00
|
|
|
messages.key?(attribute) && messages[attribute].present?
|
2011-02-09 12:19:15 -05:00
|
|
|
end
|
2011-09-06 12:35:12 -04:00
|
|
|
alias :has_key? :include?
|
2014-10-14 04:53:26 -04:00
|
|
|
alias :key? :include?
|
2011-02-09 12:19:15 -05:00
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Delete messages for +key+. Returns the deleted messages.
|
|
|
|
#
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors[:name] # => ["cannot be nil"]
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.delete(:name) # => ["cannot be nil"]
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors[:name] # => []
|
2012-01-16 11:22:44 -05:00
|
|
|
def delete(key)
|
2016-02-14 11:41:59 -05:00
|
|
|
attribute = key.to_sym
|
|
|
|
details.delete(attribute)
|
|
|
|
messages.delete(attribute)
|
2012-01-16 11:22:44 -05:00
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# When passed a symbol or a name of a method, returns an array of errors
|
2010-06-14 05:10:57 -04:00
|
|
|
# for the method.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors[:name] # => ["cannot be nil"]
|
|
|
|
# person.errors['name'] # => ["cannot be nil"]
|
2008-03-31 19:40:34 -04:00
|
|
|
def [](attribute)
|
2015-01-21 16:47:39 -05:00
|
|
|
messages[attribute.to_sym]
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Iterates through each error key, value pair in the error messages hash.
|
2011-05-23 19:39:10 -04:00
|
|
|
# Yields the attribute and the error for that attribute. If the attribute
|
2010-01-31 18:08:20 -05:00
|
|
|
# has more than one error message, yields once for each error message.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :blank, message: "can't be blank")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.each do |attribute, error|
|
2010-01-31 18:08:20 -05:00
|
|
|
# # Will yield :name and "can't be blank"
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :not_specified, message: "must be specified")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.each do |attribute, error|
|
2010-01-31 18:08:20 -05:00
|
|
|
# # Will yield :name and "can't be blank"
|
|
|
|
# # then yield :name and "must be specified"
|
|
|
|
# end
|
2008-03-31 19:40:34 -04:00
|
|
|
def each
|
2011-02-08 20:40:49 -05:00
|
|
|
messages.each_key do |attribute|
|
2015-01-21 16:47:39 -05:00
|
|
|
messages[attribute].each { |error| yield attribute, error }
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Returns the number of error messages.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :blank, message: "can't be blank")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.size # => 1
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :not_specified, message: "must be specified")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.size # => 2
|
2008-03-31 19:40:34 -04:00
|
|
|
def size
|
|
|
|
values.flatten.size
|
|
|
|
end
|
2015-02-20 15:31:24 -05:00
|
|
|
alias :count :size
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns all message values.
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
|
|
|
# person.errors.values # => [["cannot be nil", "must be specified"]]
|
2011-02-08 20:40:49 -05:00
|
|
|
def values
|
2017-04-01 02:52:11 -04:00
|
|
|
messages.select do |key, value|
|
|
|
|
!value.empty?
|
2017-03-28 05:05:14 -04:00
|
|
|
end.values
|
2011-02-08 20:40:49 -05:00
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns all message keys.
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.keys # => [:name]
|
2011-02-08 20:40:49 -05:00
|
|
|
def keys
|
2017-04-01 02:52:11 -04:00
|
|
|
messages.select do |key, value|
|
|
|
|
!value.empty?
|
2017-03-28 05:05:14 -04:00
|
|
|
end.keys
|
2011-02-08 20:40:49 -05:00
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns +true+ if no errors are found, +false+ otherwise.
|
2011-12-16 14:17:14 -05:00
|
|
|
# If the error message is a string it can be empty.
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.full_messages # => ["name cannot be nil"]
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.empty? # => false
|
2010-04-09 10:48:24 -04:00
|
|
|
def empty?
|
2015-02-20 15:31:24 -05:00
|
|
|
size.zero?
|
2010-04-09 10:48:24 -04:00
|
|
|
end
|
2015-02-20 15:31:24 -05:00
|
|
|
alias :blank? :empty?
|
2011-09-09 04:28:25 -04:00
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Returns an xml formatted representation of the Errors hash.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :blank, message: "can't be blank")
|
|
|
|
# person.errors.add(:name, :not_specified, message: "must be specified")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.to_xml
|
2010-07-29 20:30:04 -04:00
|
|
|
# # =>
|
2010-01-31 18:08:20 -05:00
|
|
|
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
|
|
|
# # <errors>
|
|
|
|
# # <error>name can't be blank</error>
|
|
|
|
# # <error>name must be specified</error>
|
|
|
|
# # </errors>
|
2016-10-28 23:05:58 -04:00
|
|
|
def to_xml(options = {})
|
2013-05-01 20:10:06 -04:00
|
|
|
to_a.to_xml({ root: "errors", skip_types: true }.merge!(options))
|
2010-06-26 05:57:43 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-06-22 17:04:16 -04:00
|
|
|
# Returns a Hash that can be used as the JSON representation for this
|
|
|
|
# object. You can pass the <tt>:full_messages</tt> option. This determines
|
|
|
|
# if the json object should contain full messages or not (false by default).
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.as_json # => {:name=>["cannot be nil"]}
|
|
|
|
# person.errors.as_json(full_messages: true) # => {:name=>["name cannot be nil"]}
|
2016-10-28 23:05:58 -04:00
|
|
|
def as_json(options = nil)
|
2012-02-17 05:12:10 -05:00
|
|
|
to_hash(options && options[:full_messages])
|
|
|
|
end
|
|
|
|
|
2012-12-05 01:11:54 -05:00
|
|
|
# Returns a Hash of attributes with their error messages. If +full_messages+
|
2012-06-22 17:04:16 -04:00
|
|
|
# is +true+, it will contain full messages (see +full_message+).
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.to_hash # => {:name=>["cannot be nil"]}
|
|
|
|
# person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
|
2012-02-17 05:12:10 -05:00
|
|
|
def to_hash(full_messages = false)
|
|
|
|
if full_messages
|
2016-08-07 19:05:28 -04:00
|
|
|
messages.each_with_object({}) do |(attribute, array), messages|
|
2014-08-22 09:08:12 -04:00
|
|
|
messages[attribute] = array.map { |message| full_message(attribute, message) }
|
2012-02-17 05:12:10 -05:00
|
|
|
end
|
|
|
|
else
|
2016-09-27 08:33:16 -04:00
|
|
|
without_default_proc(messages)
|
2012-02-17 05:12:10 -05:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2009-03-19 19:28:59 -04:00
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
# Adds +message+ to the error messages and used validator type to +details+ on +attribute+.
|
|
|
|
# More than one error can be added to the same +attribute+.
|
|
|
|
# If no +message+ is supplied, <tt>:invalid</tt> is assumed.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-06-22 17:04:16 -04:00
|
|
|
# person.errors.add(:name)
|
|
|
|
# # => ["is invalid"]
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :not_implemented, message: "must be implemented")
|
2012-06-22 17:04:16 -04:00
|
|
|
# # => ["is invalid", "must be implemented"]
|
|
|
|
#
|
|
|
|
# person.errors.messages
|
2015-01-04 03:18:03 -05:00
|
|
|
# # => {:name=>["is invalid", "must be implemented"]}
|
|
|
|
#
|
|
|
|
# person.errors.details
|
|
|
|
# # => {:name=>[{error: :not_implemented}, {error: :invalid}]}
|
2012-06-22 17:04:16 -04:00
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# If +message+ is a symbol, it will be translated using the appropriate
|
|
|
|
# scope (see +generate_message+).
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# If +message+ is a proc, it will be called, allowing for things like
|
|
|
|
# <tt>Time.now</tt> to be used within an error.
|
2012-06-23 15:49:22 -04:00
|
|
|
#
|
2013-12-24 06:39:41 -05:00
|
|
|
# If the <tt>:strict</tt> option is set to +true+, it will raise
|
2012-06-23 15:49:22 -04:00
|
|
|
# ActiveModel::StrictValidationFailed instead of adding the error.
|
2012-08-06 06:45:27 -04:00
|
|
|
# <tt>:strict</tt> option can also be set to any other exception.
|
2012-06-23 15:49:22 -04:00
|
|
|
#
|
2014-12-19 12:26:41 -05:00
|
|
|
# person.errors.add(:name, :invalid, strict: true)
|
2016-05-01 14:41:57 -04:00
|
|
|
# # => ActiveModel::StrictValidationFailed: Name is invalid
|
2014-12-19 12:26:41 -05:00
|
|
|
# person.errors.add(:name, :invalid, strict: NameIsInvalid)
|
2016-05-01 14:41:57 -04:00
|
|
|
# # => NameIsInvalid: Name is invalid
|
2012-06-23 15:49:22 -04:00
|
|
|
#
|
|
|
|
# person.errors.messages # => {}
|
2014-03-27 16:13:56 -04:00
|
|
|
#
|
|
|
|
# +attribute+ should be set to <tt>:base</tt> if the error is not
|
|
|
|
# directly associated with a single attribute.
|
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:base, :name_or_email_blank,
|
|
|
|
# message: "either name or email must be present")
|
2014-03-27 16:13:56 -04:00
|
|
|
# person.errors.messages
|
|
|
|
# # => {:base=>["either name or email must be present"]}
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.details
|
|
|
|
# # => {:base=>[{error: :name_or_email_blank}]}
|
2013-06-22 07:48:42 -04:00
|
|
|
def add(attribute, message = :invalid, options = {})
|
2015-01-04 03:18:03 -05:00
|
|
|
message = message.call if message.respond_to?(:call)
|
2016-02-17 16:09:54 -05:00
|
|
|
detail = normalize_detail(message, options)
|
2011-10-19 12:47:28 -04:00
|
|
|
message = normalize_message(attribute, message, options)
|
2012-08-06 06:45:27 -04:00
|
|
|
if exception = options[:strict]
|
|
|
|
exception = ActiveModel::StrictValidationFailed if exception == true
|
|
|
|
raise exception, full_message(attribute, message)
|
2011-08-17 10:26:00 -04:00
|
|
|
end
|
2010-05-19 10:25:46 -04:00
|
|
|
|
2015-01-21 16:47:39 -05:00
|
|
|
details[attribute.to_sym] << detail
|
|
|
|
messages[attribute.to_sym] << message
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns +true+ if an error on the attribute with the given message is
|
2016-06-22 16:45:11 -04:00
|
|
|
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
|
|
|
# person.errors.add :name, :blank
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors.added? :name, :blank # => true
|
2016-06-22 16:45:11 -04:00
|
|
|
# person.errors.added? :name, "can't be blank" # => true
|
|
|
|
#
|
|
|
|
# If the error message requires an option, then it returns +true+ with
|
|
|
|
# the correct option, or +false+ with an incorrect or missing option.
|
|
|
|
#
|
|
|
|
# person.errors.add :name, :too_long, { count: 25 }
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors.added? :name, :too_long, count: 25 # => true
|
2016-06-22 16:45:11 -04:00
|
|
|
# person.errors.added? :name, "is too long (maximum is 25 characters)" # => true
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors.added? :name, :too_long, count: 24 # => false
|
|
|
|
# person.errors.added? :name, :too_long # => false
|
|
|
|
# person.errors.added? :name, "is too long" # => false
|
2013-06-22 07:48:42 -04:00
|
|
|
def added?(attribute, message = :invalid, options = {})
|
2017-11-10 03:38:05 -05:00
|
|
|
if message.is_a? Symbol
|
2018-07-14 01:57:43 -04:00
|
|
|
self.details[attribute.to_sym].map { |e| e[:error] }.include? message
|
2017-11-10 03:38:05 -05:00
|
|
|
else
|
|
|
|
message = message.call if message.respond_to?(:call)
|
|
|
|
message = normalize_message(attribute, message, options)
|
|
|
|
self[attribute].include? message
|
|
|
|
end
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2009-03-19 19:28:59 -04:00
|
|
|
# Returns all the full error messages in an array.
|
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# class Person
|
2009-03-19 19:28:59 -04:00
|
|
|
# validates_presence_of :name, :address, :email
|
2012-06-22 12:52:57 -04:00
|
|
|
# validates_length_of :name, in: 5..30
|
2009-03-19 19:28:59 -04:00
|
|
|
# end
|
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# person = Person.create(address: '123 First St.')
|
|
|
|
# person.errors.full_messages
|
|
|
|
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
|
2009-10-20 20:20:01 -04:00
|
|
|
def full_messages
|
2011-09-09 04:28:25 -04:00
|
|
|
map { |attribute, message| full_message(attribute, message) }
|
|
|
|
end
|
2015-02-20 15:31:24 -05:00
|
|
|
alias :to_a :full_messages
|
2009-10-20 20:20:01 -04:00
|
|
|
|
2013-03-24 03:34:27 -04:00
|
|
|
# Returns all the full error messages for a given attribute in an array.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# validates_presence_of :name, :email
|
|
|
|
# validates_length_of :name, in: 5..30
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.create()
|
|
|
|
# person.errors.full_messages_for(:name)
|
|
|
|
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
|
|
|
|
def full_messages_for(attribute)
|
2016-02-14 11:41:59 -05:00
|
|
|
attribute = attribute.to_sym
|
2015-01-21 16:47:39 -05:00
|
|
|
messages[attribute].map { |message| full_message(attribute, message) }
|
2013-03-24 03:34:27 -04:00
|
|
|
end
|
|
|
|
|
2011-09-09 04:28:25 -04:00
|
|
|
# Returns a full message for a given attribute.
|
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
|
2018-05-17 08:39:31 -04:00
|
|
|
#
|
2018-06-11 17:24:49 -04:00
|
|
|
# The `"%{attribute} %{message}"` error format can be overridden with either
|
2018-05-17 08:39:31 -04:00
|
|
|
#
|
|
|
|
# * <tt>activemodel.errors.models.person/contacts/addresses.attributes.street.format</tt>
|
|
|
|
# * <tt>activemodel.errors.models.person/contacts/addresses.format</tt>
|
|
|
|
# * <tt>activemodel.errors.models.person.attributes.name.format</tt>
|
|
|
|
# * <tt>activemodel.errors.models.person.format</tt>
|
|
|
|
# * <tt>errors.format</tt>
|
2011-09-09 04:28:25 -04:00
|
|
|
def full_message(attribute, message)
|
|
|
|
return message if attribute == :base
|
2018-08-14 11:52:26 -04:00
|
|
|
attribute = attribute.to_s
|
2018-05-17 08:39:31 -04:00
|
|
|
|
2018-05-22 17:20:14 -04:00
|
|
|
if self.class.i18n_full_message && @base.class.respond_to?(:i18n_scope)
|
2018-08-14 11:52:26 -04:00
|
|
|
attribute = attribute.remove(/\[\d\]/)
|
|
|
|
parts = attribute.split(".")
|
2018-05-17 08:39:31 -04:00
|
|
|
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}"
|
|
|
|
|
2018-08-14 11:52:26 -04:00
|
|
|
attr_name = attribute.tr(".", "_").humanize
|
2018-08-16 11:07:56 -04:00
|
|
|
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
|
2018-08-14 11:52:26 -04:00
|
|
|
|
2018-05-17 08:39:31 -04:00
|
|
|
I18n.t(defaults.shift,
|
|
|
|
default: defaults,
|
2013-05-01 20:10:06 -04:00
|
|
|
attribute: attr_name,
|
2016-08-06 13:44:11 -04:00
|
|
|
message: message)
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Translates an error message in its default scope
|
2010-06-14 05:10:57 -04:00
|
|
|
# (<tt>activemodel.errors.messages</tt>).
|
|
|
|
#
|
2015-01-12 10:21:56 -05:00
|
|
|
# Error messages are first looked up in <tt>activemodel.errors.models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>,
|
|
|
|
# if it's not there, it's looked up in <tt>activemodel.errors.models.MODEL.MESSAGE</tt> and if
|
2012-06-22 12:52:57 -04:00
|
|
|
# that is not there also, it returns the translation of the default message
|
|
|
|
# (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model
|
|
|
|
# name, translated attribute name and the value are available for
|
|
|
|
# interpolation.
|
2009-03-19 19:28:59 -04:00
|
|
|
#
|
2010-06-15 04:04:22 -04:00
|
|
|
# When using inheritance in your models, it will check all the inherited
|
2010-06-14 05:10:57 -04:00
|
|
|
# models too, but only if the model itself hasn't been found. Say you have
|
2010-08-14 01:13:00 -04:00
|
|
|
# <tt>class Admin < User; end</tt> and you wanted the translation for
|
2011-03-29 01:31:05 -04:00
|
|
|
# the <tt>:blank</tt> error message for the <tt>title</tt> attribute,
|
2010-06-14 05:10:57 -04:00
|
|
|
# it looks for these translations:
|
2009-06-08 21:32:08 -04:00
|
|
|
#
|
2011-03-29 01:31:05 -04:00
|
|
|
# * <tt>activemodel.errors.models.admin.attributes.title.blank</tt>
|
|
|
|
# * <tt>activemodel.errors.models.admin.blank</tt>
|
|
|
|
# * <tt>activemodel.errors.models.user.attributes.title.blank</tt>
|
|
|
|
# * <tt>activemodel.errors.models.user.blank</tt>
|
|
|
|
# * any default you provided through the +options+ hash (in the <tt>activemodel.errors</tt> scope)
|
|
|
|
# * <tt>activemodel.errors.messages.blank</tt>
|
|
|
|
# * <tt>errors.attributes.title.blank</tt>
|
|
|
|
# * <tt>errors.messages.blank</tt>
|
2010-05-19 10:25:46 -04:00
|
|
|
def generate_message(attribute, type = :invalid, options = {})
|
|
|
|
type = options.delete(:message) if options[:message].is_a?(Symbol)
|
|
|
|
|
2011-10-11 15:44:22 -04:00
|
|
|
if @base.class.respond_to?(:i18n_scope)
|
2017-08-18 07:35:43 -04:00
|
|
|
i18n_scope = @base.class.i18n_scope.to_s
|
|
|
|
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}" ]
|
2011-10-11 15:44:22 -04:00
|
|
|
end
|
2017-08-18 07:35:43 -04:00
|
|
|
defaults << :"#{i18n_scope}.errors.messages.#{type}"
|
2011-10-11 15:44:22 -04:00
|
|
|
else
|
|
|
|
defaults = []
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2010-05-19 10:25:46 -04:00
|
|
|
defaults << :"errors.attributes.#{attribute}.#{type}"
|
|
|
|
defaults << :"errors.messages.#{type}"
|
2010-01-30 07:07:48 -05:00
|
|
|
|
2009-03-19 19:28:59 -04:00
|
|
|
key = defaults.shift
|
2015-05-04 08:36:26 -04:00
|
|
|
defaults = options.delete(:message) if options[:message]
|
2010-06-23 08:41:28 -04:00
|
|
|
value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil)
|
2009-03-19 19:28:59 -04:00
|
|
|
|
2010-01-30 07:07:48 -05:00
|
|
|
options = {
|
2013-05-01 20:10:06 -04:00
|
|
|
default: defaults,
|
2014-06-20 18:49:27 -04:00
|
|
|
model: @base.model_name.human,
|
2013-05-01 20:10:06 -04:00
|
|
|
attribute: @base.class.human_attribute_name(attribute),
|
2016-03-09 05:06:45 -05:00
|
|
|
value: value,
|
|
|
|
object: @base
|
2012-04-27 20:02:51 -04:00
|
|
|
}.merge!(options)
|
2009-03-19 19:28:59 -04:00
|
|
|
|
|
|
|
I18n.translate(key, options)
|
|
|
|
end
|
2011-10-19 12:47:28 -04:00
|
|
|
|
2016-12-08 16:20:07 -05:00
|
|
|
def marshal_dump # :nodoc:
|
2016-05-30 14:00:14 -04:00
|
|
|
[@base, without_default_proc(@messages), without_default_proc(@details)]
|
|
|
|
end
|
|
|
|
|
2016-12-08 16:20:07 -05:00
|
|
|
def marshal_load(array) # :nodoc:
|
2016-05-30 14:00:14 -04:00
|
|
|
@base, @messages, @details = array
|
|
|
|
apply_default_array(@messages)
|
|
|
|
apply_default_array(@details)
|
|
|
|
end
|
|
|
|
|
2016-12-08 16:20:07 -05:00
|
|
|
def init_with(coder) # :nodoc:
|
|
|
|
coder.map.each { |k, v| instance_variable_set(:"@#{k}", v) }
|
|
|
|
@details ||= {}
|
|
|
|
apply_default_array(@messages)
|
|
|
|
apply_default_array(@details)
|
|
|
|
end
|
|
|
|
|
2011-10-19 12:47:28 -04:00
|
|
|
private
|
|
|
|
def normalize_message(attribute, message, options)
|
2012-04-27 20:02:51 -04:00
|
|
|
case message
|
|
|
|
when Symbol
|
2011-10-19 12:47:28 -04:00
|
|
|
generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS))
|
|
|
|
else
|
|
|
|
message
|
|
|
|
end
|
|
|
|
end
|
2015-01-04 03:18:03 -05:00
|
|
|
|
2016-02-17 16:09:54 -05:00
|
|
|
def normalize_detail(message, options)
|
2015-01-04 03:18:03 -05:00
|
|
|
{ error: message }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS))
|
|
|
|
end
|
2016-05-30 14:00:14 -04:00
|
|
|
|
|
|
|
def without_default_proc(hash)
|
|
|
|
hash.dup.tap do |new_h|
|
|
|
|
new_h.default_proc = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_default_array(hash)
|
|
|
|
hash.default_proc = proc { |h, key| h[key] = [] }
|
|
|
|
hash
|
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2011-08-17 10:26:00 -04:00
|
|
|
|
2012-06-23 15:49:22 -04:00
|
|
|
# Raised when a validation cannot be corrected by end users and are considered
|
2012-06-25 03:23:33 -04:00
|
|
|
# exceptional.
|
2012-07-28 15:24:56 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
#
|
|
|
|
# validates_presence_of :name, strict: true
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.name = nil
|
|
|
|
# person.valid?
|
|
|
|
# # => ActiveModel::StrictValidationFailed: Name can't be blank
|
2011-08-17 10:26:00 -04:00
|
|
|
class StrictValidationFailed < StandardError
|
|
|
|
end
|
2015-02-25 09:14:43 -05:00
|
|
|
|
2016-05-03 14:37:11 -04:00
|
|
|
# Raised when attribute values are out of range.
|
|
|
|
class RangeError < ::RangeError
|
|
|
|
end
|
|
|
|
|
2015-02-25 09:14:43 -05:00
|
|
|
# Raised when unknown attributes are supplied via mass assignment.
|
2016-05-01 17:38:59 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeAssignment
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.assign_attributes(name: 'Gorby')
|
|
|
|
# # => ActiveModel::UnknownAttributeError: unknown attribute 'name' for Person.
|
2015-02-25 09:14:43 -05:00
|
|
|
class UnknownAttributeError < NoMethodError
|
|
|
|
attr_reader :record, :attribute
|
|
|
|
|
|
|
|
def initialize(record, attribute)
|
|
|
|
@record = record
|
|
|
|
@attribute = attribute
|
|
|
|
super("unknown attribute '#{attribute}' for #{@record.class}.")
|
|
|
|
end
|
|
|
|
end
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|