Sha256: 235b60148d290365ad62abd90b7f891b2400da7f0592630072d065b453caae4a

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

class ActiveRecordOperationsProcessor < JSONAPI::OperationsProcessor
  private

  def transaction
    if @transactional
      ActiveRecord::Base.transaction do
        yield
      end
    else
      yield
    end
  end

  def rollback
    fail ActiveRecord::Rollback if @transactional
  end

  # Catch errors that should be handled before JSONAPI::Exceptions::Error
  # and other unprocessed exceptions
  def process_operation(operation)
    with_default_handling do 
      begin
        operation.apply
      rescue ActiveRecord::DeleteRestrictionError => e
        record_locked_error = JSONAPI::Exceptions::RecordLocked.new(e.message)
        return JSONAPI::ErrorsOperationResult.new(record_locked_error.errors[0].code, record_locked_error.errors)

      rescue ActiveRecord::RecordNotFound
        record_not_found = JSONAPI::Exceptions::RecordNotFound.new(operation.associated_key)
        return JSONAPI::ErrorsOperationResult.new(record_not_found.errors[0].code, record_not_found.errors)
      end
    end  
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsonapi-resources-0.7.0 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.6.2 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.6.1 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.6.0 lib/jsonapi/active_record_operations_processor.rb
jsonapi-resources-0.5.9 lib/jsonapi/active_record_operations_processor.rb