diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-03-25 15:58:23 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-12-30 10:25:58 +0100 |
commit | 2b12288139f9bad6cf6d058d93af66cdf598dda9 (patch) | |
tree | 439daa4ce91c71baf46906561196ff0323707292 /activerecord/lib | |
parent | ecb1981bfd3ffaca3a3efd110fc85380ece8191d (diff) | |
download | rails-2b12288139f9bad6cf6d058d93af66cdf598dda9.tar.gz rails-2b12288139f9bad6cf6d058d93af66cdf598dda9.tar.bz2 rails-2b12288139f9bad6cf6d058d93af66cdf598dda9.zip |
AR specific length validator to respect `marked_for_destruction`.
Closes #7247.
Conflicts:
activerecord/CHANGELOG.md
activerecord/test/models/owner.rb
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/length.rb | 21 |
2 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index a6c8ff7f3a..f27adc9c40 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -88,3 +88,4 @@ end require "active_record/validations/associated" require "active_record/validations/uniqueness" require "active_record/validations/presence" +require "active_record/validations/length" diff --git a/activerecord/lib/active_record/validations/length.rb b/activerecord/lib/active_record/validations/length.rb new file mode 100644 index 0000000000..ef5a6cbbe7 --- /dev/null +++ b/activerecord/lib/active_record/validations/length.rb @@ -0,0 +1,21 @@ +module ActiveRecord + module Validations + class LengthValidator < ActiveModel::Validations::LengthValidator # :nodoc: + def validate_each(record, attribute, association_or_value) + if association_or_value.respond_to?(:loaded?) && association_or_value.loaded? + association_or_value = association_or_value.target.reject(&:marked_for_destruction?) + end + super + end + end + + module ClassMethods + # See <tt>ActiveModel::Validation::LengthValidator</tt> for more information. + def validates_length_of(*attr_names) + validates_with LengthValidator, _merge_attributes(attr_names) + end + + alias_method :validates_size_of, :validates_length_of + end + end +end |