Skip to content

Instantly share code, notes, and snippets.

@tatat
Created September 26, 2014 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatat/e2dc5b4d00d43c1f367a to your computer and use it in GitHub Desktop.
Save tatat/e2dc5b4d00d43c1f367a to your computer and use it in GitHub Desktop.
require 'active_model'
class ActiveModel::Errors
module HiddenEntity
attr_accessor :_entity
end
def generate_message_with_format(attribute, type = :invalid, options = {})
message = generate_message_without_format attribute, type, options
if message.is_a?(Hash)
entity, message = message, message[:text]
message.extend HiddenEntity
message._entity = entity
end
message
end
alias_method_chain :generate_message, :format
def full_message_with_format(attribute, message)
if message.is_a?(HiddenEntity)
return message._entity[:text] if attribute == :base
attr_name = attribute.to_s.tr('.', '_').humanize
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
I18n.interpolate message._entity[:format], attribute: attr_name, message: message._entity[:text]
else
full_message_without_format attribute, message
end
end
alias_method_chain :full_message, :format
end
# ja:
# activerecord:
# errors:
# messages:
# blank:
# text: "が空ですよ"
# format: "なにやってるんですか%{attribute}%{message}"
#
# みたいにできるようになる(と思う)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment