diff options
author | James Hill <jamie@jamie-laptop.local> | 2009-08-05 11:44:44 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-08-05 11:45:01 -0500 |
commit | cfd421daa2b04216e27d666361eb4053020e027d (patch) | |
tree | 878e4980a47433990e96c2edb8b28ee5b1ae22f1 /activemodel/test | |
parent | 64268a0b06d32567c6e88b7293f332b79e10414b (diff) | |
download | rails-cfd421daa2b04216e27d666361eb4053020e027d.tar.gz rails-cfd421daa2b04216e27d666361eb4053020e027d.tar.bz2 rails-cfd421daa2b04216e27d666361eb4053020e027d.zip |
Allow validations to use values from custom readers [#2936 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/validations/presence_validation_test.rb | 14 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 14 | ||||
-rw-r--r-- | activemodel/test/models/custom_reader.rb | 17 |
3 files changed, 45 insertions, 0 deletions
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 |