Validates_presence_of associated object marked for destruction #6827

Merged
merged 1 commit into from Jul 27, 2012

Conversation

Projects
None yet
4 participants

This allows us to mark the parent object as invalid if all associated objects
in a presence validated association are marked for destruction.

See: #6812

@rafaelfranca rafaelfranca and 1 other commented on an outdated diff Jun 22, 2012

activerecord/lib/active_record/validations/presence.rb
@@ -0,0 +1,22 @@
+module ActiveRecord
+ module Validations
+ class PresenceValidator < ActiveModel::Validations::PresenceValidator
+ def validate(record)
+ super(record)
@rafaelfranca

rafaelfranca Jun 22, 2012

Owner

super without arguments will pass the same arguments of the method. We don't need to pass it.

@zephyr-dev

zephyr-dev Jun 22, 2012

Good call, fixed :)

@carlosantoniodasilva carlosantoniodasilva and 1 other commented on an outdated diff Jun 23, 2012

activerecord/lib/active_record/validations/presence.rb
@@ -0,0 +1,22 @@
+module ActiveRecord
+ module Validations
+ class PresenceValidator < ActiveModel::Validations::PresenceValidator
+ def validate(record)
+ super
+ attributes.each do |attribute|
+ next unless record.class.reflect_on_association(attribute)
+ value = record.send(attribute)
+ if Array.wrap(value).all? { |r| r.marked_for_destruction? }
@carlosantoniodasilva

carlosantoniodasilva Jun 23, 2012

Owner

I think there's no need for Array.wrap, could just use Array() instead?

I'm ok with the idea, I just think there's a possibility of breaking current applications that may be relying on the current presence validation not ignoring records marked for destruction (even if it's wrong or not).

For that, I think we need a changelog entry, and some docs in this file explaining why this presence validator exists and what makes it different from the Active Model one. Guide updates are also welcome.

Thanks!

Thanks, @carlosantoniodasilva. We'll work on these and update the pull request.

Documentation added.

Owner

rafaelfranca commented Jun 26, 2012

This pull request cannot be automatically merged. Also, please squash your commits.

Squashed and rebased, thanks!

Hi all. Any update on this?

Owner

rafaelfranca commented Jul 3, 2012

@tenderlove @josevalim what do you think?

Brent Wheeldon & Nick Monje AR has a subclass of AM:PresenceValidator.
This allows us to mark the parent object as invalid if all associated objects
in a presence validated association are marked for destruction.

See: #6812
9feda92

Is there an update on this?

Member

josevalim commented Jul 27, 2012

This looks good to me.

@josevalim josevalim added a commit that referenced this pull request Jul 27, 2012

@josevalim josevalim Merge pull request #6827 from zephyr-dev/master
Validates_presence_of associated object marked for destruction
1fab518

@josevalim josevalim merged commit 1fab518 into rails:master Jul 27, 2012

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment