Sha256: 2edc5dbf59610b900bae31ef8efd44843581b6abc3856adeee853a9c40940a33

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 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
      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,
                       {
                         context: @context,
                         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

5 entries across 5 versions & 1 rubygems

Version Path
revelry_data-0.1.6 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.0.15 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.1.5 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.1.4 lib/revelry_data/jsonapi_resources_patch.rb
revelry_data-0.1.3 lib/revelry_data/jsonapi_resources_patch.rb