Sha256: 6e72f038abdb5d5c138052af1613d49c24badd8e735a155c27236e4dfbf0eb9e

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 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

JSONAPI::ActsAsResourceController.class_eval do
  def base_response_meta
    {
      current_user: current_user,
      csrf_token: form_authenticity_token,
      current_org: current_user.try(:org),
      flashes: flash,
    }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
revelry_data-0.1.1 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.1.0 lib/revelry_data/jsonapi_resources_patch.rb