Skip to content

Instantly share code, notes, and snippets.

@rubyrider
Created May 11, 2013 10:30
Show Gist options
  • Save rubyrider/5559563 to your computer and use it in GitHub Desktop.
Save rubyrider/5559563 to your computer and use it in GitHub Desktop.
def self.convert_key_to_human_readable_name(obj, options)
messages = []
class_name = obj.class.name.underscore
errors = obj.errors.messages
separator = options[:separator].present? ? options[:separator] : ','
errors.each do |key, value|
base_key = (options[key].present? && options.keys.include?(key)) ? options[key].to_s : key.to_s
values = value.each { |val| val.downcase }
values.flatten.join(" #{separator}") unless values.empty?
if key == :base
message = " #{values.join()}"
else
message = I18n.t("activerecord.errors.#{class_name}.#{(base_key)}") + " #{values.join()}"
end
messages.push message
end
messages
end
def self.human_readable_error(obj = nil, options = {})
class_name = obj.class.name.underscore.humanize.pluralize unless obj.nil?
messages = []
if obj
messages = convert_key_to_human_readable_name(obj, options)
end
exception = options[:exception].message if options[:exception]
if exception
msg = exception.split('base').last.strip if exception.match('base')
msg = exception
msg.split(',').each do |m|
table_names.each do |table|
next if !m.include?(table)
m.gsub!('Validation failed:', '')
m.gsub!("#{table}", '')
m.gsub!("#{table.singularize.downcase}", '')
messages << m.strip.humanize
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment