trailblazer-macro 2.1.0.rc1 → 2.1.0.rc11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +4 -14
- data/.rubocop_todo.yml +109 -241
- data/.travis.yml +10 -14
- data/CHANGES.md +5 -0
- data/Gemfile +3 -1
- data/Rakefile +1 -8
- data/lib/trailblazer-macro.rb +1 -1
- data/lib/trailblazer/macro.rb +27 -9
- data/lib/trailblazer/{operation → macro}/guard.rb +4 -4
- data/lib/trailblazer/{operation → macro}/model.rb +8 -10
- data/lib/trailblazer/macro/nested.rb +76 -0
- data/lib/trailblazer/{operation → macro}/policy.rb +9 -11
- data/lib/trailblazer/{operation → macro}/pundit.rb +5 -5
- data/lib/trailblazer/macro/rescue.rb +44 -0
- data/lib/trailblazer/macro/version.rb +4 -2
- data/lib/trailblazer/macro/wrap.rb +80 -0
- data/test/docs/guard_test.rb +4 -1
- data/test/docs/macro_test.rb +2 -2
- data/test/docs/nested_test.rb +78 -82
- data/test/docs/rescue_test.rb +10 -7
- data/test/docs/wrap_test.rb +14 -6
- data/test/operation/integration_test.rb +55 -0
- data/test/operation/model_test.rb +15 -5
- data/test/operation/pundit_test.rb +0 -1
- data/test/test_helper.rb +14 -4
- data/trailblazer-macro.gemspec +6 -3
- metadata +40 -28
- data/lib/trailblazer/operation/nested.rb +0 -98
- data/lib/trailblazer/operation/rescue.rb +0 -42
- data/lib/trailblazer/operation/wrap.rb +0 -83
- data/test/lib/methods.rb +0 -25
- data/test/operation/nested_test.rb +0 -293
@@ -1,293 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
# exec_context nach Nested
|
4
|
-
|
5
|
-
class NestedTest < Minitest::Spec
|
6
|
-
#---
|
7
|
-
#- shared data
|
8
|
-
class B < Trailblazer::Operation
|
9
|
-
pass ->(options, **) { options["can.B.see.A.mutable.data?"] = options["mutable.data.from.A"] }
|
10
|
-
pass ->(options, **) { options["can.B.see.current_user?"] = options["current_user"] }
|
11
|
-
pass ->(options, **) { options["can.B.see.params?"] = options["params"] }
|
12
|
-
pass ->(options, **) { options["can.B.see.A.class.data?"] = options["A.class.data"] }
|
13
|
-
pass ->(options, **) { options["can.B.see.container.data?"] = options["some.container.data"] }
|
14
|
-
pass ->(options, **) { options["mutable.data.from.B"] = "from B!" }
|
15
|
-
end
|
16
|
-
|
17
|
-
class A < Trailblazer::Operation
|
18
|
-
extend ClassDependencies
|
19
|
-
self["A.class.data"] = "yes" # class data on A
|
20
|
-
|
21
|
-
pass ->(options, **) { options["mutable.data.from.A"] = "from A!" } # mutable data on A
|
22
|
-
step Nested( B )
|
23
|
-
pass ->(options, **) { options["can.A.see.B.mutable.data?"] = options["mutable.data.from.B"] }
|
24
|
-
end
|
25
|
-
|
26
|
-
#---
|
27
|
-
#- default behavior: share everything.
|
28
|
-
# no containers
|
29
|
-
# no runtime data
|
30
|
-
# no params
|
31
|
-
it do
|
32
|
-
result = A.("params" => {})
|
33
|
-
# everything from A visible
|
34
|
-
result["A.class.data"]. must_equal "yes"
|
35
|
-
result["mutable.data.from.A"].must_equal "from A!"
|
36
|
-
|
37
|
-
# B can see everything
|
38
|
-
result["can.B.see.A.mutable.data?"].must_equal "from A!"
|
39
|
-
result["can.B.see.current_user?"].must_be_nil
|
40
|
-
result["can.B.see.params?"].must_equal({})
|
41
|
-
result["can.B.see.A.class.data?"].must_equal "yes"
|
42
|
-
result["can.B.see.container.data?"].must_be_nil
|
43
|
-
|
44
|
-
result["can.A.see.B.mutable.data?"].must_equal "from B!"
|
45
|
-
end
|
46
|
-
|
47
|
-
#---
|
48
|
-
#- Nested::NonActivity
|
49
|
-
class AlmostB < Trailblazer::Operation
|
50
|
-
step ->(options, is_successful:raise, **) { is_successful } # {AlmostB} fails if {is_successful} isn't true.
|
51
|
-
step ->(options, **) { options["can.B.see.A.mutable.data?"] = options["mutable.data.from.A"] }
|
52
|
-
pass ->(options, **) { options["mutable.data.from.B"] = "from AlmostB!" }
|
53
|
-
end
|
54
|
-
|
55
|
-
#- Nested( ->{} )
|
56
|
-
class SomeNestedWithProc < Trailblazer::Operation
|
57
|
-
extend ClassDependencies
|
58
|
-
self["A.class.data"] = "yes" # class data on A
|
59
|
-
|
60
|
-
Decider = ->(options, use_class:raise, **) { use_class }
|
61
|
-
|
62
|
-
pass ->(options, **) { options["mutable.data.from.A"] = "from A!" } # mutable data on A
|
63
|
-
step Nested( Decider )
|
64
|
-
pass ->(options, **) { options["can.A.see.B.mutable.data?"] = options["mutable.data.from.B"] }
|
65
|
-
end
|
66
|
-
|
67
|
-
#- Nested( Callable )
|
68
|
-
class SomeNestedWithCallable < Trailblazer::Operation
|
69
|
-
extend ClassDependencies
|
70
|
-
self["A.class.data"] = "yes" # class data on A
|
71
|
-
|
72
|
-
class Decider
|
73
|
-
def self.call(options, use_class:raise, **)
|
74
|
-
use_class
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
pass ->(options, **) { options["mutable.data.from.A"] = "from A!" } # mutable data on A
|
79
|
-
step Nested( Decider )
|
80
|
-
pass ->(options, **) { options["can.A.see.B.mutable.data?"] = options["mutable.data.from.B"] }
|
81
|
-
end
|
82
|
-
|
83
|
-
#- Nested( :method )
|
84
|
-
class SomeNestedWithMethod < Trailblazer::Operation
|
85
|
-
extend ClassDependencies
|
86
|
-
self["A.class.data"] = "yes" # class data on A
|
87
|
-
|
88
|
-
def decider(options, use_class:raise, **)
|
89
|
-
use_class
|
90
|
-
end
|
91
|
-
|
92
|
-
pass ->(options, **) { options["mutable.data.from.A"] = "from A!" } # mutable data on A
|
93
|
-
step Nested( :decider )
|
94
|
-
pass ->(options, **) { options["can.A.see.B.mutable.data?"] = options["mutable.data.from.B"] }
|
95
|
-
end
|
96
|
-
|
97
|
-
|
98
|
-
#- test callable
|
99
|
-
# B with Callable, successful
|
100
|
-
it do
|
101
|
-
result = SomeNestedWithCallable.("params" => {}, use_class: B)
|
102
|
-
assert_b(result, is_successful: "whatever")
|
103
|
-
end
|
104
|
-
|
105
|
-
# AlmostB with Callable, successful
|
106
|
-
it do
|
107
|
-
result = SomeNestedWithCallable.("params" => {}, use_class: AlmostB, is_successful: true)
|
108
|
-
assert_almost_b(result, is_successful: true)
|
109
|
-
end
|
110
|
-
|
111
|
-
# AlmostB with Callable, failure
|
112
|
-
it do
|
113
|
-
result = SomeNestedWithCallable.("params" => {}, use_class: AlmostB, is_successful: false)
|
114
|
-
assert_almost_b(result, is_successful: false)
|
115
|
-
end
|
116
|
-
|
117
|
-
#- test proc
|
118
|
-
# B with proc, successful
|
119
|
-
it do
|
120
|
-
result = SomeNestedWithProc.("params" => {}, use_class: B)
|
121
|
-
assert_b(result, is_successful: "whatever")
|
122
|
-
end
|
123
|
-
|
124
|
-
# AlmostB with proc, successful
|
125
|
-
it do
|
126
|
-
result = SomeNestedWithProc.("params" => {}, use_class: AlmostB, is_successful: true)
|
127
|
-
|
128
|
-
assert_almost_b(result, is_successful: true)
|
129
|
-
end
|
130
|
-
|
131
|
-
# AlmostB with proc, failure.
|
132
|
-
it do
|
133
|
-
result = SomeNestedWithProc.("params" => {}, use_class: AlmostB, is_successful: false)
|
134
|
-
|
135
|
-
assert_almost_b(result, is_successful: false)
|
136
|
-
end
|
137
|
-
|
138
|
-
#- test :method
|
139
|
-
# B with method, successful
|
140
|
-
it do
|
141
|
-
result = SomeNestedWithMethod.("params" => {}, use_class: B)
|
142
|
-
assert_b(result, is_successful: "whatever")
|
143
|
-
end
|
144
|
-
|
145
|
-
# AlmostB with method, successful
|
146
|
-
it do
|
147
|
-
result = SomeNestedWithMethod.("params" => {}, use_class: AlmostB, is_successful: true)
|
148
|
-
|
149
|
-
assert_almost_b(result, is_successful: true)
|
150
|
-
end
|
151
|
-
|
152
|
-
# AlmostB with method, failure.
|
153
|
-
it do
|
154
|
-
result = SomeNestedWithMethod.("params" => {}, use_class: AlmostB, is_successful: false)
|
155
|
-
|
156
|
-
assert_almost_b(result, is_successful: false)
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
def assert_almost_b(result, is_successful:raise)
|
161
|
-
result.success?.must_equal is_successful # AlmostB was successful, so A is successful.
|
162
|
-
|
163
|
-
# everything from A visible
|
164
|
-
result["A.class.data"]. must_equal "yes"
|
165
|
-
result["mutable.data.from.A"].must_equal "from A!"
|
166
|
-
|
167
|
-
# AlmostB doesn't look for everything
|
168
|
-
result["can.B.see.current_user?"].must_be_nil
|
169
|
-
result["can.B.see.params?"].must_be_nil
|
170
|
-
if is_successful
|
171
|
-
result["can.B.see.A.mutable.data?"].must_equal "from A!"
|
172
|
-
result["can.B.see.A.class.data?"].must_be_nil # we don't look for it.
|
173
|
-
result["can.A.see.B.mutable.data?"].must_equal "from AlmostB!"
|
174
|
-
else
|
175
|
-
result["can.B.see.A.mutable.data?"].must_be_nil
|
176
|
-
result["can.B.see.A.class.data?"].must_be_nil
|
177
|
-
result["can.A.see.B.mutable.data?"].must_be_nil
|
178
|
-
end
|
179
|
-
result["can.B.see.container.data?"].must_be_nil
|
180
|
-
|
181
|
-
|
182
|
-
# result[:is_successful].must_equal is_successful # FIXME: this is wrong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! key is symbol
|
183
|
-
end
|
184
|
-
|
185
|
-
def assert_b(result, is_successful:raise)
|
186
|
-
# everything from A visible
|
187
|
-
result["A.class.data"]. must_equal "yes"
|
188
|
-
result["mutable.data.from.A"].must_equal "from A!"
|
189
|
-
|
190
|
-
# B can see everything
|
191
|
-
result["can.B.see.A.mutable.data?"].must_equal "from A!"
|
192
|
-
result["can.B.see.current_user?"].must_be_nil
|
193
|
-
result["can.B.see.params?"].must_equal({})
|
194
|
-
result["can.B.see.A.class.data?"].must_equal "yes"
|
195
|
-
result["can.B.see.container.data?"].must_be_nil
|
196
|
-
|
197
|
-
result["can.A.see.B.mutable.data?"].must_equal "from B!"
|
198
|
-
|
199
|
-
result[:is_successful].must_be_nil
|
200
|
-
result.success?.must_equal true # B was successful, so A is successful.
|
201
|
-
end
|
202
|
-
|
203
|
-
|
204
|
-
#---
|
205
|
-
#- :exec_context
|
206
|
-
class Create < Trailblazer::Operation
|
207
|
-
class Edit < Trailblazer::Operation
|
208
|
-
step :c!
|
209
|
-
|
210
|
-
def c!(options, **); options[:c] = 1 end
|
211
|
-
end
|
212
|
-
|
213
|
-
step :a!
|
214
|
-
step Nested( Edit )
|
215
|
-
step :b!
|
216
|
-
|
217
|
-
def a!(options, **); options[:a] = 2 end
|
218
|
-
def b!(options, **); options[:b] = 3 end
|
219
|
-
end
|
220
|
-
|
221
|
-
it { Create.().inspect(:a, :b, :c).must_equal %{<Result:true [2, 3, 1] >} }
|
222
|
-
end
|
223
|
-
|
224
|
-
class NestedWithFastTrackTest < Minitest::Spec
|
225
|
-
module Steps
|
226
|
-
def b(options, a:, **)
|
227
|
-
options["b"] = a+1
|
228
|
-
end
|
229
|
-
|
230
|
-
def f(options, **)
|
231
|
-
options["f"] = 3
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
class Edit < Trailblazer::Operation
|
236
|
-
pass :a, pass_fast: true
|
237
|
-
|
238
|
-
def a(options, **)
|
239
|
-
options["a"] = 1
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
class Update < Trailblazer::Operation
|
244
|
-
step Nested( Edit )
|
245
|
-
step :b
|
246
|
-
fail :f
|
247
|
-
|
248
|
-
include Steps
|
249
|
-
end
|
250
|
-
|
251
|
-
# from Nested straight to End.pass_fast.
|
252
|
-
it { Update.({}).inspect("a", "b", "f").must_equal %{<Result:true [1, nil, nil] >} }
|
253
|
-
|
254
|
-
#- Nested, pass_fast: true
|
255
|
-
class Upsert < Trailblazer::Operation
|
256
|
-
step Nested( Edit ), pass_fast: true # this option is unnecessary.
|
257
|
-
step :b
|
258
|
-
fail :f
|
259
|
-
|
260
|
-
include Steps
|
261
|
-
end
|
262
|
-
|
263
|
-
# from Nested straight to End.pass_fast.
|
264
|
-
it { Upsert.({}).inspect("a", "b", "f").must_equal %{<Result:true [1, nil, nil] >} }
|
265
|
-
|
266
|
-
#- mapping
|
267
|
-
#- Nested, :pass_fast => :failure
|
268
|
-
it "attaches :pass_fast => :failure" do
|
269
|
-
op = Class.new(Trailblazer::Operation) do
|
270
|
-
step Nested( Edit ), Output(:pass_fast) => Track(:failure)
|
271
|
-
step :b
|
272
|
-
fail :f
|
273
|
-
|
274
|
-
include Steps
|
275
|
-
end
|
276
|
-
|
277
|
-
# from Nested to :failure track.
|
278
|
-
op.({}).inspect("a", "b", "c", "f").must_equal %{<Result:false [1, nil, nil, 3] >}
|
279
|
-
end
|
280
|
-
|
281
|
-
it "goes straigt to End.failure" do
|
282
|
-
op = Class.new(Trailblazer::Operation) do
|
283
|
-
step Nested( Edit ), Output(:pass_fast) => "End.failure"
|
284
|
-
step :b
|
285
|
-
fail :f
|
286
|
-
|
287
|
-
include Steps
|
288
|
-
end
|
289
|
-
|
290
|
-
# from Nested straight to End.failure, no fail step will be visited.
|
291
|
-
op.({}).inspect("a", "b", "c", "f").must_equal %{<Result:false [1, nil, nil, nil] >}
|
292
|
-
end
|
293
|
-
end
|