aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-05-10 12:28:38 +0300
committerJosé Valim <jose.valim@gmail.com>2010-05-10 12:28:38 +0300
commit5c245b91d2dbc0b300e8193310b3f950d0cf6c4b (patch)
treebb5c50da70417ff7951d905cab2a70f14e21ebf6 /activerecord/test
parentce5827ea4791e8b8143919ecceb0231e36e8932e (diff)
downloadrails-5c245b91d2dbc0b300e8193310b3f950d0cf6c4b.tar.gz
rails-5c245b91d2dbc0b300e8193310b3f950d0cf6c4b.tar.bz2
rails-5c245b91d2dbc0b300e8193310b3f950d0cf6c4b.zip
Make sure valid? preceives the context as in ActiveModel API (ht: Carlos Antonio)
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/validations_test.rb17
-rw-r--r--activerecord/test/models/reply.rb5
2 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index e1fb911cc9..b873babc36 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -62,6 +62,23 @@ class ValidationsTest < ActiveRecord::TestCase
assert_equal ["is Wrong Update"], r.errors[:title], "A reply with a bad content should contain an error"
end
+ def test_error_on_given_context
+ r = WrongReply.new
+ assert !r.valid?(:special_case)
+ assert "Invalid", r.errors[:title].join
+
+ r.title = "secret"
+ r.content = "Good"
+ assert r.valid?(:special_case)
+
+ r.title = nil
+ assert !r.save(:context => :special_case)
+ assert "Invalid", r.errors[:title].join
+
+ r.title = "secret"
+ assert r.save(:context => :special_case)
+ end
+
def test_invalid_record_exception
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! }
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.new.save! }
diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb
index 264a49b465..6cc9ee038a 100644
--- a/activerecord/test/models/reply.rb
+++ b/activerecord/test/models/reply.rb
@@ -17,6 +17,7 @@ class WrongReply < Reply
validate :check_empty_title
validate :check_content_mismatch, :on => :create
validate :check_wrong_update, :on => :update
+ validate :check_title_is_secret, :on => :special_case
def check_empty_title
errors[:title] << "Empty" unless attribute_present?("title")
@@ -39,6 +40,10 @@ class WrongReply < Reply
def check_wrong_update
errors[:title] << "is Wrong Update" if attribute_present?("title") && title == "Wrong Update"
end
+
+ def check_title_is_secret
+ errors[:title] << "Invalid" unless title == "secret"
+ end
end
class SillyReply < Reply