From 1579f3b45db4b5717984174689c41b1e7f679d1b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 16 Dec 2004 01:32:35 +0000 Subject: Added Base.validates_format_of that Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression provided. [Marcel] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@174 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/fixtures/company_in_module.rb | 2 +- .../test/fixtures/db_definitions/postgresql.sql | 2 +- activerecord/test/fixtures/developer.rb | 4 ++-- activerecord/test/validations_test.rb | 21 +++++++++++++++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/fixtures/company_in_module.rb b/activerecord/test/fixtures/company_in_module.rb index a484ed5eaf..52b5d37b8c 100644 --- a/activerecord/test/fixtures/company_in_module.rb +++ b/activerecord/test/fixtures/company_in_module.rb @@ -24,7 +24,7 @@ module MyApplication protected def validate - errors.add_on_boundry_breaking("name", 3..20) + errors.add_on_boundary_breaking("name", 3..20) end end diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 9bf1035ea3..e79a8cf023 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -123,5 +123,5 @@ CREATE TABLE mixins ( root_id integer, created_at timestamp, updated_at timestamp, - PRIMARY KEY (`id`) + PRIMARY KEY (id) ); diff --git a/activerecord/test/fixtures/developer.rb b/activerecord/test/fixtures/developer.rb index 737fc3824b..1c4cc30506 100644 --- a/activerecord/test/fixtures/developer.rb +++ b/activerecord/test/fixtures/developer.rb @@ -3,6 +3,6 @@ class Developer < ActiveRecord::Base protected def validate - errors.add_on_boundry_breaking("name", 3..20) + errors.add_on_boundary_breaking("name", 3..20) end -end \ No newline at end of file +end diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index af7c975de1..d4c77d9953 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -191,7 +191,7 @@ class ValidationsTest < Test::Unit::TestCase end def test_validate_boundaries - Topic.validates_boundries_of(:title, :content, :within => 3..5) + Topic.validates_boundaries_of(:title, :content, :within => 3..5) t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long") assert !t.save @@ -203,4 +203,21 @@ class ValidationsTest < Test::Unit::TestCase assert t.save end -end \ No newline at end of file + + def test_validate_format + Topic.validates_format_of(:title, :content, :with => /^Validation macros rule!$/, :message => "is bad data") + + t = Topic.create("title" => "i'm incorrect", "content" => "Validation macros rule!") + assert !t.valid?, "Shouldn't be valid" + assert !t.save, "Shouldn't save because it's invalid" + assert_equal "is bad data", t.errors.on(:title) + assert_nil t.errors.on(:content) + + t.title = "Validation macros rule!" + + assert t.save + assert_nil t.errors.on(:title) + + assert_raise(ArgumentError) { Topic.validates_format_of(:title, :content) } + end +end -- cgit v1.2.3