Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Change way of finding policy for scopes (#29)
Browse files Browse the repository at this point in the history
* Change way of finding policy for scopes

* Fix weird sharing error

* Obey review

* Bump patch version
  • Loading branch information
phyrog authored Nov 27, 2017
1 parent 4df7182 commit e43207c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
25 changes: 19 additions & 6 deletions lib/graphql-pundit/instrumenters/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ def initialize(current_user, scope, old_resolver)
raise ArgumentError, 'Invalid value passed to `scope`'
end

@scope = new_scope(scope)
@scope = scope
end

def call(root, arguments, context)
new_scope = scope.call(root, arguments, context)
scope_proc = new_scope(scope)
new_scope = scope_proc.call(root, arguments, context)
old_resolver.call(new_scope, arguments, context)
end

Expand All @@ -33,11 +34,23 @@ def new_scope(scope)
return scope if proc?(scope)

lambda do |root, _arguments, context|
unless inferred?(scope)
root.define_singleton_method(:policy_class) { scope }
end
scope = find_scope(root, scope)
scope.new(context[current_user], root).resolve
end
end

::Pundit.policy_scope!(context[current_user], root)
def find_scope(root, scope)
if !inferred?(scope)
scope::Scope
else
# Special case for Sequel datasets that do not respond to
# ActiveModel's model_name
infer_from = if root.respond_to?(:model)
root.model
else
root
end
::Pundit::PolicyFinder.new(infer_from).scope!
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-pundit/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module GraphQL
module Pundit
VERSION = '0.5.0'
VERSION = '0.5.1'
end
end
37 changes: 37 additions & 0 deletions spec/graphql-pundit/instrumenters/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def to_a
end
end

class ScopeTestDataset < ScopeTest
def model
ScopeTest
end
end

class ScopeTestPolicy
class Scope
attr_reader :scope
Expand Down Expand Up @@ -56,6 +62,21 @@ def test?
it 'filters the list' do
expect(result).to match_array([1, 2, 3])
end

context 'scope from model' do
subject { ScopeTestDataset.new([1, 2, 3, 22, 48]) }
let(:field) do
GraphQL::Field.define(type: 'String') do
name :notTest
scope
resolve ->(obj, _args, _ctx) { obj.to_a }
end
end

it 'filters the list' do
expect(result).to match_array([1, 2, 3])
end
end
end

context 'explicit scope proc' do
Expand Down Expand Up @@ -101,6 +122,22 @@ def test?
it 'returns nil' do
expect(result).to eq(nil)
end

context 'scope from model' do
subject { ScopeTestDataset.new([1, 2, 3, 22, 48]) }
let(:field) do
GraphQL::Field.define(type: 'String') do
name :test
authorize policy: :scope_test
scope
resolve ->(obj, _args, _ctx) { obj.to_a }
end
end

it 'returns nil' do
expect(result).to eq(nil)
end
end
end

context 'explicit scope proc' do
Expand Down

0 comments on commit e43207c

Please sign in to comment.