From 640d9e7e32d9ad67cf81a686aad80266fee7fa61 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 4 Jan 2010 16:29:07 -0600 Subject: Autoload AMo test case --- activemodel/test/cases/helper.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/helper.rb b/activemodel/test/cases/helper.rb index 30193956ea..917bb720d0 100644 --- a/activemodel/test/cases/helper.rb +++ b/activemodel/test/cases/helper.rb @@ -8,7 +8,6 @@ $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'config' require 'active_model' -require 'active_model/test_case' # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true -- cgit v1.2.3 From e5ed62deea3f281f9dafc8e7c9ae4354b5ad6a27 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 4 Jan 2010 16:50:01 -0600 Subject: Autoload AR test case --- activemodel/test/cases/tests_database.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/tests_database.rb b/activemodel/test/cases/tests_database.rb index 79668dd941..8ca54d2678 100644 --- a/activemodel/test/cases/tests_database.rb +++ b/activemodel/test/cases/tests_database.rb @@ -2,8 +2,6 @@ require 'logger' $:.unshift(File.dirname(__FILE__) + '/../../../activerecord/lib') require 'active_record' -require 'active_record/test_case' -require 'active_record/fixtures' module ActiveModel module TestsDatabase -- cgit v1.2.3 From 190ce3ab37966957997cd18772d1260bf121c2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 7 Jan 2010 01:04:25 +0100 Subject: Errors messages are now moved from :activerecord.errors to simply :errors on I18n yml files. --- .../i18n_generate_message_validation_test.rb | 36 --------- .../test/cases/validations/i18n_validation_test.rb | 88 +++++++++++----------- 2 files changed, 44 insertions(+), 80 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb index 54b2405c92..6116ef71d4 100644 --- a/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_generate_message_validation_test.rb @@ -7,42 +7,6 @@ class I18nGenerateMessageValidationTest < ActiveModel::TestCase def setup Person.reset_callbacks(:validate) @person = Person.new - - @old_load_path, @old_backend = I18n.load_path, I18n.backend - I18n.load_path.clear - I18n.backend = I18n::Backend::Simple.new - - I18n.backend.store_translations :'en', { - :activemodel => { - :errors => { - :messages => { - :inclusion => "is not included in the list", - :exclusion => "is reserved", - :invalid => "is invalid", - :confirmation => "doesn't match confirmation", - :accepted => "must be accepted", - :empty => "can't be empty", - :blank => "can't be blank", - :too_long => "is too long (maximum is {{count}} characters)", - :too_short => "is too short (minimum is {{count}} characters)", - :wrong_length => "is the wrong length (should be {{count}} characters)", - :not_a_number => "is not a number", - :greater_than => "must be greater than {{count}}", - :greater_than_or_equal_to => "must be greater than or equal to {{count}}", - :equal_to => "must be equal to {{count}}", - :less_than => "must be less than {{count}}", - :less_than_or_equal_to => "must be less than or equal to {{count}}", - :odd => "must be odd", - :even => "must be even" - } - } - } - } - end - - def teardown - I18n.load_path.replace @old_load_path - I18n.backend = @old_backend end # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value) diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index a7656fe219..85dabc02e7 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -12,7 +12,7 @@ class I18nValidationTest < ActiveModel::TestCase @old_load_path, @old_backend = I18n.load_path, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new - I18n.backend.store_translations('en', :activemodel => {:errors => {:messages => {:custom => nil}}}) + I18n.backend.store_translations('en', :errors => {:messages => {:custom => nil}}) end def teardown @@ -42,13 +42,13 @@ class I18nValidationTest < ActiveModel::TestCase end def test_errors_full_messages_translates_human_attribute_name_for_model_attributes - @person.errors.add('name', 'empty') - I18n.expects(:translate).with(:"person.name", :default => ['Name', 'Name'], :scope => [:activemodel, :attributes], :count => 1).returns('Name') - @person.errors.full_messages + @person.errors.add(:name, 'not found') + Person.expects(:human_attribute_name).with(:name, :default => 'Name').returns("Person's name") + assert_equal ["Person's name not found"], @person.errors.full_messages end def test_errors_full_messages_uses_format - I18n.backend.store_translations('en', :activemodel => {:errors => {:format => "Field {{attribute}} {{message}}"}}) + I18n.backend.store_translations('en', :errors => {:format => "Field {{attribute}} {{message}}"}) @person.errors.add('name', 'empty') assert_equal ["Field Name empty"], @person.errors.full_messages end @@ -254,8 +254,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:confirmation => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}} Person.validates_confirmation_of :title @person.title_confirmation = 'foo' @@ -264,7 +264,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_confirmation_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:confirmation => 'global message'}} Person.validates_confirmation_of :title @person.title_confirmation = 'foo' @@ -275,8 +275,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:accepted => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}} Person.validates_acceptance_of :title, :allow_nil => false @person.valid? @@ -284,7 +284,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_acceptance_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:accepted => 'global message'}} Person.validates_acceptance_of :title, :allow_nil => false @person.valid? @@ -294,8 +294,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:blank => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}} Person.validates_presence_of :title @person.valid? @@ -303,7 +303,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_presence_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:blank => 'global message'}} Person.validates_presence_of :title @person.valid? @@ -313,8 +313,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:too_short => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}} Person.validates_length_of :title, :within => 3..5 @person.valid? @@ -322,7 +322,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:too_short => 'global message'}} Person.validates_length_of :title, :within => 3..5 @person.valid? @@ -332,8 +332,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_length_of :is w/o mocha def test_validates_length_of_is_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:wrong_length => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}} Person.validates_length_of :title, :is => 5 @person.valid? @@ -341,7 +341,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_length_of_is_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:wrong_length => 'global message'}} Person.validates_length_of :title, :is => 5 @person.valid? @@ -351,8 +351,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:invalid => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} Person.validates_format_of :title, :with => /^[1-9][0-9]*$/ @person.valid? @@ -360,7 +360,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_format_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:invalid => 'global message'}} Person.validates_format_of :title, :with => /^[1-9][0-9]*$/ @person.valid? @@ -370,8 +370,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:inclusion => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}} Person.validates_inclusion_of :title, :in => %w(a b c) @person.valid? @@ -379,7 +379,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_inclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:inclusion => 'global message'}} Person.validates_inclusion_of :title, :in => %w(a b c) @person.valid? @@ -389,8 +389,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:exclusion => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}} Person.validates_exclusion_of :title, :in => %w(a b c) @person.title = 'a' @@ -399,7 +399,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_exclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:exclusion => 'global message'}} Person.validates_exclusion_of :title, :in => %w(a b c) @person.title = 'a' @@ -410,8 +410,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title @person.title = 'a' @@ -420,7 +420,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @person.title = 'a' @@ -431,8 +431,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:not_a_number => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @person.title = 'a' @@ -441,7 +441,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_only_integer_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:not_a_number => 'global message'}} Person.validates_numericality_of :title, :only_integer => true @person.title = 'a' @@ -452,8 +452,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:odd => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :odd => true @person.title = 0 @@ -462,7 +462,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_odd_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:odd => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :odd => true @person.title = 0 @@ -473,8 +473,8 @@ class I18nValidationTest < ActiveModel::TestCase # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}}} - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:less_than => 'custom message'}}}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :less_than => 0 @person.title = 1 @@ -483,7 +483,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_numericality_of_less_than_finds_global_default_translation - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:less_than => 'global message'}} Person.validates_numericality_of :title, :only_integer => true, :less_than => 0 @person.title = 1 @@ -494,7 +494,7 @@ class I18nValidationTest < ActiveModel::TestCase # test with validates_with def test_validations_with_message_symbol_must_translate - I18n.backend.store_translations 'en', :activemodel => {:errors => {:messages => {:custom_error => "I am a custom error"}}} + I18n.backend.store_translations 'en', :errors => {:messages => {:custom_error => "I am a custom error"}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? @@ -502,7 +502,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_with_message_symbol_must_translate_per_attribute - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? @@ -510,7 +510,7 @@ class I18nValidationTest < ActiveModel::TestCase end def test_validates_with_message_symbol_must_translate_per_model - I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:custom_error => "I am a custom error"}}}} + I18n.backend.store_translations 'en', :errors => {:models => {:person => {:custom_error => "I am a custom error"}}} Person.validates_presence_of :title, :message => :custom_error @person.title = nil @person.valid? -- cgit v1.2.3 From b0572ab2b670f00c96226d8ba29f4f153771e086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 7 Jan 2010 02:37:42 +0100 Subject: Be sure to dup load paths before clearing the array. --- activemodel/test/cases/validations/i18n_validation_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/i18n_validation_test.rb b/activemodel/test/cases/validations/i18n_validation_test.rb index 85dabc02e7..7d33fcea98 100644 --- a/activemodel/test/cases/validations/i18n_validation_test.rb +++ b/activemodel/test/cases/validations/i18n_validation_test.rb @@ -9,7 +9,7 @@ class I18nValidationTest < ActiveModel::TestCase Person.reset_callbacks(:validate) @person = Person.new - @old_load_path, @old_backend = I18n.load_path, I18n.backend + @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new I18n.backend.store_translations('en', :errors => {:messages => {:custom => nil}}) -- cgit v1.2.3 From a25ef06956725f5c4c8833f86f61f1f12c4323e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 7 Jan 2010 02:45:50 +0100 Subject: Allow to specify default attributes names translation in I18n yml files. For example, you could easily specify :created_at and :updated_at translations as: en: attributes: created_at: "Created at" updated_at: "Updated at" This configuration is built on ActiveModel, so it means those translations are shared between different ORMs as well (but always as a fallback). --- activemodel/test/cases/translation_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index bfc1ca12e6..e25d308ca1 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -11,6 +11,11 @@ class ActiveModelI18nTests < ActiveModel::TestCase I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } assert_equal 'person name attribute', Person.human_attribute_name('name') end + + def test_translated_model_attributes_with_default + I18n.backend.store_translations 'en', :attributes => { :name => 'name default attribute' } + assert_equal 'name default attribute', Person.human_attribute_name('name') + end def test_translated_model_attributes_with_symbols I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } -- cgit v1.2.3 From 0a79eb7889e7ac711ff171a453d65f3df57b9237 Mon Sep 17 00:00:00 2001 From: jamie Date: Thu, 7 Jan 2010 18:44:35 +0100 Subject: Add validates method as shortcut to setup validators for a given set of attributes: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit class Person < ActiveRecord::Base include MyValidators validates :name, :presence => true, :uniqueness => true, :length => { :maximum => 100 } validates :email, :presence => true, :email => true end [#3058 status:resolved] Signed-off-by: José Valim --- .../test/cases/validations/validates_test.rb | 53 ++++++++++++++++++++++ .../test/cases/validations/with_validation_test.rb | 22 +++++++++ activemodel/test/models/custom_reader.rb | 2 - activemodel/test/models/person_with_validator.rb | 11 +++++ activemodel/test/validators/email_validator.rb | 6 +++ 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 activemodel/test/cases/validations/validates_test.rb create mode 100644 activemodel/test/models/person_with_validator.rb create mode 100644 activemodel/test/validators/email_validator.rb (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb new file mode 100644 index 0000000000..c0bf03efc7 --- /dev/null +++ b/activemodel/test/cases/validations/validates_test.rb @@ -0,0 +1,53 @@ +# encoding: utf-8 +require 'cases/helper' +require 'models/person' +require 'models/person_with_validator' +require 'validators/email_validator' + +class ValidatesTest < ActiveRecord::TestCase + def test_validates_with_built_in_validation + Person.validates :title, :numericality => true + person = Person.new + person.valid? + assert person.errors[:title].include?('is not a number') + end + + def test_validates_with_built_in_validation_and_options + Person.validates :title, :numericality => { :message => 'my custom message' } + person = Person.new + person.valid? + assert person.errors[:title].include?('my custom message') + end + + def test_validates_with_validator_class + Person.validates :karma, :email => true + person = Person.new + person.valid? + assert person.errors[:karma].include?('is not an email') + end + + def test_validates_with_validator_class_and_options + Person.validates :karma, :email => { :message => 'my custom message' } + person = Person.new + person.valid? + assert person.errors[:karma].include?('my custom message') + end + + def test_validates_with_unknown_validator + assert_raise(ArgumentError) { Person.validates :karma, :unknown => true } + end + + def test_validates_with_included_validator + PersonWithValidator.validates :title, :presence => true + person = PersonWithValidator.new + person.valid? + assert person.errors[:title].include?('Local validator') + end + + def test_validates_with_included_validator_and_options + PersonWithValidator.validates :title, :presence => { :custom => ' please' } + person = PersonWithValidator.new + person.valid? + assert person.errors[:title].include?('Local validator please') + end +end \ No newline at end of file diff --git a/activemodel/test/cases/validations/with_validation_test.rb b/activemodel/test/cases/validations/with_validation_test.rb index 7540ccb580..66b072ea38 100644 --- a/activemodel/test/cases/validations/with_validation_test.rb +++ b/activemodel/test/cases/validations/with_validation_test.rb @@ -120,6 +120,28 @@ class ValidatesWithTest < ActiveRecord::TestCase Topic.validates_with(validator, :if => "1 == 1", :foo => :bar) assert topic.valid? end + + test "calls setup method of validator passing in self when validator has setup method" do + topic = Topic.new + validator = stub_everything + validator.stubs(:new).returns(validator) + validator.stubs(:validate) + validator.stubs(:respond_to?).with(:setup).returns(true) + validator.expects(:setup).with(Topic).once + Topic.validates_with(validator) + assert topic.valid? + end + + test "doesn't call setup method of validator when validator has no setup method" do + topic = Topic.new + validator = stub_everything + validator.stubs(:new).returns(validator) + validator.stubs(:validate) + validator.stubs(:respond_to?).with(:setup).returns(false) + validator.expects(:setup).with(Topic).never + Topic.validates_with(validator) + assert topic.valid? + end test "validates_with with options" do Topic.validates_with(ValidatorThatValidatesOptions, :field => :first_name) diff --git a/activemodel/test/models/custom_reader.rb b/activemodel/test/models/custom_reader.rb index 7ac70e6167..14a8be9ebc 100644 --- a/activemodel/test/models/custom_reader.rb +++ b/activemodel/test/models/custom_reader.rb @@ -8,8 +8,6 @@ class CustomReader def []=(key, value) @data[key] = value end - - private def read_attribute_for_validation(key) @data[key] diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb new file mode 100644 index 0000000000..f9763ea853 --- /dev/null +++ b/activemodel/test/models/person_with_validator.rb @@ -0,0 +1,11 @@ +class PersonWithValidator + include ActiveModel::Validations + + class PresenceValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors[attribute] << "Local validator#{options[:custom]}" if value.blank? + end + end + + attr_accessor :title, :karma +end diff --git a/activemodel/test/validators/email_validator.rb b/activemodel/test/validators/email_validator.rb new file mode 100644 index 0000000000..cff47ac230 --- /dev/null +++ b/activemodel/test/validators/email_validator.rb @@ -0,0 +1,6 @@ +class EmailValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors[attribute] << (options[:message] || "is not an email") unless + value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i + end +end \ No newline at end of file -- cgit v1.2.3 From 47a5fd4c4ba447c997c0a029adfa80d8b790b25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 7 Jan 2010 19:22:32 +0100 Subject: Allow :if, :unless, :on, :allow_nil and :allow_blank as shared options in validates. --- .../test/cases/validations/validates_test.rb | 41 ++++++++++++++++++++-- activemodel/test/models/person.rb | 4 +++ 2 files changed, 42 insertions(+), 3 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index c0bf03efc7..181ff38b64 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -4,14 +4,21 @@ require 'models/person' require 'models/person_with_validator' require 'validators/email_validator' -class ValidatesTest < ActiveRecord::TestCase +class ValidatesTest < ActiveModel::TestCase + setup :reset_callbacks + teardown :reset_callbacks + + def reset_callbacks + Person.reset_callbacks(:validate) + end + def test_validates_with_built_in_validation Person.validates :title, :numericality => true person = Person.new person.valid? assert person.errors[:title].include?('is not a number') end - + def test_validates_with_built_in_validation_and_options Person.validates :title, :numericality => { :message => 'my custom message' } person = Person.new @@ -25,7 +32,35 @@ class ValidatesTest < ActiveRecord::TestCase person.valid? assert person.errors[:karma].include?('is not an email') end - + + def test_validates_with_if_as_local_conditions + Person.validates :karma, :presence => true, :email => { :unless => :condition_is_true } + person = Person.new + person.valid? + assert !person.errors[:karma].include?('is not an email') + assert person.errors[:karma].include?('can\'t be blank') + end + + def test_validates_with_if_as_shared_conditions + Person.validates :karma, :presence => true, :email => true, :if => :condition_is_true + person = Person.new + person.valid? + assert person.errors[:karma].include?('is not an email') + assert person.errors[:karma].include?('can\'t be blank') + end + + def test_validates_with_unless_shared_conditions + Person.validates :karma, :presence => true, :email => true, :unless => :condition_is_true + person = Person.new + assert person.valid? + end + + def test_validates_with_allow_nil_shared_conditions + Person.validates :karma, :length => { :minimum => 20 }, :email => true, :allow_nil => true + person = Person.new + assert person.valid? + end + def test_validates_with_validator_class_and_options Person.validates :karma, :email => { :message => 'my custom message' } person = Person.new diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index c83d768379..53ac68055f 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -3,6 +3,10 @@ class Person extend ActiveModel::Translation attr_accessor :title, :karma, :salary + + def condition_is_true + true + end end class Child < Person -- cgit v1.2.3 From fa14d6d51ed89acde66b49e8d3a6423396c3d553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 8 Jan 2010 08:34:44 +0100 Subject: Compile length validator options still at the class level, so whenever the validator is called, it just needs to check for :maximum, :minimum and :is values. --- activemodel/test/cases/validations/length_validation_test.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 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 f3ef5e648a..99d0268b67 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -221,10 +221,8 @@ class LengthValidationTest < ActiveModel::TestCase end def test_validates_length_of_nasty_params - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :minimum=>9) } - assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :is=>9) } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>-6) } + assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6) } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>"a") } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") } assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a") } -- cgit v1.2.3 From 7045c4c279499eb7340fb420398d613497739eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 8 Jan 2010 08:37:58 +0100 Subject: Allow validates to map some types to specific options. So now you can do: validates :email, :presence => true, :format => /@/ validates :genre, :inclusion => %w(m f) validates :password, :length => 6..20 --- .../test/cases/validations/validates_test.rb | 48 +++++++++++++++++----- activemodel/test/models/person.rb | 2 +- 2 files changed, 38 insertions(+), 12 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 181ff38b64..966690d75f 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -10,43 +10,42 @@ class ValidatesTest < ActiveModel::TestCase def reset_callbacks Person.reset_callbacks(:validate) + PersonWithValidator.reset_callbacks(:validate) end def test_validates_with_built_in_validation Person.validates :title, :numericality => true person = Person.new person.valid? - assert person.errors[:title].include?('is not a number') + assert_equal ['is not a number'], person.errors[:title] end def test_validates_with_built_in_validation_and_options - Person.validates :title, :numericality => { :message => 'my custom message' } + Person.validates :salary, :numericality => { :message => 'my custom message' } person = Person.new person.valid? - assert person.errors[:title].include?('my custom message') + assert_equal ['my custom message'], person.errors[:salary] end def test_validates_with_validator_class Person.validates :karma, :email => true person = Person.new person.valid? - assert person.errors[:karma].include?('is not an email') + assert_equal ['is not an email'], person.errors[:karma] end def test_validates_with_if_as_local_conditions Person.validates :karma, :presence => true, :email => { :unless => :condition_is_true } person = Person.new person.valid? - assert !person.errors[:karma].include?('is not an email') - assert person.errors[:karma].include?('can\'t be blank') + assert_equal ["can't be blank"], person.errors[:karma] end def test_validates_with_if_as_shared_conditions Person.validates :karma, :presence => true, :email => true, :if => :condition_is_true person = Person.new person.valid? - assert person.errors[:karma].include?('is not an email') - assert person.errors[:karma].include?('can\'t be blank') + assert ["can't be blank", "is not an email"], person.errors[:karma].sort end def test_validates_with_unless_shared_conditions @@ -61,11 +60,38 @@ class ValidatesTest < ActiveModel::TestCase assert person.valid? end + def test_validates_with_regexp + Person.validates :karma, :format => /positive|negative/ + person = Person.new + assert person.invalid? + assert_equal ['is invalid'], person.errors[:karma] + person.karma = "positive" + assert person.valid? + end + + def test_validates_with_array + Person.validates :genre, :inclusion => %w(m f) + person = Person.new + assert person.invalid? + assert_equal ['is not included in the list'], person.errors[:genre] + person.genre = "m" + assert person.valid? + end + + def test_validates_with_range + Person.validates :password, :length => 6..20 + person = Person.new + assert person.invalid? + assert_equal ['is too short (minimum is 6 characters)'], person.errors[:password] + person.password = '123456' + assert person.valid? + end + def test_validates_with_validator_class_and_options Person.validates :karma, :email => { :message => 'my custom message' } person = Person.new person.valid? - assert person.errors[:karma].include?('my custom message') + assert_equal ['my custom message'], person.errors[:karma] end def test_validates_with_unknown_validator @@ -76,13 +102,13 @@ class ValidatesTest < ActiveModel::TestCase PersonWithValidator.validates :title, :presence => true person = PersonWithValidator.new person.valid? - assert person.errors[:title].include?('Local validator') + assert_equal ['Local validator'], person.errors[:title] end def test_validates_with_included_validator_and_options PersonWithValidator.validates :title, :presence => { :custom => ' please' } person = PersonWithValidator.new person.valid? - assert person.errors[:title].include?('Local validator please') + assert_equal ['Local validator please'], person.errors[:title] end end \ No newline at end of file diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index 53ac68055f..86e8d73bbb 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -2,7 +2,7 @@ class Person include ActiveModel::Validations extend ActiveModel::Translation - attr_accessor :title, :karma, :salary + attr_accessor :title, :karma, :salary, :genre, :password def condition_is_true true -- cgit v1.2.3 From 017f5d5308098438da0b8c44163af4ecb422f1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 9 Jan 2010 00:17:16 +0100 Subject: Fix typo by renaming :genre to :gender. --- activemodel/test/cases/validations/validates_test.rb | 12 ++++++------ activemodel/test/models/person.rb | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index 966690d75f..d15fb4a524 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -70,20 +70,20 @@ class ValidatesTest < ActiveModel::TestCase end def test_validates_with_array - Person.validates :genre, :inclusion => %w(m f) + Person.validates :gender, :inclusion => %w(m f) person = Person.new assert person.invalid? - assert_equal ['is not included in the list'], person.errors[:genre] - person.genre = "m" + assert_equal ['is not included in the list'], person.errors[:gender] + person.gender = "m" assert person.valid? end def test_validates_with_range - Person.validates :password, :length => 6..20 + Person.validates :karma, :length => 6..20 person = Person.new assert person.invalid? - assert_equal ['is too short (minimum is 6 characters)'], person.errors[:password] - person.password = '123456' + assert_equal ['is too short (minimum is 6 characters)'], person.errors[:karma] + person.karma = 'something' assert person.valid? end diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb index 86e8d73bbb..cf16a38618 100644 --- a/activemodel/test/models/person.rb +++ b/activemodel/test/models/person.rb @@ -2,7 +2,7 @@ class Person include ActiveModel::Validations extend ActiveModel::Translation - attr_accessor :title, :karma, :salary, :genre, :password + attr_accessor :title, :karma, :salary, :gender def condition_is_true true -- cgit v1.2.3 From b078f7fd3910b0b174bc951cc8c0d27536b09c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 11 Jan 2010 23:37:48 +0100 Subject: Fix typos and add tests to ensure they will be caught the next time. --- .../test/cases/validations/numericality_validation_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 75cd654f98..38b3f87e93 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -154,6 +154,14 @@ class NumericalityValidationTest < ActiveModel::TestCase Person.reset_callbacks(:validate) end + def test_validates_numericality_with_invalid_args + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than_or_equal_to => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than_or_equal_to => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :greater_than => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :less_than => "foo" } + assert_raise(ArgumentError){ Topic.validates_numericality_of :approved, :equal_to => "foo" } + end + private def invalid!(values, error = nil) -- cgit v1.2.3