Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 146 lines (138 sloc) 4.908 kB
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
1 module ActiveModel
2 module Validations
9216021 @joshk minor changes to instance level validations implementation based on f…
joshk authored
3 module HelperMethods
4 private
5 def _merge_attributes(attr_names)
6 options = attr_names.extract_options!
7 options.merge(:attributes => attr_names.flatten)
8 end
9 end
10
e9e9ed6 @carllerche Be able to pass a validator method to #validates
carllerche authored
11 class WithValidator < EachValidator
12 def validate_each(record, attr, val)
cd13fbd @carllerche Optionally pass in the attribute being validated to an instance metho…
carllerche authored
13 method_name = options[:with]
14
15 if record.method(method_name).arity == 0
16 record.send method_name
17 else
18 record.send method_name, attr
19 end
e9e9ed6 @carllerche Be able to pass a validator method to #validates
carllerche authored
20 end
21 end
22
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
23 module ClassMethods
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
24 # Passes the record off to the class or classes specified and allows them
25 # to add errors based on more complex conditions.
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
26 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
27 # class Person
28 # include ActiveModel::Validations
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
29 # validates_with MyValidator
30 # end
31 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
32 # class MyValidator < ActiveModel::Validator
33 # def validate(record)
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
34 # if some_complex_logic
c317419 @dolzenko Use .add instead of << to add errors
dolzenko authored
35 # record.errors.add :base, "This record is invalid"
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
36 # end
37 # end
38 #
39 # private
40 # def some_complex_logic
41 # # ...
42 # end
43 # end
44 #
45 # You may also pass it multiple classes, like so:
46 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
47 # class Person
48 # include ActiveModel::Validations
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
49 # validates_with MyValidator, MyOtherValidator, :on => :create
50 # end
51 #
52 # Configuration options:
8e0affb @pacoguzman Configuration options are symbols
pacoguzman authored
53 # * <tt>:on</tt> - Specifies when this validation is active
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
54 # (<tt>:create</tt> or <tt>:update</tt>
8e0affb @pacoguzman Configuration options are symbols
pacoguzman authored
55 # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
56 # if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
57 # or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
58 # The method, proc or string should return or evaluate to a true or false value.
448df2d @avakhov Cosmetic fixes in AM validatations docs
avakhov authored
59 # * <tt>:unless</tt> - Specifies a method, proc or string to call to
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
60 # determine if the validation should not occur
61 # (e.g. <tt>:unless => :skip_validation</tt>, or
62 # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>).
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
63 # The method, proc or string should return or evaluate to a true or false value.
8620bf9 @bogdan Implemented strict validation concept
bogdan authored
64 # * <tt>:strict</tt> - Specifies whether validation should be strict.
65 # See <tt>ActiveModel::Validation#validates!</tt> for more information
66
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
67 # If you pass any additional configuration options, they will be passed
68 # to the class and available as <tt>options</tt>:
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
69 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
70 # class Person
71 # include ActiveModel::Validations
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
72 # validates_with MyValidator, :my_custom_key => "my custom value"
73 # end
74 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
75 # class MyValidator < ActiveModel::Validator
76 # def validate(record)
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
77 # options[:my_custom_key] # => "my custom value"
78 # end
79 # end
80 #
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored
81 def validates_with(*args, &block)
2476c53 @josevalim Validator is simply sent to validate method. However, the API needs t…
josevalim authored
82 options = args.extract_options!
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
83 args.each do |klass|
84 validator = klass.new(options, &block)
85 validator.setup(self) if validator.respond_to?(:setup)
8f97e9d @sikachu Add validators reflection so you can do 'Person.validators' and 'Pers…
sikachu authored
86
87 if validator.respond_to?(:attributes) && !validator.attributes.empty?
88 validator.attributes.each do |attribute|
89 _validators[attribute.to_sym] << validator
90 end
91 else
92 _validators[nil] << validator
93 end
94
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored
95 validate(validator, options)
96 end
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
97 end
98 end
9131a88 @joshk validation macros can now be used within an instance
joshk authored
99
100 # Passes the record off to the class or classes specified and allows them
101 # to add errors based on more complex conditions.
102 #
103 # class Person
104 # include ActiveModel::Validations
105 #
c02288e @bmarini Fix example code for #validates_with instance method
bmarini authored
106 # validate :instance_validations
9131a88 @joshk validation macros can now be used within an instance
joshk authored
107 #
108 # def instance_validations
109 # validates_with MyValidator
110 # end
111 # end
112 #
9216021 @joshk minor changes to instance level validations implementation based on f…
joshk authored
113 # Please consult the class method documentation for more information on
114 # creating your own validator.
9131a88 @joshk validation macros can now be used within an instance
joshk authored
115 #
116 # You may also pass it multiple classes, like so:
117 #
118 # class Person
119 # include ActiveModel::Validations
120 #
c02288e @bmarini Fix example code for #validates_with instance method
bmarini authored
121 # validate :instance_validations, :on => :create
9131a88 @joshk validation macros can now be used within an instance
joshk authored
122 #
123 # def instance_validations
124 # validates_with MyValidator, MyOtherValidator
125 # end
126 # end
127 #
128 # Standard configuration options (:on, :if and :unless), which are
8c16333 @oscardelben minor docs improvements
oscardelben authored
129 # available on the class version of +validates_with+, should instead be
130 # placed on the +validates+ method as these are applied and tested
9131a88 @joshk validation macros can now be used within an instance
joshk authored
131 # in the callback
132 #
133 # If you pass any additional configuration options, they will be passed
8c16333 @oscardelben minor docs improvements
oscardelben authored
134 # to the class and available as +options+, please refer to the
9216021 @joshk minor changes to instance level validations implementation based on f…
joshk authored
135 # class version of this method for more information
9131a88 @joshk validation macros can now be used within an instance
joshk authored
136 #
137 def validates_with(*args, &block)
138 options = args.extract_options!
139 args.each do |klass|
140 validator = klass.new(options, &block)
141 validator.validate(self)
142 end
143 end
22f3398 @zilkey Introduce validates_with to encapsulate attribute validations in a cl…
zilkey authored
144 end
8620bf9 @bogdan Implemented strict validation concept
bogdan authored
145 end
Something went wrong with that request. Please try again.