Sha256: ff77693708a7410f2e698940537d3087bbaa51562f4ea451634647accb682f56

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

module RestfulResource
  module RailsValidations
    module ClassMethods
      def put(id, data: {}, **params)
        begin
          super(id, data: data, **params)
        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 post(data: {}, **params)
        begin
          super(data: data, **params)
        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
    end

    def self.included(base)
      base.extend(ClassMethods)
    end

    def valid?
      @inner_object.errors.nil?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restful_resource-0.8.28 lib/restful_resource/rails_validations.rb
restful_resource-0.8.27 lib/restful_resource/rails_validations.rb