Created
September 30, 2013 12:03
-
-
Save tarolandia/6762796 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
# 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