Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971632 to your computer and use it in GitHub Desktop.
Save AquaGeek/971632 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #2646
From 49ab68f8f14511c05a5c583341b6a1ce864a32e1 Mon Sep 17 00:00:00 2001
From: Jarl Friis <jarl@hermes.(none)>
Date: Thu, 14 May 2009 11:55:51 +0200
Subject: [PATCH] Test that demonstrates lack of validation
---
activerecord/test/cases/validations_test.rb | 14 ++++++++++++++
1 files changed, 14 insertions(+), 0 deletions(-)
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index c20f5ae..f68a972 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -916,6 +916,20 @@ class ValidationsTest < ActiveRecord::TestCase
end
end
+ def test_validates_size_of_association_using_nested_attributes
+ repair_validations(Pirate) do
+ assert_nothing_raised { Pirate.validates_size_of :birds, :minimum => 1 }
+ p = Pirate.new('catchphrase' => 'hey!!')
+ assert !p.save
+ assert p.errors.on(:birds)
+ bird = p.birds.build('name' => 'birdname')
+ assert p.valid?
+ p.attributes = {:birds_attributes => [ {:_delete => 1, :id => bird.id}]}
+ assert !p.valid?
+ assert p.errors.on(:birds)
+ end
+ end
+
def test_validates_size_of_association_using_within
repair_validations(Owner) do
assert_nothing_raised { Owner.validates_size_of :pets, :within => 1..2 }
--
1.6.0.4
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index c20f5ae..6c3f41c 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -916,6 +916,31 @@ class ValidationsTest < ActiveRecord::TestCase
end
end
+ def test_validates_size_of_association_using_nested_attributes
+ require 'models/pirate'
+ require 'models/bird'
+ repair_validations(Pirate) do
+ # A validation that does not work:
+ # assert_nothing_raised { Pirate.validates_size_of :birds, :minimum => 1 }
+ # A validation that works:
+ # assert_nothing_raised do
+ # Pirate.validates_each(:birds) do |record, attr, value|
+ # record.errors.add attr, 'is too short.' if value.reject { |v| v.marked_for_destruction? }.size < 1
+ # end
+ # end
+ p = Pirate.new('catchphrase' => 'hey!!')
+ assert !p.save
+ assert p.errors.on(:birds)
+ bird = p.birds.build('name' => 'birdname')
+ assert p.valid?
+ assert p.save
+ bird = p.birds.first
+ p.attributes = {:birds_attributes => { '1' => {:_delete => 1, :id => bird.id} } }
+ assert !p.valid?
+ assert p.errors.on(:birds)
+ end
+ end
+
def test_validates_size_of_association_using_within
repair_validations(Owner) do
assert_nothing_raised { Owner.validates_size_of :pets, :within => 1..2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment