graphqr 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30f68dd1bcb6907786a2bd497b056fc44e5cd5ff
4
- data.tar.gz: 33e51b0fe16f514a13afe9be920abd2f1adf4c5b
3
+ metadata.gz: d228d49d49c7960ee308e30a8323b652c8c35911
4
+ data.tar.gz: 6bb7c19653461926355c9a832d54ec99cf595e54
5
5
  SHA512:
6
- metadata.gz: d7b24cac875d94516b5dd8f08c69f4b6086310127acaa50daf0baf15427b9ad3c69edd3c711fbb7fb86f641ca0a94d7ce06260c0e5263a1ccdff25e62ef157b8
7
- data.tar.gz: 3635d74b54fddd9b2753cd7d21872f4be500b4400060d899783b7c6d7ce88b09e50be1fbe56ce2c467c7fb3d715b68af276eedfd760a03f84bd283039a8eb257
6
+ metadata.gz: '09e53ab5da54bad1ec25dd9c88909b14a3f63ecd39da55b66c94de7419b5fe0e1f64fcafb3b5af94797cbe9bb0f65ef706db69399652432dfa3f9342e3b167dc'
7
+ data.tar.gz: ecda93af4ec0a88e2a5c69cb56dc90e13dde9f64eef14656cc26c5823ddfee2ca01089b02ce3c2cd1c0d8b5a3e57525715524310f7abf010b89f45842b13a4e1
@@ -8,7 +8,9 @@ module GraphQR # rubocop:disable Style/Documentation
8
8
  yield self
9
9
  end
10
10
 
11
- # Returns the selected paginator
11
+ ##
12
+ # Returns the selected paginator.
13
+ # If no paginator is selected, it tries to find the one used
12
14
  def paginator
13
15
  if instance_variable_defined? :@paginator
14
16
  @paginator
@@ -29,11 +31,46 @@ module GraphQR # rubocop:disable Style/Documentation
29
31
  end
30
32
  end
31
33
 
34
+ ##
35
+ # Returns the selected policy_provider.
36
+ # If no policy_provider is selected, it tries to find the one used
37
+ def policy_provider
38
+ if instance_variable_defined? :@policy_provider
39
+ @policy_provider
40
+ else
41
+ set_policy_provider
42
+ end
43
+ end
44
+
45
+ ##
46
+ # Sets the preferred policy_provider
47
+ # TODO: support CanCan
48
+ def policy_provider=(policy_provider)
49
+ case policy_provider.to_sym
50
+ when :pundit
51
+ use_pundit
52
+ else
53
+ raise StandardError, "Unknown policy_provider: #{policy_provider}"
54
+ end
55
+ end
56
+
32
57
  private
33
58
 
34
59
  def set_paginator
35
60
  use_pagy if defined?(Pagy)
36
61
  end
62
+
63
+ def use_pagy
64
+ @paginator = :pagy
65
+ end
66
+
67
+ def set_policy_provider
68
+ use_pundit if defined?(Pundit)
69
+ end
70
+
71
+ def use_pundit
72
+ @policy_provider = :pundit
73
+ end
37
74
  end
38
75
 
39
76
  class << self
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pundit'
4
+
5
+ module GraphQR
6
+ module Policies
7
+ ##
8
+ # TODO: add documentation
9
+ class PunditProvider
10
+ attr_reader :policy_context
11
+
12
+ def initialize(policy_context:)
13
+ @policy_context = policy_context
14
+ end
15
+
16
+ def allowed?(action:, record:, policy_class: nil)
17
+ policy = policy_for(record: record, policy_class: policy_class)
18
+
19
+ policy.apply(action)
20
+ end
21
+
22
+ def authorized_records(records:)
23
+ Pundit.policy_scope(policy_context, records)
24
+ end
25
+
26
+ def permitted_field?(record:, field_name:)
27
+ policy = policy_for(record: record)
28
+
29
+ policy.permitted_fields.include?(field_name)
30
+ end
31
+
32
+ private
33
+
34
+ def policy_for(record:, policy_class: nil)
35
+ policy_class ||= policy_class_for(record: record)
36
+ policy_class.new(policy_context, record)
37
+ end
38
+
39
+ def policy_class_for(record:)
40
+ Pundit::PolicyFinder.new(record).policy!
41
+ end
42
+ end
43
+ end
44
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphQR
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
data/lib/graphqr.rb CHANGED
@@ -11,9 +11,17 @@ module GraphQR
11
11
  GraphQR.config.paginator
12
12
  end
13
13
 
14
+ def policy_provider
15
+ GraphQR.config.policy_provider
16
+ end
17
+
14
18
  def use_pagy?
15
19
  paginator == :pagy
16
20
  end
21
+
22
+ def use_pundit?
23
+ policy_provider == :pundit
24
+ end
17
25
  end
18
26
  end
19
27
 
@@ -29,6 +37,12 @@ rescue NameError
29
37
  Kernel.warn 'Pagy not found'
30
38
  end
31
39
 
40
+ begin
41
+ require 'graphqr/policies/pundit_provider'
42
+ rescue LoadError
43
+ Kernel.warn 'Pundit not found'
44
+ end
45
+
32
46
  require 'graphqr/apply_scopes'
33
47
  require 'graphqr/authorized'
34
48
  require 'graphqr/base'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Puyol
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-05-28 00:00:00.000000000 Z
13
+ date: 2019-05-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: graphql
@@ -153,6 +153,7 @@ files:
153
153
  - lib/graphqr/pagination/resolvers/pagy_resolver.rb
154
154
  - lib/graphqr/pagination/types/pagination_page_info_type.rb
155
155
  - lib/graphqr/permitted_fields_extension.rb
156
+ - lib/graphqr/policies/pundit_provider.rb
156
157
  - lib/graphqr/query_field.rb
157
158
  - lib/graphqr/scope_items.rb
158
159
  - lib/graphqr/version.rb