From cfd421daa2b04216e27d666361eb4053020e027d Mon Sep 17 00:00:00 2001 From: James Hill Date: Wed, 5 Aug 2009 11:44:44 -0500 Subject: Allow validations to use values from custom readers [#2936 state:resolved] Signed-off-by: Joshua Peek --- .../test/cases/validations/presence_validation_test.rb | 14 ++++++++++++++ activemodel/test/cases/validations_test.rb | 14 ++++++++++++++ activemodel/test/models/custom_reader.rb | 17 +++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 activemodel/test/models/custom_reader.rb (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/presence_validation_test.rb b/activemodel/test/cases/validations/presence_validation_test.rb index aa5bdf1e62..bb6fb91774 100644 --- a/activemodel/test/cases/validations/presence_validation_test.rb +++ b/activemodel/test/cases/validations/presence_validation_test.rb @@ -54,4 +54,18 @@ class PresenceValidationTest < ActiveModel::TestCase assert p.valid? end end + + def test_validates_presence_of_for_ruby_class_with_custom_reader + repair_validations(Person) do + CustomReader.validates_presence_of :karma + + p = CustomReader.new + assert p.invalid? + + assert_equal ["can't be blank"], p.errors[:karma] + + p[:karma] = "Cold" + assert p.valid? + end + end end diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 8c89494247..0b340e68bf 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -5,6 +5,7 @@ require 'cases/tests_database' require 'models/topic' require 'models/reply' require 'models/developer' +require 'models/custom_reader' class ValidationsTest < ActiveModel::TestCase include ActiveModel::TestsDatabase @@ -97,6 +98,19 @@ class ValidationsTest < ActiveModel::TestCase assert_equal %w(gotcha gotcha), t.errors[:title] assert_equal %w(gotcha gotcha), t.errors[:content] end + + def test_validates_each_custom_reader + hits = 0 + CustomReader.validates_each(:title, :content, [:title, :content]) do |record, attr| + record.errors.add attr, 'gotcha' + hits += 1 + end + t = CustomReader.new("title" => "valid", "content" => "whatever") + assert !t.valid? + assert_equal 4, hits + assert_equal %w(gotcha gotcha), t.errors[:title] + assert_equal %w(gotcha gotcha), t.errors[:content] + end def test_validate_block Topic.validate { |topic| topic.errors.add("title", "will never be valid") } diff --git a/activemodel/test/models/custom_reader.rb b/activemodel/test/models/custom_reader.rb new file mode 100644 index 0000000000..7ac70e6167 --- /dev/null +++ b/activemodel/test/models/custom_reader.rb @@ -0,0 +1,17 @@ +class CustomReader + include ActiveModel::Validations + + def initialize(data = {}) + @data = data + end + + def []=(key, value) + @data[key] = value + end + + private + + def read_attribute_for_validation(key) + @data[key] + end +end \ No newline at end of file -- cgit v1.2.3 From 5ab94b2595836fe2de36fd632ba9577c459b1292 Mon Sep 17 00:00:00 2001 From: jzw Date: Wed, 5 Aug 2009 20:17:59 -0500 Subject: validates_length_of with maximum should allow nil [#2309 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activemodel/test/cases/validations/length_validation_test.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 4a2f72feab..bc24900ecf 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -52,6 +52,13 @@ class LengthValidationTest < ActiveModel::TestCase assert_equal ["is too short (minimum is 5 characters)"], t.errors["title"] end + def test_validates_length_of_using_maximum_should_allow_nil + Topic.validates_length_of :title, :maximum => 10 + t = Topic.create + puts t.errors + assert t.valid? + end + def test_optionally_validates_length_of_using_minimum Topic.validates_length_of :title, :minimum => 5, :allow_nil => true @@ -75,9 +82,6 @@ class LengthValidationTest < ActiveModel::TestCase t.title = "" assert t.valid? - - t.title = nil - assert !t.valid? end def test_optionally_validates_length_of_using_maximum -- cgit v1.2.3 From c6fe49b00921cda55af2dc311dd432795c4313f5 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 8 Aug 2009 19:08:39 +0100 Subject: Simplyfy validates_length_of and remove puts --- activemodel/test/cases/validations/length_validation_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index bc24900ecf..499f6a5e31 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -55,7 +55,6 @@ class LengthValidationTest < ActiveModel::TestCase def test_validates_length_of_using_maximum_should_allow_nil Topic.validates_length_of :title, :maximum => 10 t = Topic.create - puts t.errors assert t.valid? end -- cgit v1.2.3 From 5632b36701ad9514d596c558877cd74c14c1d54b Mon Sep 17 00:00:00 2001 From: Adam Keys Date: Sat, 8 Aug 2009 16:35:03 -0500 Subject: Fix exclusive range patch to use begin/end instead of min/max. [#2981 status:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim Signed-off-by: Pratik Naik --- .../test/cases/validations/length_validation_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 499f6a5e31..2c97b762f1 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -112,6 +112,20 @@ class LengthValidationTest < ActiveModel::TestCase assert t.valid? end + def test_validates_length_of_using_within_with_exclusive_range + Topic.validates_length_of(:title, :within => 4...10) + + t = Topic.new("title" => "9 chars!!") + assert t.valid? + + t.title = "Now I'm 10" + assert !t.valid? + assert_equal ["is too long (maximum is 9 characters)"], t.errors[:title] + + t.title = "Four" + assert t.valid? + end + def test_optionally_validates_length_of_using_within Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true -- cgit v1.2.3