Sha256: 77e79c4f3625995bd09eee6264e1a38987e70f27814c1e6fc2fa742cd71725e5

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

module RestfulResource
  module RailsValidations
    module ClassMethods
      def put(id, data: {}, **others)
        with_validations(id, data: data) { super }
      end

      def patch(id, data: {}, **others)
        with_validations(id, data: data) { super }
      end

      def post(data: {}, **)
        with_validations(data: data) { super }
      end

      def get(*)
        with_validations { super }
      end

      def delete(*)
        with_validations { super }
      end

      private

      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
    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.13.2 lib/restful_resource/rails_validations.rb
restful_resource-2.13.1 lib/restful_resource/rails_validations.rb
restful_resource-2.13.0 lib/restful_resource/rails_validations.rb
restful_resource-2.12.1 lib/restful_resource/rails_validations.rb
restful_resource-2.12.0 lib/restful_resource/rails_validations.rb
restful_resource-2.11.0 lib/restful_resource/rails_validations.rb
restful_resource-2.10.3 lib/restful_resource/rails_validations.rb
restful_resource-2.10.1 lib/restful_resource/rails_validations.rb
restful_resource-2.10.0 lib/restful_resource/rails_validations.rb
restful_resource-2.9.1 lib/restful_resource/rails_validations.rb