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 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 activemodel/test/cases/validations/validates_test.rb (limited to 'activemodel/test/cases/validations/validates_test.rb') 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 -- 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 ++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'activemodel/test/cases/validations/validates_test.rb') 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 -- 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 +++++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'activemodel/test/cases/validations/validates_test.rb') 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 -- 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 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activemodel/test/cases/validations/validates_test.rb') 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 -- cgit v1.2.3