diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 1431228..4e8c470 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -170,6 +170,16 @@ module ActiveModel
self
end
+ # Represents an error message. Contains the model instance (+base+), the +attribute+ and the +options+
+ # passed to validators.
+ class ErrorMessage < String
+ attr_reader :base, :attribute, :options
+ def initialize(message, base, attribute, options)
+ super(message)
+ @base, @attribute, @options = base, attribute, options
+ end
+ end
+
# Adds +message+ to the error messages on +attribute+, which will be returned on a call to
# on(attribute) for the same attribute. More than one error can be added to the same
# +attribute+ in which case an array will be returned on a call to on(attribute).
@@ -245,7 +255,8 @@ module ActiveModel
options = { :default => "%{attribute} %{message}", :attribute => attr_name }
messages.each do |m|
- full_messages << I18n.t(:"errors.format", options.merge(:message => m))
+ full_messages << ((m.is_a?(ErrorMessage) && m.options[:full_message]) ?
+ m : I18n.t(:"errors.format", options.merge(:message => m)))
end
end
end
@@ -279,13 +290,15 @@ module ActiveModel
#
errors.messages.blank
#
def generate_message(attribute, type = :invalid, options = {})
- type = options.delete(:message) if options[:message].is_a?(Symbol)
+ original_options = options.clone
+ message = [options.delete(:full_message), options.delete(:message)].compact[0]
+ message, type = nil, message if message.is_a? Symbol
if options[:default]
ActiveSupport::Deprecation.warn \
"ActiveModel::Errors#generate_message(attributes, custom_message) has been deprecated.\n" +
"Use ActiveModel::Errors#generate_message(attributes, :message => 'your message') instead."
- options[:message] = options.delete(:default)
+ message = options.delete(:default)
end
defaults = @base.class.lookup_ancestors.map do |klass|
@@ -293,12 +306,11 @@ module ActiveModel
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ]
end
- defaults << options.delete(:message)
+ defaults << message if message
defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
defaults << :"errors.attributes.#{attribute}.#{type}"
defaults << :"errors.messages.#{type}"
- defaults.compact!
defaults.flatten!
key = defaults.shift
@@ -311,7 +323,7 @@ module ActiveModel
:value => value
}.merge(options)
- I18n.translate(key, options)
+ ErrorMessage.new I18n.translate(key, options), @base, attribute, original_options
end
end
end