From 7bdb4b5598ec9bd7473f52e59ee58682c4d23801 Mon Sep 17 00:00:00 2001 From: Prathamesh Sonpatki Date: Wed, 9 Mar 2016 15:36:45 +0530 Subject: Allow passing record being validated to error message generator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Pass object to I18n helper so that when calling message proc, it will pass that object as argument to the proc and we can generate custom error messages based on current record being validated. - Based on https://github.com/rails/rails/issues/856. [Ɓukasz Bandzarewicz, Prathamesh Sonpatki] --- activemodel/test/cases/validations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activemodel/test') diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index f0317ad219..18b8d9e4af 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -444,4 +444,12 @@ class ValidationsTest < ActiveModel::TestCase assert topic.invalid? assert duped.valid? end + + def test_validation_with_message_as_proc_that_takes_a_record_as_a_parameter + Topic.validates_presence_of(:title, message: proc { |record| "You have failed me for the last time, #{record.author_name}." }) + + t = Topic.new(author_name: 'Admiral') + assert t.invalid? + assert_equal ["You have failed me for the last time, Admiral."], t.errors[:title] + end end -- cgit v1.2.3