Sha256: d54fa36d06b69d5c45cf0f36749888b639df0a4dd06ee29560060f423d02b739

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

Contents

require 'jsonapi/operation_result'
require 'jsonapi/callbacks'

module JSONAPI
  class OperationsProcessor
    include Callbacks
    define_jsonapi_resources_callbacks :operation, :operations

    def process(request)
      @results = []
      @request = request
      @context = request.context
      @operations = request.operations

      run_callbacks :operations do
        transaction do
          @operations.each do |operation|
            @operation = operation
            @result = nil
            run_callbacks :operation do
              @result = @operation.apply(@context)
              @results.push(@result)
              if @result.has_errors?
                rollback
              end
            end
          end
        end
      end
      @results
    end

    private

    # The base OperationsProcessor provides no transaction support
    # Override the transaction and rollback methods to provide transaction support.
    # For ActiveRecord transactions you can use the ActiveRecordOperationsProcessor
    def transaction
      yield
    end

    def rollback
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
jsonapi-resources-0.3.3 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.3.2 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.3.1 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.3.0 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.3.0.pre2 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.3.0.pre1 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.2.0 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.1.1 lib/jsonapi/operations_processor.rb
jsonapi-resources-0.1.0 lib/jsonapi/operations_processor.rb