aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIain Hecker <github@iain.nl>2008-08-16 21:45:23 +0200
committerSven Fuchs <svenfuchs@artweb-design.de>2008-08-20 17:57:53 +0200
commitfc0358ae16d5631dd1d996b2cf95a8987f9d0c8d (patch)
treec022a5462e8f24ee5e9d0ee9a962c50049dae61a
parente43cdb93f08544304c10198211d17868f0881030 (diff)
downloadrails-fc0358ae16d5631dd1d996b2cf95a8987f9d0c8d.tar.gz
rails-fc0358ae16d5631dd1d996b2cf95a8987f9d0c8d.tar.bz2
rails-fc0358ae16d5631dd1d996b2cf95a8987f9d0c8d.zip
Added :value as interpolation variable available to error messages
-rw-r--r--activerecord/lib/active_record/validations.rb18
-rw-r--r--activerecord/test/cases/validations_i18n_test.rb4
2 files changed, 12 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 040681a09c..a442d008a9 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -92,17 +92,19 @@ module ActiveRecord
:"custom.#{klass.name.underscore}.#{message}" ]
end
- defaults << options[:default] if options[:default]
- defaults.flatten! << message
+ defaults << options.delete(:default)
+ defaults = defaults.compact.flatten << message
model_name = @base.class.name
key = defaults.shift
-
- options.merge!({
- :default => defaults,
- :model => @base.class.human_name,
- :attribute => @base.class.human_attribute_name(attribute.to_s),
- :scope => [:activerecord, :errors, :messages] })
+ value = @base.send(attribute) if @base.respond_to?(attribute)
+
+ options = { :default => defaults,
+ :model => @base.class.human_name,
+ :attribute => @base.class.human_attribute_name(attribute.to_s),
+ :value => value,
+ :scope => [:activerecord, :errors, :messages]
+ }.merge(options)
I18n.translate(key, options)
end
diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb
index 96f86fbe0e..469e9d6c03 100644
--- a/activerecord/test/cases/validations_i18n_test.rb
+++ b/activerecord/test/cases/validations_i18n_test.rb
@@ -48,7 +48,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
I18n.expects(:translate).with(:topic, {:count => 1, :default => ['Topic'], :scope => [:activerecord, :models]}).returns('Topic')
I18n.expects(:translate).with(:'topic.title', {:count => 1, :default => ['Title'], :scope => [:activerecord, :attributes]}).returns('Title')
- I18n.expects(:translate).with(:"custom.topic.title.invalid", :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1')
+ I18n.expects(:translate).with(:"custom.topic.title.invalid", :value => nil, :scope => global_scope, :default => [:"custom.topic.invalid", 'default from class def error 1', :invalid], :attribute => "Title", :model => "Topic").returns('default from class def error 1')
@topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
end
@@ -56,7 +56,7 @@ class ActiveRecordValidationsI18nTests < Test::Unit::TestCase
custom_scope = [:activerecord, :errors, :custom, 'topic', :title]
I18n.expects(:translate).with(:reply, {:count => 1, :default => [:topic, 'Reply'], :scope => [:activerecord, :models]}).returns('Reply')
I18n.expects(:translate).with(:'reply.title', {:count => 1, :default => [:'topic.title', 'Title'], :scope => [:activerecord, :attributes]}).returns('Title')
- I18n.expects(:translate).with(:"custom.reply.title.invalid", :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def")
+ I18n.expects(:translate).with(:"custom.reply.title.invalid", :value => nil, :scope => [:activerecord, :errors, :messages], :default => [:"custom.reply.invalid", :"custom.topic.title.invalid", :"custom.topic.invalid", 'default from class def', :invalid], :model => 'Reply', :attribute => 'Title').returns("default from class def")
Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
end