lib/quirky-api/response/errors.rb
# Retrieves all errors on an object and maps each error to the specific
# problematic field.
#
# @param errors [Object] An errors object that will be 'translated'.
#
Retrieves all errors on an object and maps each error to the specific
problematic field.
Retrieves all errors on an object and maps each error to the specific problematic field .
def translate_errors(errors, prepend_string = nil)
        model = errors.instance_variable_get('@base')
        errors.each_with_object({}) do |(key, error), hsh|
          key = key.to_s.split('.').last if key.match(/\./)
          full_message = if key.to_s == 'base'
                           error
                         else
                           col = model.class.human_attribute_name(key)
                           str = ''
                           if prepend_string
                             unless prepend_string.blank?
                               str += "#{prepend_string} "
                             end
                           else
                             str += "#{col} "
                           end
                           str += error
                           (str.slice(0) || '').upcase + (str.slice(1..-1) || '')
                         end
          (hsh[key] ||= []) << full_message
        end
      end
def translate_errors ( errors , prepend_string = nil ) model = errors . instance_variable_get ( '@base' ) errors . each_with_object ( { } ) do | ( key , error ) , hsh | key = key . to_s . split ( '.' ) . last if key . match ( / \. / ) full_message = if key . to_s == 'base' error else col = model . class . human_attribute_name ( key ) str = '' if prepend_string unless prepend_string . blank? str += "#{prepend_string} " end else str += "#{col} " end str += error ( str . slice ( 0 ) || '' ) . upcase + ( str . slice ( 1 .. - 1 ) || '' ) end ( hsh [ key ] ||= [ ] ) << full_message end end
Retrieves all errors on an object and maps each error to the specific
problematic field.
Retrieves all errors on an object and maps each error to the specific problematic field .
# Gets the model that has the errors. # Some nested attributes get a weird dot syntax. # Retrieves the full error and cleans it as necessary.
errors
An errors object that will be 'translated'.
An errors object that will be ' translated ' .
prepend_string