Sha256: 08670a99f1afafe524e9662b27a353f9a54bd999c32357d6ec5d8f47d5c582a2

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

module JSONAPIonify::Api
  class Relationship::One < Relationship

    prepend_class do
      rel = self.rel
      remove_action :list, :create

      class << self
        undef_method :list
      end

      define_singleton_method(:show) do |content_type: nil, callbacks: true, &block|
        options = {
          content_type: content_type,
          callbacks:    callbacks,
          cacheable:    true,
          prepend:      'relationships'
        }
        define_action(:show, 'GET', **options, &block).response status: 200 do |context|
          context.response_object[:data] = build_resource_identifier(context.instance)
          context.response_object.to_json
        end
      end

      define_singleton_method(:replace) do |content_type: nil, callbacks: true, &block|
        options = {
          content_type: content_type,
          callbacks:    callbacks,
          cacheable:    false,
          prepend:      'relationships'
        }
        define_action(:replace, 'PATCH', **options, &block).response status: 200 do |context|
          context.response_object[:data] = build_resource_identifier(context.instance)
          context.response_object.to_json
        end
      end

      context :instance do |context|
        instance_exec rel.name, context.owner, context, &rel.resolve
      end

      show
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jsonapionify-0.10.2 lib/jsonapionify/api/relationship/one.rb
jsonapionify-0.10.1 lib/jsonapionify/api/relationship/one.rb
jsonapionify-0.10.0 lib/jsonapionify/api/relationship/one.rb
jsonapionify-0.9.3 lib/jsonapionify/api/relationship/one.rb