Sha256: ca68280d3d51d713853c1865c8bea6e30963df27b27fdd67a6f7d5f4212e4904
Contents?: true
Size: 1.13 KB
Versions: 10
Compression:
Stored size: 1.13 KB
Contents
module RestfulResource module RailsValidations module ClassMethods def put(id, data: {}, **) super rescue HttpClient::UnprocessableEntity => e errors = parse_json(e.response.body) result = nil if errors.is_a?(Hash) && errors.has_key?('errors') result = data.merge(errors) else result = data.merge(errors: errors) end result = result.merge(id: id) self.new(result) end def post(data: {}, **) with_validations(data: data) { super } end def get(*) with_validations { super } end private def with_validations(data: {}) yield rescue HttpClient::UnprocessableEntity => e errors = parse_json(e.response.body) result = nil if errors.is_a?(Hash) && errors.has_key?('errors') result = data.merge(errors) else result = data.merge(errors: errors) end self.new(result) end end def self.included(base) base.extend(ClassMethods) end def valid? @inner_object.errors.nil? end end end
Version data entries
10 entries across 10 versions & 1 rubygems