Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 51 lines (46 sloc) 2.307 kb
170956c @rafaelfranca Remove code duplication in InclusionValidator and ExclusionValidator.
rafaelfranca authored
1 require "active_model/validations/clusivity"
58594be @sikachu Add support for proc or lambda as an option for InclusionValidator, E…
sikachu authored
2
87535bd @dhh Splitting them up first
dhh authored
3 module ActiveModel
3004bc2 @frodsan fixing removed empty lines and examples
frodsan authored
4
87535bd @dhh Splitting them up first
dhh authored
5 module Validations
9ac095f @frodsan minor edits and remove mixed titles in AM::Validations docs [ci skip]
frodsan authored
6 class ExclusionValidator < EachValidator # :nodoc:
170956c @rafaelfranca Remove code duplication in InclusionValidator and ExclusionValidator.
rafaelfranca authored
7 include Clusivity
977a5c4 @josevalim Added check_validity! to EachValidator and refactor existing ones.
josevalim authored
8
f1085f4 @josevalim Move validations in ActiveModel to validators, however all validatity…
josevalim authored
9 def validate_each(record, attribute, value)
170956c @rafaelfranca Remove code duplication in InclusionValidator and ExclusionValidator.
rafaelfranca authored
10 if include?(record, value)
eebb9dd @patricksrobertson Convert ActiveModel to 1.9 hash syntax.
patricksrobertson authored
11 record.errors.add(attribute, :exclusion, options.except(:in, :within).merge!(value: value))
26392c4 @jeroenvandijk Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an opt…
jeroenvandijk authored
12 end
58594be @sikachu Add support for proc or lambda as an option for InclusionValidator, E…
sikachu authored
13 end
f1085f4 @josevalim Move validations in ActiveModel to validators, however all validatity…
josevalim authored
14 end
15
9131a88 @joshk validation macros can now be used within an instance
joshk authored
16 module HelperMethods
95e14d1 @frodsan better format and fixes to validators docs
frodsan authored
17 # Validates that the value of the specified attribute is not in a
18 # particular enumerable object.
87535bd @dhh Splitting them up first
dhh authored
19 #
20 # class Person < ActiveRecord::Base
68df230 @frodsan update ActiveModel::Validations::HelperMethods documentation [ci skip]
frodsan authored
21 # validates_exclusion_of :username, in: %w( admin superuser ), message: "You don't belong here"
22 # validates_exclusion_of :age, in: 30..60, message: 'This site is only for under 30 and over 60'
23 # validates_exclusion_of :format, in: %w( mov avi ), message: "extension %{value} is not allowed"
70c4676 @frodsan fix syntax of AM::Validations::HelperMethods examples [ci skip]
frodsan authored
24 # validates_exclusion_of :password, in: ->(person) { [person.username, person.first_name] },
68df230 @frodsan update ActiveModel::Validations::HelperMethods documentation [ci skip]
frodsan authored
25 # message: 'should not be the same as your username or first name'
2f3eb48 @sobrinho Accept a symbol for `:in` option on inclusion and exclusion validators
sobrinho authored
26 # validates_exclusion_of :karma, in: :reserved_karmas
87535bd @dhh Splitting them up first
dhh authored
27 # end
28 #
29 # Configuration options:
68df230 @frodsan update ActiveModel::Validations::HelperMethods documentation [ci skip]
frodsan authored
30 # * <tt>:in</tt> - An enumerable object of items that the value shouldn't
2f3eb48 @sobrinho Accept a symbol for `:in` option on inclusion and exclusion validators
sobrinho authored
31 # be part of. This can be supplied as a proc, lambda or symbol which returns an
95e14d1 @frodsan better format and fixes to validators docs
frodsan authored
32 # enumerable. If the enumerable is a range the test is performed with
a63f7a1 @rafaelfranca Update documentation and change the range method name to delimiter.
rafaelfranca authored
33 # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
588bb6b @amatsuda Range#cover? is not implemented in AS now
amatsuda authored
34 # <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>.
95e14d1 @frodsan better format and fixes to validators docs
frodsan authored
35 # * <tt>:message</tt> - Specifies a custom error message (default is: "is
36 # reserved").
68df230 @frodsan update ActiveModel::Validations::HelperMethods documentation [ci skip]
frodsan authored
37 # * <tt>:allow_nil</tt> - If set to true, skips this validation if the
38 # attribute is +nil+ (default is +false+).
95e14d1 @frodsan better format and fixes to validators docs
frodsan authored
39 # * <tt>:allow_blank</tt> - If set to true, skips this validation if the
40 # attribute is blank(default is +false+).
c175563 @bogdan AM::Validations: remove documentation duplicates
bogdan authored
41 #
42 # There is also a list of default options supported by every validator:
43 # +:if+, +:unless+, +:on+ and +:strict+.
44 # See <tt>ActiveModel::Validation#validates</tt> for more information
87535bd @dhh Splitting them up first
dhh authored
45 def validates_exclusion_of(*attr_names)
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
46 validates_with ExclusionValidator, _merge_attributes(attr_names)
87535bd @dhh Splitting them up first
dhh authored
47 end
48 end
49 end
28f3627 @josh Properly require ActiveModel validation dependencies
josh authored
50 end
Something went wrong with that request. Please try again.