Sha256: 95e0e0c2b910626dedde40515166d01a098e9c4349e55055e1a5467b6d17255e

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

module Graphiti
  class Resource
    module Interface
      extend ActiveSupport::Concern

      class_methods do
        def all(params = {}, base_scope = nil)
          validate!(params)
          _all(params, {}, base_scope)
        end

        # @api private
        def _all(params, opts, base_scope)
          runner = Runner.new(self, params, opts.delete(:query))
          runner.proxy(base_scope, opts)
        end

        def find(params = {}, base_scope = nil)
          validate!(params)
          _find(params, base_scope)
        end

        def _find(params = {}, base_scope = nil)
          id = params[:data].try(:[], :id) || params.delete(:id)
          params[:filter] ||= {}
          params[:filter].merge!(id: id) if id

          runner = Runner.new(self, params)
          runner.proxy(base_scope, single: true, raise_on_missing: true)
        end

        def build(params, base_scope = nil)
          validate!(params)
          runner = Runner.new(self, params)
          runner.proxy(base_scope, single: true, raise_on_missing: true)
        end

        private

        def validate!(params)
          return unless validate_endpoints?

          if context && context.respond_to?(:request)
            path = context.request.env['PATH_INFO']
            unless allow_request?(path, params, context_namespace)
              raise Errors::InvalidEndpoint.new(self, path, context_namespace)
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphiti-1.0.beta.5 lib/graphiti/resource/interface.rb
graphiti-1.0.beta.4 lib/graphiti/resource/interface.rb
graphiti-1.0.beta.3 lib/graphiti/resource/interface.rb
graphiti-1.0.beta.2 lib/graphiti/resource/interface.rb
graphiti-1.0.alpha.26 lib/graphiti/resource/interface.rb