graphql_rails 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1d9d24469554419e5b4b033eb06b1e8f0be82079285d31ee7dbfb1e8e19e825
4
- data.tar.gz: 3acc8f452a19ca66421a438103e8da1ed24da263ce551eb7d082ac67458011e8
3
+ metadata.gz: 309517d73a351c64e7d68b46d53cc6dc5b1305c1f782115bd5e88cde0d54ee34
4
+ data.tar.gz: 0fa2965c43db677ab4206eb383b23a995a1ee757d8857fd836d7f64f6524abf0
5
5
  SHA512:
6
- metadata.gz: 47974bc6d410ff9ed1215025aa4d06362494f87e20fee0c4259a3186ffa001a38c495b9ec7a9eff2428e966dc32ed52f776129cbab413abf874bc8ecb4334bc2
7
- data.tar.gz: aa1f164524942256b310088b0ae046f5d98b90e283f4c17f465c8d3ca64a3ca4b8cd836af78b2fe77332fe062518b00380ed68d22df57b61d6eca824f0f6caff
6
+ metadata.gz: f3769cec225cae791c8e71276f195a317f4a6bafcfb24d155896555aaa214380d54e62b573032c3b056e7da0b0133c8fb680e5055addc7c1df473d47b3a7073b
7
+ data.tar.gz: d0dd5023a8e0130f8e2f40ed34f5ddc636498420fc312e88e606e53749f37fff70cae4a0b0cd18f3d80fa973ab62ebd598bd61108202bae0a365accf17107277
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql_rails (0.4.0)
4
+ graphql_rails (0.4.1)
5
5
  activesupport (>= 4)
6
6
  graphql (~> 1)
7
7
 
@@ -22,10 +22,12 @@ GEM
22
22
  arel (9.0.0)
23
23
  ast (2.4.0)
24
24
  bson (4.3.0)
25
+ byebug (10.0.2)
25
26
  codecov (0.1.10)
26
27
  json
27
28
  simplecov
28
29
  url
30
+ coderay (1.1.2)
29
31
  concurrent-ruby (1.0.5)
30
32
  diff-lcs (1.3)
31
33
  docile (1.3.0)
@@ -33,6 +35,7 @@ GEM
33
35
  i18n (1.0.1)
34
36
  concurrent-ruby (~> 1.0)
35
37
  json (2.1.0)
38
+ method_source (0.9.0)
36
39
  minitest (5.11.3)
37
40
  mongo (2.5.1)
38
41
  bson (>= 4.3.0, < 5.0.0)
@@ -43,6 +46,12 @@ GEM
43
46
  parser (2.5.1.0)
44
47
  ast (~> 2.4.0)
45
48
  powerpack (0.1.1)
49
+ pry (0.11.3)
50
+ coderay (~> 1.1.0)
51
+ method_source (~> 0.9.0)
52
+ pry-byebug (3.6.0)
53
+ byebug (~> 10.0)
54
+ pry (~> 0.10)
46
55
  rainbow (3.0.0)
47
56
  rake (10.5.0)
48
57
  rspec (3.7.0)
@@ -88,6 +97,7 @@ DEPENDENCIES
88
97
  codecov
89
98
  graphql_rails!
90
99
  mongoid
100
+ pry-byebug
91
101
  rake (~> 10.0)
92
102
  rspec (~> 3.0)
93
103
  rubocop
@@ -164,6 +164,26 @@ class UsersController < GraphqlRails::Controller
164
164
  end
165
165
  ```
166
166
 
167
+ ## anonymous action filters
168
+
169
+ before/after/around action filters can be written as blocks too:
170
+
171
+ ```ruby
172
+ class UsersController < GraphqlRails::Controller
173
+ around_action do |controller, block|
174
+ I18n.with_locale('it') do
175
+ block.call
176
+ end
177
+ end
178
+
179
+ def create
180
+ User.create(params)
181
+ end
182
+ end
183
+ ```
184
+
185
+ it's not recommended but might be helpful in some edge cases.
186
+
167
187
  ### *only* and *except* option
168
188
 
169
189
  `UsersController.before_action` accepts `only` or `except` options which allows to skip filters for some actions.
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'bundler', '~> 1.16'
27
27
  spec.add_development_dependency 'rake', '~> 10.0'
28
28
  spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'pry-byebug'
29
30
  end
@@ -10,16 +10,20 @@ module GraphqlRails
10
10
  # base class for all graphql_rails controllers
11
11
  class Controller
12
12
  class << self
13
- def before_action(*args)
14
- controller_configuration.add_before_action(*args)
13
+ def inherited(sublass)
14
+ sublass.instance_variable_set(:@controller_configuration, controller_configuration.dup)
15
15
  end
16
16
 
17
- def around_action(*args)
18
- controller_configuration.add_around_action(*args)
17
+ def before_action(*args, &block)
18
+ controller_configuration.add_action_hook(:before, *args, &block)
19
19
  end
20
20
 
21
- def after_action(*args)
22
- controller_configuration.add_after_action(*args)
21
+ def around_action(*args, &block)
22
+ controller_configuration.add_action_hook(:around, *args, &block)
23
+ end
24
+
25
+ def after_action(*args, &block)
26
+ controller_configuration.add_action_hook(:after, *args, &block)
23
27
  end
24
28
 
25
29
  def action(action_name)
@@ -31,11 +35,14 @@ module GraphqlRails
31
35
  end
32
36
  end
33
37
 
38
+ attr_reader :action_name
39
+
34
40
  def initialize(graphql_request)
35
41
  @graphql_request = graphql_request
36
42
  end
37
43
 
38
44
  def call(method_name)
45
+ @action_name = method_name
39
46
  call_with_rendering(method_name)
40
47
 
41
48
  FormatResults.new(
@@ -44,6 +51,8 @@ module GraphqlRails
44
51
  params: params,
45
52
  graphql_context: graphql_request.context
46
53
  ).call
54
+ ensure
55
+ @action_name = nil
47
56
  end
48
57
 
49
58
  protected
@@ -10,6 +10,11 @@ module GraphqlRails
10
10
  class ActionConfiguration
11
11
  attr_reader :attributes, :return_type, :pagination_options
12
12
 
13
+ def initialize_copy(other)
14
+ super
15
+ @attributes = other.instance_variable_get(:@attributes).dup.transform_values(&:dup)
16
+ end
17
+
13
18
  def initialize
14
19
  @attributes = {}
15
20
  @can_return_nil = false
@@ -2,12 +2,13 @@
2
2
 
3
3
  module GraphqlRails
4
4
  class Controller
5
- # stores information about controller filter
6
- class ActionFilter
7
- attr_reader :name
5
+ # stores information about controller hooks like before_action, after_action, etc.
6
+ class ActionHook
7
+ attr_reader :name, :action_proc
8
8
 
9
- def initialize(name, only: [], except: [])
9
+ def initialize(name: nil, only: [], except: [], &action_proc)
10
10
  @name = name
11
+ @action_proc = action_proc
11
12
  @only_actions = Array(only).map(&:to_sym)
12
13
  @except_actions = Array(except).map(&:to_sym)
13
14
  end
@@ -22,6 +23,10 @@ module GraphqlRails
22
23
  end
23
24
  end
24
25
 
26
+ def anonymous?
27
+ !!action_proc # rubocop:disable Style/DoubleNegation
28
+ end
29
+
25
30
  private
26
31
 
27
32
  attr_reader :only_actions, :except_actions
@@ -10,43 +10,47 @@ module GraphqlRails
10
10
  end
11
11
 
12
12
  def call
13
- run_before_action_hooks
14
- run_around_actions { yield }.tap do
15
- run_after_action_hooks
16
- end
13
+ result = nil
14
+ run_action_hooks(:before)
15
+ run_around_action_hooks { result = yield }
16
+ run_action_hooks(:after)
17
+ result
17
18
  end
18
19
 
19
20
  private
20
21
 
21
22
  attr_reader :action_name, :controller
22
23
 
23
- def all_around_actions
24
- controller_configuration.around_actions_for(action_name)
24
+ def all_around_hooks
25
+ controller_configuration.action_hooks_for(:around, action_name)
25
26
  end
26
27
 
27
28
  def controller_configuration
28
29
  controller.class.controller_configuration
29
30
  end
30
31
 
31
- def run_around_actions(around_actions = all_around_actions, &block)
32
- pending_around_actions = around_actions.clone
33
- around_action = pending_around_actions.shift
32
+ def run_around_action_hooks(around_hooks = all_around_hooks, &block)
33
+ pending_around_hooks = around_hooks.clone
34
+ action_hook = pending_around_hooks.shift
34
35
 
35
- if around_action
36
- controller.send(around_action.name) { run_around_actions(pending_around_actions, &block) }
36
+ if action_hook
37
+ execute_hook(action_hook) { run_around_action_hooks(pending_around_hooks, &block) }
37
38
  else
38
39
  yield
39
40
  end
40
41
  end
41
42
 
42
- def run_before_action_hooks
43
- before_actions = controller_configuration.before_actions_for(action_name)
44
- before_actions.each { |filter| controller.send(filter.name) }
43
+ def execute_hook(action_hook, &block)
44
+ if action_hook.anonymous?
45
+ action_hook.action_proc.call(controller, *block)
46
+ else
47
+ controller.send(action_hook.name, &block)
48
+ end
45
49
  end
46
50
 
47
- def run_after_action_hooks
48
- after_actions = controller_configuration.after_actions_for(action_name)
49
- after_actions.each { |filter| controller.send(filter.name) }
51
+ def run_action_hooks(hook_type)
52
+ action_hooks = controller_configuration.action_hooks_for(hook_type, action_name)
53
+ action_hooks.each { |hook| execute_hook(hook) }
50
54
  end
51
55
  end
52
56
  end
@@ -3,41 +3,43 @@
3
3
  require 'active_support/core_ext/string/inflections'
4
4
  require 'graphql_rails/attribute'
5
5
  require 'graphql_rails/controller/action_configuration'
6
- require 'graphql_rails/controller/action_filter'
6
+ require 'graphql_rails/controller/action_hook'
7
7
 
8
8
  module GraphqlRails
9
9
  class Controller
10
10
  # stores all graphql_rails contoller specific config
11
11
  class Configuration
12
12
  def initialize
13
- @before_actions = {}
14
- @around_actions = {}
15
- @after_actions = {}
13
+ @hooks = {
14
+ before: {},
15
+ after: {},
16
+ around: {}
17
+ }
18
+
16
19
  @action_by_name = {}
17
20
  end
18
21
 
19
- def before_actions_for(action_name)
20
- action_filters_for(action_name, before_actions)
21
- end
22
+ def initialize_copy(other)
23
+ super
22
24
 
23
- def around_actions_for(action_name)
24
- action_filters_for(action_name, around_actions)
25
- end
25
+ @action_by_name = other.instance_variable_get(:@action_by_name).transform_values(&:dup)
26
26
 
27
- def after_actions_for(action_name)
28
- action_filters_for(action_name, after_actions)
27
+ hooks_to_copy = other.instance_variable_get(:@hooks)
28
+ @hooks = hooks_to_copy.each.with_object({}) do |(hook_type, type_hooks), new_hooks|
29
+ new_hooks[hook_type] = type_hooks.transform_values(&:dup)
30
+ end
29
31
  end
30
32
 
31
- def add_around_action(name, **options)
32
- add_action(name, around_actions, **options)
33
+ def action_hooks_for(hook_type, action_name)
34
+ hooks[hook_type].values.select { |hook| hook.applicable_for?(action_name) }
33
35
  end
34
36
 
35
- def add_before_action(name, **options)
36
- add_action(name, before_actions, **options)
37
- end
37
+ def add_action_hook(hook_type, name = nil, **options, &block)
38
+ hook_name = name&.to_sym
39
+ hook_key = hook_name || :"anonymous_#{block.hash}"
38
40
 
39
- def add_after_action(name, **options)
40
- add_action(name, after_actions, **options)
41
+ hooks[hook_type][hook_key] = \
42
+ ActionHook.new(name: hook_name, **options, &block)
41
43
  end
42
44
 
43
45
  def action(method_name)
@@ -46,16 +48,7 @@ module GraphqlRails
46
48
 
47
49
  private
48
50
 
49
- attr_reader :before_actions, :around_actions, :after_actions
50
-
51
- def action_filters_for(action_name, action_filters)
52
- action_filters.values.select { |filter| filter.applicable_for?(action_name) }
53
- end
54
-
55
- def add_action(name, actions_collection, **options)
56
- symbolized_name = name.to_sym
57
- actions_collection[symbolized_name] = ActionFilter.new(symbolized_name, **options)
58
- end
51
+ attr_reader :hooks
59
52
  end
60
53
  end
61
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlRails
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Povilas Jurčys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-21 00:00:00.000000000 Z
11
+ date: 2018-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - po.jurcys@gmail.com
@@ -117,7 +131,7 @@ files:
117
131
  - lib/graphql_rails/controller.rb
118
132
  - lib/graphql_rails/controller/action.rb
119
133
  - lib/graphql_rails/controller/action_configuration.rb
120
- - lib/graphql_rails/controller/action_filter.rb
134
+ - lib/graphql_rails/controller/action_hook.rb
121
135
  - lib/graphql_rails/controller/action_hooks_runner.rb
122
136
  - lib/graphql_rails/controller/configuration.rb
123
137
  - lib/graphql_rails/controller/controller_function.rb