Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created September 2, 2010 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josevalim/562212 to your computer and use it in GitHub Desktop.
Save josevalim/562212 to your computer and use it in GitHub Desktop.
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index ed82c6b..f7851df 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -179,11 +179,14 @@ module ActiveModel
# If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error.
def add(attribute, message = nil, options = {})
message ||= :invalid
+ options[:message] ||= message
- if message.is_a?(Symbol)
- message = generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS))
- elsif message.is_a?(Proc)
- message = message.call
+ [ :message, :full_message ].each do |key|
+ if options[key].is_a?(Symbol)
+ message = generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS), key)
+ elsif options[key].is_a?(Proc)
+ message = message.call
+ end
end
self[attribute] << message
@@ -278,25 +281,19 @@ module ActiveModel
# <li><tt>errors.attributes.title.blank</tt></li>
# <li><tt>errors.messages.blank</tt></li>
# </ol>
- def generate_message(attribute, type = :invalid, options = {})
- type = options.delete(:message) if options[:message].is_a?(Symbol)
-
+ def generate_message(attribute, type = :invalid, options = {}, key)
+ type = options.delete(key) if options[key].is_a?(Symbol)
+ # TODO: This will lookup in the same place for both full_messages and messages
defaults = @base.class.lookup_ancestors.map do |klass|
[ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.attributes.#{attribute}.#{type}",
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.underscore}.#{type}" ]
end
- defaults << options.delete(:message)
- defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
+ defaults << options[key]
+ defaults << :"#{@base.class.i18n_scope}.errors.#{key}s.#{type}"
defaults << :"errors.attributes.#{attribute}.#{type}"
- defaults << :"errors.messages.#{type}"
+ defaults << :"errors.#{key}s.#{type}"
defaults.compact!
defaults.flatten!
@@ -309,7 +306,7 @@ module ActiveModel
:model => @base.class.model_name.human,
:attribute => @base.class.human_attribute_name(attribute),
:value => value
- }.merge(options)
+ }.merge(options.except(:message, :full_message))
I18n.translate(key, options)
end
@rosenfeld
Copy link

It's hard to understand just looking at the patch. I will need to apply it when I get home to understand it better. For instance, at first, it seems that the last changed line should read merge! instead of merge, but it is hard to know without the entire context. I'll take a closer look at it when I get home, but surely full_errors.messages looks strange. I'll think more about this tonight...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment