Skip to content

Instantly share code, notes, and snippets.

@tatat
Created July 1, 2015 17:57
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 tatat/6c9bd86575616e211ed7 to your computer and use it in GitHub Desktop.
Save tatat/6c9bd86575616e211ed7 to your computer and use it in GitHub Desktop.
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