module RestfulResource::RailsValidations::ClassMethods

Public Instance Methods

delete(id, **) click to toggle source
Calls superclass method
# File lib/restful_resource/rails_validations.rb, line 20
def delete(id, **)
  with_validations { super }
end
get(**) click to toggle source
Calls superclass method
# File lib/restful_resource/rails_validations.rb, line 16
def get(**)
  with_validations { super }
end
patch(id, data: {}, **others) click to toggle source
Calls superclass method
# File lib/restful_resource/rails_validations.rb, line 8
def patch(id, data: {}, **others)
  with_validations(id, data: data) { super }
end
post(data: {}, **) click to toggle source
Calls superclass method
# File lib/restful_resource/rails_validations.rb, line 12
def post(data: {}, **)
  with_validations(data: data) { super }
end
put(id, data: {}, **others) click to toggle source
Calls superclass method
# File lib/restful_resource/rails_validations.rb, line 4
def put(id, data: {}, **others)
  with_validations(id, data: data) { super }
end

Private Instance Methods

with_validations(id = nil, data: {}) { || ... } click to toggle source
# File lib/restful_resource/rails_validations.rb, line 26
def with_validations(id = nil, data: {})
  yield
rescue HttpClient::UnprocessableEntity => e
  errors = parse_json(e.response.body)
  result = if errors.is_a?(Hash) && errors.key?('errors')
             data.merge(errors)
           else
             data.merge(errors: errors)
           end

  result = result.merge(id: id) if id

  new(result)
end