Created
July 1, 2015 17:57
-
-
Save tatat/6c9bd86575616e211ed7 to your computer and use it in GitHub Desktop.
This file contains 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 WithSymbol | |
attr_accessor :symbol_value | |
end | |
def add_with_symbol(attribute, message = :invalid, options = {}) | |
add_without_symbol(attribute, message, options).tap do |messages| | |
normalized_message = messages.last | |
normalized_message.extend WithSymbol | |
if message.is_a? Symbol | |
normalized_message.symbol_value = message | |
end | |
end | |
end | |
alias_method_chain :add, :symbol | |
def symbolized_messages | |
list = messages.map do |attribute, messages| | |
[attribute, messages.map(&:symbol_value)] | |
end | |
Hash[list] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment