Skip to content
Newer
Older
100644 44 lines (42 sloc) 2.13 KB
638333b @lifo Move uniqueness and association validations to Active Record
lifo authored Mar 19, 2009
1 module ActiveRecord
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
2 module Validations
44cd9e0 @josevalim ActiveRecord::Validations are now built on top of Validator as well.
josevalim authored Dec 23, 2009
3 class AssociatedValidator < ActiveModel::EachValidator
4 def validate_each(record, attribute, value)
a8134ac @exviva Do not validate associated records marked for destruction
exviva authored Dec 4, 2011
5 if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?}.any?
6 record.errors.add(attribute, :invalid, options.merge(:value => value))
7 end
44cd9e0 @josevalim ActiveRecord::Validations are now built on top of Validator as well.
josevalim authored Dec 23, 2009
8 end
9 end
10
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
11 module ClassMethods
12 # Validates whether the associated object or objects are all valid themselves. Works with any kind of association.
13 #
14 # class Book < ActiveRecord::Base
15 # has_many :pages
16 # belongs_to :library
17 #
18 # validates_associated :pages, :library
19 # end
20 #
38bfcff @vijaydev make the warning clear about the effect of using validates_associated…
vijaydev authored Jul 30, 2011
21 # WARNING: This validation must not be used on both ends of an association. Doing so will lead to a circular dependency and cause infinite recursion.
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
22 #
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored Aug 14, 2010
23 # NOTE: This validation will not fail if the association hasn't been assigned. If you want to
24 # ensure that the association is both present and guaranteed to be valid, you also need to
1ce40ca ensuring that description does not exceed 100 columns
Neeraj Singh authored Aug 2, 2010
25 # use +validates_presence_of+.
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
26 #
27 # Configuration options:
64092de @fxn Improve documentation coverage and markup
fxn authored May 2, 2008
28 # * <tt>:message</tt> - A custom error message (default is: "is invalid")
b481574 @fxn copy-edits 8d96b89
fxn authored Feb 21, 2011
29 # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
30 # validation contexts by default (+nil+), other options are <tt>:create</tt>
31 # and <tt>:update</tt>.
64092de @fxn Improve documentation coverage and markup
fxn authored May 2, 2008
32 # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
0034b78 @smartinez87 Remove extra white spaces on ActiveRecord docs.
smartinez87 authored May 23, 2011
33 # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
34 # method, proc or string should return or evaluate to a true or false value.
64092de @fxn Improve documentation coverage and markup
fxn authored May 2, 2008
35 # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0034b78 @smartinez87 Remove extra white spaces on ActiveRecord docs.
smartinez87 authored May 23, 2011
36 # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
37 # method, proc or string should return or evaluate to a true or false value.
38 def validates_associated(*attr_names)
93898b3 @josevalim Ensure new validates works with uniqueness validator.
josevalim authored Jan 8, 2010
39 validates_with AssociatedValidator, _merge_attributes(attr_names)
87535bd @dhh Splitting them up first
dhh authored Apr 1, 2008
40 end
41 end
42 end
1ce40ca ensuring that description does not exceed 100 columns
Neeraj Singh authored Aug 2, 2010
43 end
Something went wrong with that request. Please try again.