Skip to content

Instantly share code, notes, and snippets.

@tarolandia
Created September 30, 2013 12:03
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 tarolandia/6762796 to your computer and use it in GitHub Desktop.
Save tarolandia/6762796 to your computer and use it in GitHub Desktop.
# Grabs errors from a json response.
def from_json(json, save_cache = false)
array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue []
from_array array, save_cache
end
def from_array(messages, save_cache = false)
clear unless save_cache
humanized_attributes = Hash[@base.attributes.keys.map { |attr_name| [attr_name.humanize, attr_name] }]
messages.each do |message|
attr_message = humanized_attributes.keys.detect do |attr_name|
if message[0, attr_name.size + 1] == "#{attr_name} "
add humanized_attributes[attr_name], message[(attr_name.size + 1)..-1]
end
end
self[:base] << message if attr_message.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment