Sha256: be6a27717b60be01fa58d3e4dcac35535fa9ba8ec8a715a9b74085382cdd439f

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# Adds support for 'new' route in resource controller, which returns an object
# initialized by the server with defaults.

class ActiveRecordOperationsProcessor
  def _run_new_operation_callbacks
    return yield
  end
end

module JSONAPI
  class NewOperation < JSONAPI::Operation
    attr_reader :include_directives

    def initialize(resource_klass, options = {})
      @include_directives = options[:include_directives]
      @transactional = false
      super(resource_klass, options)
    end

    def apply(context)
      resource_record = resource_klass.create(context)
      return JSONAPI::ResourceOperationResult.new(:ok, resource_record)

    rescue JSONAPI::Exceptions::Error => e
      return JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors)
    end
  end
end

JSONAPI::Request.class_eval do
  def setup_new_action(params)
    parse_fields(params[:fields])
    parse_include_directives(params[:include])
    add_new_operation
  end

  def add_new_operation
    @operations.push JSONAPI::NewOperation.new(
                       @resource_klass,
                       {
                         include_directives: @include_directives
                       }
                     )
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
revelry_data-0.0.8.1 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.0.10 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.0.8 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.0.7 lib/revelry_data/jsonapi_resources_patch.rb