Sha256: 5fa257a6bcd5dc8a0574ce9925fea4b00d893be48930692bf600c444823de199

Contents?: true

Size: 1.59 KB

Versions: 24

Compression:

Stored size: 1.59 KB

Contents

module Graphiti
  module Base
    extend ActiveSupport::Concern

    def jsonapi_resource
      @jsonapi_resource
    end

    def query
      @query ||= Query.new(jsonapi_resource, params)
    end

    def query_hash
      @query_hash ||= query.hash
    end

    def wrap_context
      Graphiti.with_context(jsonapi_context, action_name.to_sym) do
        yield
      end
    end

    def jsonapi_context
      self
    end

    def jsonapi_scope(scope, opts = {})
      jsonapi_resource.build_scope(scope, query, opts)
    end

    def normalized_params
      normalized = params
      if normalized.respond_to?(:to_unsafe_h)
        normalized = normalized.to_unsafe_h.deep_symbolize_keys
      end
      normalized
    end

    def deserialized_params
      @deserialized_params ||= begin
        payload = normalized_params
        if payload[:data] && payload[:data][:type]
          Graphiti::Deserializer.new(payload)
        end
      end
    end

    def jsonapi_render_options
      options = {}
      options.merge!(default_jsonapi_render_options)
      options[:meta]   ||= {}
      options[:expose] ||= {}
      options[:expose][:context] = jsonapi_context
      options
    end

    def proxy(base = nil, opts = {})
      base ||= jsonapi_resource.base_scope
      scope_opts = opts.slice :sideload_parent_length,
        :default_paginate, :after_resolve
      scope = jsonapi_scope(base, scope_opts)
      ResourceProxy.new jsonapi_resource,
        scope,
        query,
        payload: deserialized_params,
        single: opts[:single],
        raise_on_missing: opts[:raise_on_missing]
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
graphiti-1.0.beta.5 lib/graphiti/base.rb
graphiti-1.0.beta.4 lib/graphiti/base.rb
graphiti-1.0.beta.3 lib/graphiti/base.rb
graphiti-1.0.beta.2 lib/graphiti/base.rb
graphiti-1.0.alpha.26 lib/graphiti/base.rb
graphiti-1.0.alpha.25 lib/graphiti/base.rb
graphiti-1.0.alpha.24 lib/graphiti/base.rb
graphiti-1.0.alpha.23 lib/graphiti/base.rb
graphiti-1.0.alpha.22 lib/graphiti/base.rb
graphiti-1.0.alpha.21 lib/graphiti/base.rb
graphiti-1.0.alpha.20 lib/graphiti/base.rb
graphiti-1.0.alpha.19 lib/graphiti/base.rb
graphiti-1.0.alpha.18 lib/graphiti/base.rb
graphiti-1.0.alpha.17 lib/graphiti/base.rb
graphiti-1.0.alpha.16 lib/graphiti/base.rb
graphiti-1.0.alpha.15 lib/graphiti/base.rb
graphiti-1.0.alpha.14 lib/graphiti/base.rb
graphiti-1.0.alpha.12 lib/graphiti/base.rb
graphiti-1.0.alpha.11 lib/graphiti/base.rb
graphiti-1.0.alpha.10 lib/graphiti/base.rb