Created
September 26, 2014 14:17
-
-
Save tatat/e2dc5b4d00d43c1f367a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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