aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations/associated.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-12-23 12:14:00 +0100
committerJosé Valim <jose.valim@gmail.com>2009-12-23 12:14:00 +0100
commit44cd9e0e7132abe632664377f13f3edd1106685a (patch)
tree71373ffe546443361cb9e33010de8c73a835c073 /activerecord/lib/active_record/validations/associated.rb
parent279067639f319f3b4bbcaf90c26f286e96df2c77 (diff)
downloadrails-44cd9e0e7132abe632664377f13f3edd1106685a.tar.gz
rails-44cd9e0e7132abe632664377f13f3edd1106685a.tar.bz2
rails-44cd9e0e7132abe632664377f13f3edd1106685a.zip
ActiveRecord::Validations are now built on top of Validator as well.
Diffstat (limited to 'activerecord/lib/active_record/validations/associated.rb')
-rw-r--r--activerecord/lib/active_record/validations/associated.rb16
1 files changed, 9 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/validations/associated.rb b/activerecord/lib/active_record/validations/associated.rb
index 92f47d770f..6e6f4df415 100644
--- a/activerecord/lib/active_record/validations/associated.rb
+++ b/activerecord/lib/active_record/validations/associated.rb
@@ -1,5 +1,12 @@
module ActiveRecord
module Validations
+ class AssociatedValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ return if (value.is_a?(Array) ? value : [value]).compact.all?{ |r| r.valid? }
+ record.errors.add(attribute, :invalid, :default => options[:message], :value => value)
+ end
+ end
+
module ClassMethods
# Validates whether the associated object or objects are all valid themselves. Works with any kind of association.
#
@@ -33,13 +40,8 @@ module ActiveRecord
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value.
def validates_associated(*attr_names)
- configuration = attr_names.extract_options!
-
- validates_each(attr_names, configuration) do |record, attr_name, value|
- unless (value.is_a?(Array) ? value : [value]).collect { |r| r.nil? || r.valid? }.all?
- record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
- end
- end
+ options = attr_names.extract_options!
+ validates_with AssociatedValidator, options.merge(:attributes => attr_names)
end
end
end