diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-06-22 15:41:02 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-06-22 15:45:09 +0200 |
commit | 006a4d787e3fad4d142605d49ba4541992c6465b (patch) | |
tree | 9af15f5b1a37b183b001eb707323a3afed7df751 /activerecord/lib/active_record | |
parent | 792de3f1ed5358056f75821d39b73ceda272fb03 (diff) | |
download | rails-006a4d787e3fad4d142605d49ba4541992c6465b.tar.gz rails-006a4d787e3fad4d142605d49ba4541992c6465b.tar.bz2 rails-006a4d787e3fad4d142605d49ba4541992c6465b.zip |
refactor, don't duplicate presence validator logic.
This is a small refactoring that simplifies the Active Record specific
lenght validator.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/validations/presence.rb | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/validations/presence.rb b/activerecord/lib/active_record/validations/presence.rb index a9b791397b..23a3985d35 100644 --- a/activerecord/lib/active_record/validations/presence.rb +++ b/activerecord/lib/active_record/validations/presence.rb @@ -1,18 +1,12 @@ module ActiveRecord module Validations class PresenceValidator < ActiveModel::Validations::PresenceValidator # :nodoc: - def validate(record) + def validate_each(record, attribute, association_or_value) return unless should_validate?(record) - super - attributes.each do |attribute| - next unless record.class._reflect_on_association(attribute) - associated_records = Array.wrap(record.send(attribute)) - - # Superclass validates presence. Ensure present records aren't about to be destroyed. - if associated_records.present? && associated_records.all?(&:marked_for_destruction?) - record.errors.add(attribute, :blank, options) - end + if record.class._reflect_on_association(attribute) + association_or_value = Array.wrap(association_or_value).reject(&:marked_for_destruction?) end + super end end |