From b5961fbba884cf790f3f1366c80266c4e430b652 Mon Sep 17 00:00:00 2001 From: Tom Gehrke Date: Wed, 20 Jun 2018 10:24:43 +0200 Subject: [PATCH] Fix n problems in README and Gem (#52) * Fix two problems in README * Update README.md * Add notice for possible error when upgrading * Only define Field if using graphql >= 1.8 to avoid a load error --- README.md | 17 ++++++++++++++--- lib/graphql-pundit/field.rb | 11 +++++++---- lib/graphql-pundit/version.rb | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5c1f777..138c860 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Add this line to your application's Gemfile: ```ruby -gem 'graphql-pundit' +gem 'graphql-pundit', '~> 0.7.0' ``` And then execute: @@ -20,6 +20,15 @@ And then execute: $ bundle ``` +### Upgrade Notice + +If you're upgrading from an earlier version, make sure to delete your +`bootsnap` cache, to avoid a load error (see +[this issue](https://github.com/ontohub/graphql-pundit/issues/51)). +The cache files are usually located in the `tmp` directory in your +repository and are named `bootsnap-compile-cache` and +`bootsnap-load-path-cache`. + ## Usage ### Class based API (`graphql-ruby >= 1.8`) @@ -163,7 +172,7 @@ class User < BaseObject authorize: true, record: ->(obj, args, ctx) { ctx[:current_user] } field :password_hash, ... do - authorize policy: ->(obj, args, ctx) { ctx[:current_user] } + authorize record: ->(obj, args, ctx) { ctx[:current_user] } end # will use AccountPolicy#email? with the first account as the record (the policy was inferred from the record class) @@ -184,7 +193,9 @@ initialize the policy with, but for mutations there is no parent object. ###### `before_scope` and `after_scope` `before_scope` and `after_scope` can be used to apply Pundit scopes to the -fields. Both options can be combined freely within one field. +fields. Both options can be combined freely within one field. The result of +`before_scope` is passed to the resolver as the "parent object", while the +result of `after_scope` is returned as the result of the field. ```ruby class User < BaseObject diff --git a/lib/graphql-pundit/field.rb b/lib/graphql-pundit/field.rb index ef068a8..443a258 100644 --- a/lib/graphql-pundit/field.rb +++ b/lib/graphql-pundit/field.rb @@ -6,10 +6,13 @@ module GraphQL module Pundit - # Field class that contains authorization and scope behavior - class Field < GraphQL::Schema::Field - prepend GraphQL::Pundit::Scope - prepend GraphQL::Pundit::Authorization + if defined?(GraphQL::Schema::Field) + # Field class that contains authorization and scope behavior + # This only works with graphql >= 1.8.0 + class Field < GraphQL::Schema::Field + prepend GraphQL::Pundit::Scope + prepend GraphQL::Pundit::Authorization + end end end end diff --git a/lib/graphql-pundit/version.rb b/lib/graphql-pundit/version.rb index 8dd6720..44479b2 100644 --- a/lib/graphql-pundit/version.rb +++ b/lib/graphql-pundit/version.rb @@ -2,6 +2,6 @@ module GraphQL module Pundit - VERSION = '0.7.0' + VERSION = '0.7.1' end end