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

Commit

Permalink
Allow passing policy class to scope (#13)
Browse files Browse the repository at this point in the history
* Allow passing policy class to scope

* Bump version

* Remove dead code
  • Loading branch information
phyrog authored Sep 6, 2017
1 parent 8987e35 commit 93f8233
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ field :posts
end
```

In case you only want to specify the Policy class containing the Scope explicitly, you can pass the Policy class explicitly:

```ruby
field :posts
scope PostablePolicy
resolve ...
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
10 changes: 0 additions & 10 deletions lib/graphql-pundit/instrumenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ def instrument(type, field)
scoped_field = scope_instrumenter.instrument(type, field)
authorization_instrumenter.instrument(type, scoped_field)
end

def authorize(current_user, obj, args, ctx, options)
if options[:proc]
options[:proc].call(obj, args, ctx)
else
::Pundit.authorize(ctx[current_user],
options[:record] || obj,
options[:query].to_s + '?')
end
end
end
end
end
6 changes: 5 additions & 1 deletion lib/graphql-pundit/instrumenters/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def instrument(_type, field)
old_resolve = field.resolve_proc

scope_proc = lambda do |obj, _args, ctx|
unless inferred?(scope)
obj.define_singleton_method(:policy_class) { scope }
end

::Pundit.policy_scope!(ctx[current_user], obj)
end
scope_proc = scope if proc?(scope)
Expand All @@ -40,7 +44,7 @@ def instrument(_type, field)
private

def valid_value?(value)
inferred?(value) || proc?(value)
value.is_a?(Class) || inferred?(value) || proc?(value)
end

def proc?(value)
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.2.0'
VERSION = '0.3.0'
end
end
33 changes: 31 additions & 2 deletions spec/graphql-pundit/instrumenters/scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test?
end
end

context 'explicit scope' do
context 'explicit scope proc' do
let(:field) do
GraphQL::Field.define(type: 'String') do
name :notTest
Expand All @@ -71,6 +71,20 @@ def test?
expect(result).to match_array([22, 48])
end
end

context 'explicit scope class' do
let(:field) do
GraphQL::Field.define(type: 'String') do
name :notTest
scope ScopeTestPolicy
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 'with authorization' do
Expand All @@ -89,7 +103,7 @@ def test?
end
end

context 'explicit scope' do
context 'explicit scope proc' do
let(:field) do
GraphQL::Field.define(type: 'String') do
name :test
Expand All @@ -103,6 +117,21 @@ def test?
expect(result).to eq(nil)
end
end

context 'explicit scope class' do
let(:field) do
GraphQL::Field.define(type: 'String') do
name :test
authorize
scope ScopeTestPolicy
resolve ->(obj, _args, _ctx) { obj.to_a }
end
end

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

context 'invalid scope argument' do
Expand Down

0 comments on commit 93f8233

Please sign in to comment.