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

Version Path
restful_resource-2.3.0 lib/restful_resource/rails_validations.rb
restful_resource-2.2.8 lib/restful_resource/rails_validations.rb
restful_resource-2.2.7 lib/restful_resource/rails_validations.rb
restful_resource-2.2.6 lib/restful_resource/rails_validations.rb
restful_resource-2.2.5 lib/restful_resource/rails_validations.rb
restful_resource-2.2.4 lib/restful_resource/rails_validations.rb
restful_resource-2.2.3 lib/restful_resource/rails_validations.rb
restful_resource-2.2.2 lib/restful_resource/rails_validations.rb
restful_resource-2.2.1 lib/restful_resource/rails_validations.rb
restful_resource-2.2.0 lib/restful_resource/rails_validations.rb