diff options
author | Brent Wheeldon & Nick Monje <pair+brent+nmonje@pivotallabs.com> | 2012-06-22 12:09:48 -0400 |
---|---|---|
committer | Ben Moss <pair+bmoss@pivotallabs.com> | 2012-07-20 17:37:57 -0400 |
commit | 9feda929409ab687befaed8d1c9878d94e955adc (patch) | |
tree | 5deafb5e134315050c59e059a71d18ec6915e655 /activerecord/test/cases/validations | |
parent | 215d41d802637520129cb7551b35faca72873143 (diff) | |
download | rails-9feda929409ab687befaed8d1c9878d94e955adc.tar.gz rails-9feda929409ab687befaed8d1c9878d94e955adc.tar.bz2 rails-9feda929409ab687befaed8d1c9878d94e955adc.zip |
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: https://github.com/rails/rails/issues/6812
Diffstat (limited to 'activerecord/test/cases/validations')
-rw-r--r-- | activerecord/test/cases/validations/presence_validation_test.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/activerecord/test/cases/validations/presence_validation_test.rb b/activerecord/test/cases/validations/presence_validation_test.rb new file mode 100644 index 0000000000..cd9175f454 --- /dev/null +++ b/activerecord/test/cases/validations/presence_validation_test.rb @@ -0,0 +1,44 @@ +# encoding: utf-8 +require "cases/helper" +require 'models/man' +require 'models/face' +require 'models/interest' + +class PresenceValidationTest < ActiveRecord::TestCase + class Boy < Man; end + + repair_validations(Boy) + + def test_validates_presence_of_non_association + Boy.validates_presence_of(:name) + b = Boy.new + assert b.invalid? + + b.name = "Alex" + assert b.valid? + end + + def test_validates_presence_of_has_one_marked_for_destruction + Boy.validates_presence_of(:face) + b = Boy.new + f = Face.new + b.face = f + assert b.valid? + + f.mark_for_destruction + assert b.invalid? + end + + def test_validates_presence_of_has_many_marked_for_destruction + Boy.validates_presence_of(:interests) + b = Boy.new + b.interests << [i1 = Interest.new, i2 = Interest.new] + assert b.valid? + + i1.mark_for_destruction + assert b.valid? + + i2.mark_for_destruction + assert b.invalid? + end +end |