lib/graphql-pundit.rb in graphql-pundit-0.4.0 vs lib/graphql-pundit.rb in graphql-pundit-0.5.0
- old
+ new
@@ -3,14 +3,21 @@
require 'graphql-pundit/instrumenter'
require 'graphql-pundit/version'
require 'graphql'
-# Define `authorize` and `authorize!` helpers
+# Defines authorization related helpers
module GraphQL
- def self.assign_authorize(raise_unauthorized)
- lambda do |defn, query = nil, policy: nil, record: nil|
+ # Defines `authorize` and `authorize!` helpers
+ class AuthorizationHelper
+ attr_reader :raise_unauthorized
+
+ def initialize(raise_unauthorized)
+ @raise_unauthorized = raise_unauthorized
+ end
+
+ def call(defn, query = nil, policy: nil, record: nil)
opts = {record: record,
query: query || defn.name,
policy: policy,
raise: raise_unauthorized}
if query.respond_to?(:call)
@@ -19,16 +26,17 @@
Define::InstanceDefinable::AssignMetadataKey.new(:authorize).
call(defn, opts)
end
end
- def self.assign_scope
- lambda do |defn, proc = :infer_scope|
+ # Defines `scope` helper
+ class ScopeHelper
+ def call(defn, proc = :infer_scope)
Define::InstanceDefinable::AssignMetadataKey.new(:scope).
call(defn, proc)
end
end
- Field.accepts_definitions(authorize: assign_authorize(false),
- authorize!: assign_authorize(true),
- scope: assign_scope)
+ Field.accepts_definitions(authorize: AuthorizationHelper.new(false),
+ authorize!: AuthorizationHelper.new(true),
+ scope: ScopeHelper.new)
end