2012-03-28 16:52:52 -04:00
|
|
|
require "active_model/validations/clusivity"
|
2011-02-19 18:13:33 -05:00
|
|
|
|
2008-03-31 20:05:48 -04:00
|
|
|
module ActiveModel
|
2012-05-16 11:01:43 -04:00
|
|
|
|
2008-03-31 20:05:48 -04:00
|
|
|
module Validations
|
2012-10-25 21:41:35 -04:00
|
|
|
class InclusionValidator < EachValidator # :nodoc:
|
2012-03-28 16:52:52 -04:00
|
|
|
include Clusivity
|
2009-12-22 19:08:27 -05:00
|
|
|
|
2011-02-19 18:13:33 -05:00
|
|
|
def validate_each(record, attribute, value)
|
2012-03-28 16:52:52 -04:00
|
|
|
unless include?(record, value)
|
2013-05-01 20:10:06 -04:00
|
|
|
record.errors.add(attribute, :inclusion, options.except(:in, :within).merge!(value: value))
|
2011-03-11 00:35:23 -05:00
|
|
|
end
|
2011-02-19 18:13:33 -05:00
|
|
|
end
|
2009-12-22 18:36:51 -05:00
|
|
|
end
|
|
|
|
|
2010-05-11 06:28:42 -04:00
|
|
|
module HelperMethods
|
2012-05-16 02:23:46 -04:00
|
|
|
# Validates whether the value of the specified attribute is available in a
|
|
|
|
# particular enumerable object.
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# class Person < ActiveRecord::Base
|
2012-07-05 01:09:29 -04:00
|
|
|
# validates_inclusion_of :gender, in: %w( m f )
|
|
|
|
# validates_inclusion_of :age, in: 0..99
|
|
|
|
# validates_inclusion_of :format, in: %w( jpg gif png ), message: "extension %{value} is not included in the list"
|
2012-07-05 12:19:20 -04:00
|
|
|
# validates_inclusion_of :states, in: ->(person) { STATES[person.country] }
|
2012-08-12 16:38:22 -04:00
|
|
|
# validates_inclusion_of :karma, in: :available_karmas
|
2008-03-31 20:05:48 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Configuration options:
|
2011-03-11 00:35:23 -05:00
|
|
|
# * <tt>:in</tt> - An enumerable object of available items. This can be
|
2012-08-12 16:38:22 -04:00
|
|
|
# supplied as a proc, lambda or symbol which returns an enumerable. If the
|
2013-05-28 02:07:05 -04:00
|
|
|
# enumerable is a numerical range the test is performed with <tt>Range#cover?</tt>,
|
2014-01-13 05:25:58 -05:00
|
|
|
# otherwise with <tt>include?</tt>. When using a proc or lambda the instance
|
|
|
|
# under validation is passed as an argument.
|
2012-07-20 13:23:05 -04:00
|
|
|
# * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
|
2012-07-05 01:09:29 -04:00
|
|
|
# * <tt>:message</tt> - Specifies a custom error message (default is: "is
|
|
|
|
# not included in the list").
|
2012-07-10 03:07:38 -04:00
|
|
|
#
|
|
|
|
# There is also a list of default options supported by every validator:
|
2014-01-26 14:47:56 -05:00
|
|
|
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
2012-07-10 03:07:38 -04:00
|
|
|
# See <tt>ActiveModel::Validation#validates</tt> for more information
|
2008-03-31 20:05:48 -04:00
|
|
|
def validates_inclusion_of(*attr_names)
|
2010-01-07 12:44:35 -05:00
|
|
|
validates_with InclusionValidator, _merge_attributes(attr_names)
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|