snfoil 0.6 → 0.7.0

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
  SHA256:
3
- metadata.gz: cfab0f49425b4cd1ba02f48f0c3bc114e45fd061118d9673740f2ec05b7655e2
4
- data.tar.gz: 9c66aac6b0edf869f8243db1c447c5e0ef4df1162734c086dfe26fde86427e74
3
+ metadata.gz: 47df13555f0efd4bd9ad506a79f9af2d3955f209bd4fabc0e086f6f5e7b7d953
4
+ data.tar.gz: 74a0a9556cfeed6b434ba40319b7bea0329ddffbcd31f3155aec2b37f0474b71
5
5
  SHA512:
6
- metadata.gz: 239615bc72acaa404521d0771fddaf05a7489cbbf7d9f8b5f1ba8e271c4cb520a2146bdeaf153e933c1c34d83b662a72ebe41f2fd3162825bbd52615fa6b5d8d
7
- data.tar.gz: b4ac4f458812bfce5002e30103f35a30fc44a8509c3ec3cfb8fa86e612d2fa6f48ada5b34713c29e4910ae321477fbc9bfffdc4e47468121424523f33ba8c3db
6
+ metadata.gz: d76ecf6affbc2be179ccd3fef9c28941f4217ff6ad4a0b708605749cf2f89b0108e409fd51cd792fd53915e1f4db00324cce3f9aa430dadd5a6b42315d432b21
7
+ data.tar.gz: 2acfb8773a4f31bce92c81b66eb9024800990a10804cf9e7d9d72a53ea0bc631b4b9f5ed44af5e7218c668d55c634ae43b5d919e91af32015415326f1994bece
data/TODO.md ADDED
@@ -0,0 +1,5 @@
1
+ # TODO
2
+ ## Tests
3
+ ### Contexts
4
+ - Add tests for propagation of :authorize in options aggregate
5
+ - Add tests for Create now using Build
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'active_support/concern'
4
4
  require_relative './setup_context'
5
- require_relative './change_context'
6
5
 
7
6
  module SnFoil
8
7
  module Contexts
@@ -11,33 +10,58 @@ module SnFoil
11
10
 
12
11
  included do
13
12
  include SetupContext
14
- include ChangeContext
15
13
  end
16
14
 
17
15
  class_methods do
16
+ attr_reader :i_setup_build_hooks
17
+
18
18
  def build(params:, entity: nil, **options)
19
19
  new(entity).build(**options, params: params)
20
20
  end
21
+
22
+ def setup_build(method = nil, **options, &block)
23
+ raise ArgumentError, '#setup_build requires either a method name or a block' if method.nil? && block.nil?
24
+
25
+ (@i_setup_build_hooks ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] }
26
+ end
21
27
  end
22
28
 
23
29
  def setup_build_object(params: {}, object: nil, **options)
24
- SnFoil.logger.info 'Warning: Using build bypasses authorize. It is safer to interact with models through create' unless ENV['ISTEST']
25
- return wrap_object(object) if object
30
+ object = if object
31
+ wrap_object(object)
32
+ else
33
+ klass = options.fetch(:model) { model }
34
+ wrap_object(klass).new
35
+ end
26
36
 
27
- klass = options.fetch(:model) { model }
28
- options.merge! object: wrap_object(klass).new(**params)
37
+ object.attributes = params
38
+ options.merge! object: object
29
39
  end
30
40
 
31
41
  def build(**options)
32
42
  options[:action] = :build
33
- options = setup_build(setup_change(**options))
43
+ options = before_setup_build_object(**options)
34
44
  options = setup_build_object(**options)
45
+ authorize(options[:object], options[:authorize], **options) if options[:authorize]
35
46
  unwrap_object(options[:object])
36
47
  end
37
48
 
38
49
  def setup_build(**options)
39
50
  options
40
51
  end
52
+
53
+ def setup_build_hooks
54
+ self.class.i_setup_build_hooks || []
55
+ end
56
+
57
+ private
58
+
59
+ def before_setup_build_object(**options)
60
+ options = setup(**options)
61
+ options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
62
+ options = setup_build(**options)
63
+ setup_build_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
64
+ end
41
65
  end
42
66
  end
43
67
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/concern'
4
- require_relative './setup_context'
5
4
  require_relative './change_context'
6
5
 
7
6
  module SnFoil
@@ -10,8 +9,10 @@ module SnFoil
10
9
  extend ActiveSupport::Concern
11
10
 
12
11
  included do
13
- include SetupContext
12
+ include BuildContext
14
13
  include ChangeContext
14
+
15
+ alias_method :setup_create_object, :setup_build_object
15
16
  end
16
17
 
17
18
  class_methods do
@@ -52,23 +53,12 @@ module SnFoil
52
53
  end
53
54
  end
54
55
 
55
- def setup_create_object(params: {}, object: nil, **options)
56
- object = if object
57
- wrap_object(object)
58
- else
59
- klass = options.fetch(:model) { model }
60
- wrap_object(klass).new
61
- end
62
-
63
- object.attributes = params
64
- options.merge! object: object
65
- end
66
-
67
56
  def create(**options)
68
57
  options[:action] = :create
58
+ options = before_setup_build_object(**options)
69
59
  options = before_setup_create_object(**options)
70
60
  options = setup_create_object(**options)
71
- authorize(options[:object], :create?, **options)
61
+ authorize(options[:object], options.fetch(:authorize) { :create? }, **options)
72
62
  options = create_hooks(**options)
73
63
  options[:object]
74
64
  end
@@ -116,12 +106,10 @@ module SnFoil
116
106
  private
117
107
 
118
108
  def before_setup_create_object(**options)
119
- options = setup_create(**options)
120
- options = setup_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
121
109
  options = setup_change(**options)
122
110
  options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
123
- options = setup(**options)
124
- setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
111
+ options = setup_create(**options)
112
+ setup_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
125
113
  end
126
114
 
127
115
  # This method is private to help protect the order of execution of hooks
@@ -138,31 +126,31 @@ module SnFoil
138
126
  end
139
127
 
140
128
  def before_create_save(**options)
141
- options = before_create(**options)
142
- options = before_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
143
129
  options = before_change(**options)
144
- before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
130
+ options = before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
131
+ options = before_create(**options)
132
+ before_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
145
133
  end
146
134
 
147
135
  def after_create_save(**options)
148
- options = after_create(**options)
149
- options = after_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
150
136
  options = after_change(**options)
151
- after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
137
+ options = after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
138
+ options = after_create(**options)
139
+ after_create_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
152
140
  end
153
141
 
154
142
  def after_create_save_success(**options)
155
- options = after_create_success(**options)
156
- options = after_create_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
157
143
  options = after_change_success(**options)
158
- after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
144
+ options = after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
145
+ options = after_create_success(**options)
146
+ after_create_success_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
159
147
  end
160
148
 
161
149
  def after_create_save_failure(**options)
162
- options = after_create_failure(**options)
163
- options = after_create_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
164
150
  options = after_change_failure(**options)
165
- after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
151
+ options = after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
152
+ options = after_create_failure(**options)
153
+ after_create_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
166
154
  end
167
155
  end
168
156
  end
@@ -62,9 +62,9 @@ module SnFoil
62
62
  options[:action] = :destroy
63
63
  options = before_setup_destroy_object(**options)
64
64
  options = setup_destroy_object(**options)
65
- authorize(options[:object], :destroy?, **options)
65
+ authorize(options[:object], options.fetch(:authorize) { :destroy? }, **options)
66
66
  options = destroy_hooks(**options)
67
- unwrap_object(options[:object])
67
+ options[:object]
68
68
  end
69
69
 
70
70
  def setup_destroy(**options)
@@ -110,12 +110,12 @@ module SnFoil
110
110
  private
111
111
 
112
112
  def before_setup_destroy_object(**options)
113
- options = setup_destroy(**options)
114
- options = setup_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
113
+ options = setup(**options)
114
+ options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
115
115
  options = setup_change(**options)
116
116
  options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
117
- options = setup(**options)
118
- setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
117
+ options = setup_destroy(**options)
118
+ setup_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
119
119
  end
120
120
 
121
121
  # This method is private to help protect the order of execution of hooks
@@ -132,31 +132,31 @@ module SnFoil
132
132
  end
133
133
 
134
134
  def before_destroy_save(options)
135
- options = before_destroy(**options)
136
- options = before_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
137
135
  options = before_change(**options)
138
- before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
136
+ options = before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
137
+ options = before_destroy(**options)
138
+ before_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
139
139
  end
140
140
 
141
141
  def after_destroy_save(options)
142
- options = after_destroy(**options)
143
- options = after_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
144
142
  options = after_change(**options)
145
- after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
143
+ options = after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
144
+ options = after_destroy(**options)
145
+ after_destroy_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
146
146
  end
147
147
 
148
148
  def after_destroy_save_success(options)
149
- options = after_destroy_success(**options)
150
- options = after_destroy_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
151
149
  options = after_change_success(**options)
152
- after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
150
+ options = after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
151
+ options = after_destroy_success(**options)
152
+ after_destroy_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
153
153
  end
154
154
 
155
155
  def after_destroy_save_failure(options)
156
- options = after_destroy_failure(**options)
157
- options = after_destroy_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
158
156
  options = after_change_failure(**options)
159
- after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
157
+ options = after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
158
+ options = after_destroy_failure(**options)
159
+ after_destroy_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
160
160
  end
161
161
  end
162
162
  end
@@ -54,10 +54,10 @@ module SnFoil
54
54
  private
55
55
 
56
56
  def before_setup_index(**options)
57
- options = setup_index(**options)
58
- options = setup_index_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
59
57
  options = setup(**options)
60
- setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
58
+ options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
59
+ options = setup_index(**options)
60
+ setup_index_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
61
61
  end
62
62
  end
63
63
  end
@@ -51,10 +51,10 @@ module SnFoil
51
51
  private
52
52
 
53
53
  def before_setup_show(**options)
54
- options = setup_show(**options)
55
- options = setup_show_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
56
54
  options = setup(**options)
57
- setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
55
+ options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
56
+ options = setup_show(**options)
57
+ setup_show_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
58
58
  end
59
59
  end
60
60
  end
@@ -56,7 +56,8 @@ module SnFoil
56
56
  raise ArgumentError, 'one of the following keywords is required: id, object' unless id || object
57
57
 
58
58
  object = wrap_object(object || scope.resolve.find(id))
59
- authorize(object, :update?, **options)
59
+ authorize(object, options.fetch(:authorize) { :update? }, **options)
60
+
60
61
  object.attributes = params
61
62
  options.merge! object: object
62
63
  end
@@ -65,9 +66,9 @@ module SnFoil
65
66
  options[:action] = :update
66
67
  options = before_setup_update_object(**options)
67
68
  options = setup_update_object(**options)
68
- authorize(options[:object], :update?, **options)
69
+ authorize(options[:object], options.fetch(:authorize) { :update? }, **options)
69
70
  options = update_hooks(**options)
70
- unwrap_object(options[:object])
71
+ options[:object]
71
72
  end
72
73
 
73
74
  def setup_update(**options)
@@ -113,12 +114,12 @@ module SnFoil
113
114
  private
114
115
 
115
116
  def before_setup_update_object(**options)
116
- options = setup_update(**options)
117
- options = setup_update_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
117
+ options = setup(**options)
118
+ options = setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
118
119
  options = setup_change(**options)
119
120
  options = setup_change_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
120
- options = setup(**options)
121
- setup_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
121
+ options = setup_update(**options)
122
+ setup_update_hooks.reduce(options) { |opts, hook| run_hook(hook, opts) }
122
123
  end
123
124
 
124
125
  # This method is private to help protect the order of execution of hooks
@@ -135,31 +136,31 @@ module SnFoil
135
136
  end
136
137
 
137
138
  def before_update_save(options)
138
- options = before_update(**options)
139
- options = before_update_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
140
139
  options = before_change(**options)
141
- before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
140
+ options = before_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
141
+ options = before_update(**options)
142
+ before_update_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
142
143
  end
143
144
 
144
145
  def after_update_save(options)
145
- options = after_update(**options)
146
- options = after_update_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
147
146
  options = after_change(**options)
148
- after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
147
+ options = after_change_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
148
+ options = after_update(**options)
149
+ after_update_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
149
150
  end
150
151
 
151
152
  def after_update_save_success(options)
152
- options = after_update_success(**options)
153
- options = after_update_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
154
153
  options = after_change_success(**options)
155
- after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
154
+ options = after_change_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
155
+ options = after_update_success(**options)
156
+ after_update_success_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
156
157
  end
157
158
 
158
159
  def after_update_save_failure(options)
159
- options = after_update_failure(**options)
160
- options = after_update_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
161
160
  options = after_change_failure(**options)
162
- after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
161
+ options = after_change_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
162
+ options = after_update_failure(**options)
163
+ after_update_failure_hooks.reduce(options) { |opts, hook| run_hook(hook, **opts) }
163
164
  end
164
165
  end
165
166
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SnFoil
4
- VERSION = '0.6'
4
+ VERSION = '0.7.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snfoil
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Howes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-07-22 00:00:00.000000000 Z
12
+ date: 2020-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -162,6 +162,7 @@ files:
162
162
  - CODE_OF_CONDUCT.md
163
163
  - README.md
164
164
  - Rakefile
165
+ - TODO.md
165
166
  - lib/sn_foil.rb
166
167
  - lib/sn_foil/adapters/orms/active_record.rb
167
168
  - lib/sn_foil/adapters/orms/base_adapter.rb