diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-04-05 04:32:14 -0300 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-04-05 04:32:14 -0300 |
commit | 58bdf2790b6c61066a580d140117a105854d6051 (patch) | |
tree | 7fd6f10a30d2fd7ea8bd9320a5fee15eefac0cfd | |
parent | 817d109c0ab6a7ab0061300b8be21ad823cb30fc (diff) | |
parent | 7bdb4b5598ec9bd7473f52e59ee58682c4d23801 (diff) | |
download | rails-58bdf2790b6c61066a580d140117a105854d6051.tar.gz rails-58bdf2790b6c61066a580d140117a105854d6051.tar.bz2 rails-58bdf2790b6c61066a580d140117a105854d6051.zip |
Merge pull request #24119 from prathamesh-sonpatki/proc-messages
Allow passing record being validated to error message generator
-rw-r--r-- | activemodel/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 3 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 8 |
3 files changed, 15 insertions, 1 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index fb7ab5cb40..7be8b2e522 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,8 @@ +* Allow passing record being validated to the message proc to generate + customized error messages for that object using I18n helper. + + *Prathamesh Sonpatki* + ## Rails 5.0.0.beta3 (February 24, 2016) ## * No changes. diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 836201535f..d925960b41 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -486,7 +486,8 @@ module ActiveModel default: defaults, model: @base.model_name.human, attribute: @base.class.human_attribute_name(attribute), - value: value + value: value, + object: @base }.merge!(options) I18n.translate(key, options) 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 |