Sha256: 57a563955eaf4307adc4c2df68cb81005cbf3766c65df69b9f3f3be594b66d82

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module JSONAPIonify::Api
  module Resource::Defaults::ResponseContexts
    extend ActiveSupport::Concern

    included do
      before(:response) { |context| context.clear }

      context(:invalidate_cache?, readonly: true, persisted: true) { |c| c.includes.present? }

      # Response Objects
      context(:links, readonly: true, persisted: true) do |context|
        context.response_object[:links]
      end

      context(:meta, readonly: true, persisted: true) do |context|
        JSONAPIonify::Structure::Helpers::MetaDelegate.new context.response_object
      end

      context(:response_object, readonly: true, persisted: true) do |context|
        JSONAPIonify.parse(links: { self: context.request.url })
      end

      context(:response_collection, readonly: true) do |context|
        if context.root_request?
          collections = %i{
            paginated_collection
            sorted_collection
            collection
          }
          context.public_send collections.find { |c| context.respond_to? c }
        else
          context.collection
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jsonapionify-0.9.2 lib/jsonapionify/api/resource/defaults/response_contexts.rb
jsonapionify-0.9.1 lib/jsonapionify/api/resource/defaults/response_contexts.rb