specific length validator for ActiveRecord to respect `mark_for_destruction`. #9917

wants to merge 2 commits into

4 participants

Ruby on Rails member

I implemented a separate validator to ensure that records, which are marked for destruction do not count when validating the length.

I also noticed that AR already had specific length tests, which I moved into a separate test-case.

Closes #7247.

Ruby on Rails member

@rafaelfranca can you take a look?

@senny senny commented on the diff Mar 25, 2013
@@ -0,0 +1,23 @@
+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 { |v|
+ v.marked_for_destruction?
+ }
+ end
+ super
+ end
+ end
+ module ClassMethods
+ # See <tt>ActiveModel::Validation::LengthValidator</tt> for more information.
senny Mar 25, 2013 Ruby on Rails member

I was not sure how much documentation we need here. I did not want to duplicate everything from AM.

@rafaelfranca rafaelfranca was assigned Apr 30, 2013

I would like to see another assert after this line, such as assert_equal 0, owner.pets.size The fact that owner.pets.size is 0 should be the reason that owner.valid? is not true.

@robin850 robin850 added this to the 4.0.4 milestone Mar 8, 2014
@rafaelfranca rafaelfranca modified the milestone: 4.0.5, 4.0.4, 4.0.6 Mar 10, 2014
@rafaelfranca rafaelfranca modified the milestone: 4.0.10, 4.1.7 Aug 21, 2014
@rafaelfranca rafaelfranca modified the milestone: 4.1.9, 4.0.13 Nov 19, 2014
@rafaelfranca rafaelfranca removed this from the 4.0.13 milestone Dec 29, 2014
@rafaelfranca rafaelfranca removed their assignment Dec 29, 2014
Ruby on Rails member

@senny seems good to me :shipit:

Ruby on Rails member

Committed with 2b12288

@senny senny closed this Dec 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment