diff options
author | Michael Koziarski <michael@koziarski.com> | 2007-08-28 23:17:17 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2007-08-28 23:17:17 +0000 |
commit | 5840108b6e7f8b7af426d701eee8a8989368da07 (patch) | |
tree | 8f0862096c977d246f3ed76f05b1d65aaafd845b /activerecord | |
parent | 93eaaef43c5614a61bde3eace9a539b5703f3168 (diff) | |
download | rails-5840108b6e7f8b7af426d701eee8a8989368da07.tar.gz rails-5840108b6e7f8b7af426d701eee8a8989368da07.tar.bz2 rails-5840108b6e7f8b7af426d701eee8a8989368da07.zip |
Pass the right binding when string is passed to :if with validations. [caspercg] Closes #9300
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7365 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/validations_test.rb | 22 |
2 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index dcc8caa84d..f259c2bbb6 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -334,7 +334,7 @@ module ActiveRecord def evaluate_condition(condition, record) case condition when Symbol: record.send(condition) - when String: eval(condition, binding) + when String: eval(condition, record.send(:binding)) else if condition_block?(condition) condition.call(record) diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb index 847e11a747..47a4d4d034 100755 --- a/activerecord/test/validations_test.rb +++ b/activerecord/test/validations_test.rb @@ -1127,6 +1127,28 @@ class ValidationsTest < Test::Unit::TestCase assert !t.valid? assert_equal "can't be blank", t.errors.on("title").first end + + # previous implementation of validates_presence_of eval'd the + # string with the wrong binding, this regression test is to + # ensure that it works correctly + def test_validation_with_if_as_string + Topic.validates_presence_of(:title) + Topic.validates_presence_of(:author_name, :if => "title.to_s.match('important')") + + t = Topic.new + assert !t.valid?, "A topic without a title should not be valid" + assert !t.errors.invalid?("author_name"), "A topic without an 'important' title should not require an author" + + t.title = "Just a title" + assert t.valid?, "A topic with a basic title should be valid" + + t.title = "A very important title" + assert !t.valid?, "A topic with an important title, but without an author, should not be valid" + assert t.errors.invalid?("author_name"), "A topic with an 'important' title should require an author" + + t.author_name = "Hubert J. Farnsworth" + assert t.valid?, "A topic with an important title and author should be valid" + end end |