From bf5cf5db86fa0aeb818d32541a35310de992f426 Mon Sep 17 00:00:00 2001
From: Aditya Sanghi <asanghi@me.com>
Date: Fri, 29 Apr 2011 02:54:37 +0530
Subject: :if should not fire on validations when not in context with :on

---
 activemodel/test/cases/validations_test.rb | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

(limited to 'activemodel/test')

diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index 2f36195627..0b50acf913 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -212,6 +212,20 @@ class ValidationsTest < ActiveModel::TestCase
     assert_equal 'is too short (minimum is 2 characters)', t.errors[key][0]
   end
 
+  def test_validaton_with_if_and_on
+    Topic.validates_presence_of :title, :if => Proc.new{|x| x.author_name = "bad"; true }, :on => :update
+
+    t = Topic.new(:title => "")
+
+    # If block should not fire
+    assert t.valid?
+    assert t.author_name.nil?
+
+    # If block should fire
+    assert t.invalid?(:update)
+    assert t.author_name == "bad"
+  end
+
   def test_invalid_should_be_the_opposite_of_valid
     Topic.validates_presence_of :title
 
-- 
cgit v1.2.3