diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-12-28 11:13:35 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-12-28 11:13:35 -0800 |
commit | 632df063a33fab68b50ed893630af7f38821878d (patch) | |
tree | ef76a8193129d9e51a86226224021ba63fb6d1b7 /activerecord/lib/active_record/validations/associated.rb | |
parent | 91e28aae8649c503e81d66ad6829403ccc2c6571 (diff) | |
parent | 74098e4cb6de01745db8f1d8d567645553ade7c5 (diff) | |
download | rails-632df063a33fab68b50ed893630af7f38821878d.tar.gz rails-632df063a33fab68b50ed893630af7f38821878d.tar.bz2 rails-632df063a33fab68b50ed893630af7f38821878d.zip |
Merge commit 'josevalim/validations'
Diffstat (limited to 'activerecord/lib/active_record/validations/associated.rb')
-rw-r--r-- | activerecord/lib/active_record/validations/associated.rb | 16 |
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..66b78682ad 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]).collect{ |r| r.nil? || r.valid? }.all? + 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 |