Skip to content
Newer
Older
100644 222 lines (202 sloc) 7.38 KB
28f3627 @josh Properly require ActiveModel validation dependencies
josh authored Jun 8, 2009
1 require 'active_support/core_ext/array/extract_options'
a98db7c @jeremy Use Array.wrap uniformly
jeremy authored Mar 27, 2010
2 require 'active_support/core_ext/array/wrap'
8f97e9d @sikachu Add validators reflection so you can do 'Person.validators' and 'Pers…
sikachu authored Feb 18, 2010
3 require 'active_support/core_ext/class/attribute'
28f3627 @josh Properly require ActiveModel validation dependencies
josh authored Jun 9, 2009
4 require 'active_support/core_ext/hash/keys'
90a7915 @gcampbell Add missing ActiveModel::Validations require
gcampbell authored Aug 5, 2010
5 require 'active_support/core_ext/hash/except'
f737c2d @josh All AMo modules are safe to defer
josh authored Dec 22, 2009
6 require 'active_model/errors'
51739d3 moving before_validation and after_validation functionality from Acti…
Neeraj Singh authored Jun 16, 2010
7 require 'active_model/validations/callbacks'
28f3627 @josh Properly require ActiveModel validation dependencies
josh authored Jun 9, 2009
8
5dc3f91 @technoweenie initial experimental commit of active_model
technoweenie authored Nov 9, 2007
9 module ActiveModel
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
10
11 # == Active Model Validations
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
12 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Feb 1, 2010
13 # Provides a full validation framework to your objects.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
14 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
15 # A minimal implementation could be:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
16 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
17 # class Person
18 # include ActiveModel::Validations
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
19 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
20 # attr_accessor :first_name, :last_name
21 #
22 # validates_each :first_name, :last_name do |record, attr, value|
23 # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
24 # end
25 # end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
26 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
27 # Which provides you with the full standard validation stack that you
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored Jul 30, 2010
28 # know from Active Record:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
29 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
30 # person = Person.new
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored Jul 30, 2010
31 # person.valid? # => true
32 # person.invalid? # => false
33 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
34 # person.first_name = 'zoolander'
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored Jul 30, 2010
35 # person.valid? # => false
36 # person.invalid? # => true
37 # person.errors # => #<OrderedHash {:first_name=>["starts with z."]}>
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
38 #
f99db5a @vijaydev formatting changes
vijaydev authored Mar 7, 2011
39 # Note that <tt>ActiveModel::Validations</tt> automatically adds an +errors+ method
40 # to your instances initialized with a new <tt>ActiveModel::Errors</tt> object, so
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
41 # there is no need for you to do this manually.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
42 #
5dc3f91 @technoweenie initial experimental commit of active_model
technoweenie authored Nov 9, 2007
43 module Validations
e7d6f48 @josh Use AS::Concern in ActiveModel
josh authored May 29, 2009
44 extend ActiveSupport::Concern
21e7b84 @josh Callbacks, DeprecatedCallbacks = NewCallbacks, Callbacks
josh authored Oct 12, 2009
45 include ActiveSupport::Callbacks
e7d6f48 @josh Use AS::Concern in ActiveModel
josh authored May 29, 2009
46
47 included do
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored Oct 20, 2009
48 extend ActiveModel::Translation
9131a88 @joshk validation macros can now be used within an instance
joshk authored May 11, 2010
49
9216021 @joshk minor changes to instance level validations implementation based on f…
joshk authored May 11, 2010
50 extend HelperMethods
51 include HelperMethods
9131a88 @joshk validation macros can now be used within an instance
joshk authored May 11, 2010
52
8248506 @joshk updated AMo validations to use a context for valid? and invalid?, rem…
joshk authored May 8, 2010
53 attr_accessor :validation_context
b67ec8b @josevalim class_attribute is not a direct replacement of class_inheritable_*.
josevalim authored Jun 10, 2010
54 define_callbacks :validate, :scope => :name
8248506 @joshk updated AMo validations to use a context for valid? and invalid?, rem…
joshk authored May 8, 2010
55
b67ec8b @josevalim class_attribute is not a direct replacement of class_inheritable_*.
josevalim authored Jun 10, 2010
56 class_attribute :_validators
8f97e9d @sikachu Add validators reflection so you can do 'Person.validators' and 'Pers…
sikachu authored Feb 18, 2010
57 self._validators = Hash.new { |h,k| h[k] = [] }
9ddc614 @dhh Something more to work with
dhh authored Mar 31, 2008
58 end
59
60 module ClassMethods
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored Dec 23, 2009
61 # Validates each attribute against a block.
62 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored Jan 7, 2010
63 # class Person
64 # include ActiveModel::Validations
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
65 #
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
66 # attr_accessor :first_name, :last_name
67 #
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored Dec 23, 2009
68 # validates_each :first_name, :last_name do |record, attr, value|
a07d0f8 @mikel Full update on ActiveModel documentation
mikel authored Jan 31, 2010
69 # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored Dec 23, 2009
70 # end
71 # end
72 #
73 # Options:
b481574 @fxn copy-edits 8d96b89
fxn authored Feb 21, 2011
74 # * <tt>:on</tt> - Specifies the context where this validation is active
75 # (e.g. <tt>:on => :create</tt> or <tt>:on => :custom_validation_context</tt>)
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored Dec 23, 2009
76 # * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
77 # * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
78 # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
79 # if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
80 # or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method,
81 # proc or string should return or evaluate to a true or false value.
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored Dec 23, 2009
82 # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored Jan 7, 2010
83 # not occur (e.g. <tt>:unless => :skip_validation</tt>, or
2a4b780 @smartinez87 Remove extra white spaces on ActiveModel docs.
smartinez87 authored May 23, 2011
84 # <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
2790676 @josevalim validates_each uses a BlockValidator.
josevalim authored Dec 23, 2009
85 # method, proc or string should return or evaluate to a true or false value.
86 def validates_each(*attr_names, &block)
87 options = attr_names.extract_options!.symbolize_keys
88 validates_with BlockValidator, options.merge(:attributes => attr_names.flatten), &block
89 end
90
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
91 # Adds a validation method or block to the class. This is useful when
ccf9577 @dolzenko Fix a bunch of minor spelling mistakes
dolzenko authored Jun 11, 2010
92 # overriding the +validate+ instance method becomes too unwieldy and
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
93 # you're looking for more descriptive declaration of your validations.
94 #
95 # This can be done with a symbol pointing to a method:
96 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored Jan 7, 2010
97 # class Comment
98 # include ActiveModel::Validations
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
99 #
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
100 # validate :must_be_friends
101 #
102 # def must_be_friends
bca9b84 @mikel Fixing documentation to reflect deprecated add_to_base
mikel authored Sep 12, 2010
103 # errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee)
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
104 # end
105 # end
106 #
5fb42ac @spastorino Tests and docs which explain the use of validate with a block and wit…
spastorino authored Dec 19, 2010
107 # With a block which is passed with the current record to be validated:
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
108 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored Jan 7, 2010
109 # class Comment
110 # include ActiveModel::Validations
111 #
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
112 # validate do |comment|
113 # comment.must_be_friends
114 # end
115 #
116 # def must_be_friends
4ea9a8d @fastred Fix syntax error in ActiveModel::Validations .validate example
fastred authored Oct 1, 2010
117 # errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee)
d950add @dhh Move it around a bit
dhh authored Apr 1, 2008
118 # end
119 # end
120 #
5fb42ac @spastorino Tests and docs which explain the use of validate with a block and wit…
spastorino authored Dec 19, 2010
121 # Or with a block where self points to the current record to be validated:
122 #
123 # class Comment
124 # include ActiveModel::Validations
125 #
126 # validate do
127 # errors.add(:base, "Must be friends to leave a comment") unless commenter.friend_of?(commentee)
128 # end
129 # end
130 #
4f37b97 @josevalim Changed ActiveRecord to use new callbacks and speed up observers by o…
josevalim authored Sep 8, 2009
131 def validate(*args, &block)
84081fc @josevalim Freeze options so we raise an error when people modify it in place.
josevalim authored Aug 3, 2010
132 options = args.extract_options!
133 if options.key?(:on)
134 options = options.dup
a98db7c @jeremy Use Array.wrap uniformly
jeremy authored Mar 27, 2010
135 options[:if] = Array.wrap(options[:if])
bf5cf5d @asanghi :if should not fire on validations when not in context with :on
asanghi authored Apr 29, 2011
136 options[:if].unshift("validation_context == :#{options[:on]}")
28f3627 @josh Properly require ActiveModel validation dependencies
josh authored Jun 9, 2009
137 end
84081fc @josevalim Freeze options so we raise an error when people modify it in place.
josevalim authored Aug 3, 2010
138 args << options
2ea1d68 @josevalim Refactor new callbacks and AR implementation.
josevalim authored Sep 8, 2009
139 set_callback(:validate, *args, &block)
4f37b97 @josevalim Changed ActiveRecord to use new callbacks and speed up observers by o…
josevalim authored Sep 8, 2009
140 end
8f97e9d @sikachu Add validators reflection so you can do 'Person.validators' and 'Pers…
sikachu authored Feb 18, 2010
141
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
142 # List all validators that are being used to validate the model using
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
143 # +validates_with+ method.
8f97e9d @sikachu Add validators reflection so you can do 'Person.validators' and 'Pers…
sikachu authored Feb 18, 2010
144 def validators
145 _validators.values.flatten.uniq
146 end
147
148 # List all validators that being used to validate a specific attribute.
cf9324e @carllerche Find all validators for multiple attributes
carllerche authored Feb 5, 2011
149 def validators_on(*attributes)
933adce @spastorino Use map + flatten here
spastorino authored Feb 7, 2011
150 attributes.map do |attribute|
151 _validators[attribute.to_sym]
152 end.flatten
8f97e9d @sikachu Add validators reflection so you can do 'Person.validators' and 'Pers…
sikachu authored Feb 18, 2010
153 end
154
b67ec8b @josevalim class_attribute is not a direct replacement of class_inheritable_*.
josevalim authored Jun 10, 2010
155 # Check if method is an attribute method or not.
ce48b31 @spastorino Makes validates_acceptance_of to not override database fields [#4460
spastorino authored Apr 28, 2010
156 def attribute_method?(attribute)
157 method_defined?(attribute)
158 end
b67ec8b @josevalim class_attribute is not a direct replacement of class_inheritable_*.
josevalim authored Jun 10, 2010
159
160 # Copy validators on inheritance.
161 def inherited(base)
162 dup = _validators.dup
163 base._validators = dup.each { |k, v| dup[k] = v.dup }
164 super
165 end
9ddc614 @dhh Something more to work with
dhh authored Mar 31, 2008
166 end
167
f99db5a @vijaydev formatting changes
vijaydev authored Mar 7, 2011
168 # Returns the +Errors+ object that holds all information about attribute error messages.
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
169 def errors
8828b2c @lifo Move all the Active Record validations to Active Model
lifo authored Mar 19, 2009
170 @errors ||= Errors.new(self)
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
171 end
9ddc614 @dhh Something more to work with
dhh authored Mar 31, 2008
172
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
173 # Runs all the specified validations and returns true if no errors were added
174 # otherwise false. Context can optionally be supplied to define which callbacks
175 # to test against (the context is defined on the validations using :on).
8248506 @joshk updated AMo validations to use a context for valid? and invalid?, rem…
joshk authored May 8, 2010
176 def valid?(context = nil)
177 current_context, self.validation_context = validation_context, context
9ddc614 @dhh Something more to work with
dhh authored Mar 31, 2008
178 errors.clear
0247995 @josevalim ActiveModel::Validations::Callbacks should not be required by default.
josevalim authored Jun 19, 2010
179 run_validations!
8248506 @joshk updated AMo validations to use a context for valid? and invalid?, rem…
joshk authored May 8, 2010
180 ensure
181 self.validation_context = current_context
9ddc614 @dhh Something more to work with
dhh authored Mar 31, 2008
182 end
8828b2c @lifo Move all the Active Record validations to Active Model
lifo authored Mar 19, 2009
183
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
184 # Performs the opposite of <tt>valid?</tt>. Returns true if errors were added,
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
185 # false otherwise.
8248506 @joshk updated AMo validations to use a context for valid? and invalid?, rem…
joshk authored May 8, 2010
186 def invalid?(context = nil)
187 !valid?(context)
8828b2c @lifo Move all the Active Record validations to Active Model
lifo authored Mar 19, 2009
188 end
21e7b84 @josh Callbacks, DeprecatedCallbacks = NewCallbacks, Callbacks
josh authored Oct 13, 2009
189
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
190 # Hook method defining how an attribute value should be retrieved. By default
191 # this is assumed to be an instance named after the attribute. Override this
7d7d54f @rizwanreza Revised the rest of the files from ActiveModel.
rizwanreza authored Jun 14, 2010
192 # method in subclasses should you need to retrieve the value for a given
193 # attribute differently:
194 #
0a79eb7 @thelucid Add validates method as shortcut to setup validators for a given set …
thelucid authored Jan 7, 2010
195 # class MyClass
196 # include ActiveModel::Validations
197 #
198 # def initialize(data = {})
199 # @data = data
200 # end
201 #
202 # def read_attribute_for_validation(key)
203 # @data[key]
204 # end
205 # end
206 #
47a5fd4 @josevalim Allow :if, :unless, :on, :allow_nil and :allow_blank as shared option…
josevalim authored Jan 7, 2010
207 alias :read_attribute_for_validation :send
0247995 @josevalim ActiveModel::Validations::Callbacks should not be required by default.
josevalim authored Jun 19, 2010
208
209 protected
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
210
0247995 @josevalim ActiveModel::Validations::Callbacks should not be required by default.
josevalim authored Jun 19, 2010
211 def run_validations!
57bc25c @jfirebaugh Use run_callbacks; the generated _run_<name>_callbacks method is not …
jfirebaugh authored Jan 9, 2011
212 run_callbacks :validate
0247995 @josevalim ActiveModel::Validations::Callbacks should not be required by default.
josevalim authored Jun 19, 2010
213 errors.empty?
214 end
5dc3f91 @technoweenie initial experimental commit of active_model
technoweenie authored Nov 9, 2007
215 end
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
216 end
217
218 Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
219 filename = File.basename(path)
220 require "active_model/validations/#{filename}"
8828b2c @lifo Move all the Active Record validations to Active Model
lifo authored Mar 19, 2009
221 end
Something went wrong with that request. Please try again.