trailblazer-macro 2.1.14 → 2.1.16
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 +4 -4
- data/CHANGES.md +12 -0
- data/Gemfile +3 -2
- data/lib/trailblazer/macro/each.rb +7 -2
- data/lib/trailblazer/macro/model/find.rb +175 -0
- data/lib/trailblazer/macro/rescue.rb +8 -6
- data/lib/trailblazer/macro/strategy.rb +1 -1
- data/lib/trailblazer/macro/version.rb +1 -1
- data/lib/trailblazer/macro.rb +14 -3
- data/test/docs/autogenerated/operation_each_test.rb +2 -0
- data/test/docs/autogenerated/operation_model_test.rb +4 -1
- data/test/docs/each_test.rb +12 -4
- data/test/docs/guard_test.rb +17 -15
- data/test/docs/macro_test.rb +1 -1
- data/test/docs/model/find_test.rb +679 -0
- data/test/docs/model_test.rb +3 -0
- data/test/docs/nested_static_test.rb +3 -1
- data/test/docs/pundit_test.rb +19 -19
- data/test/docs/rescue_test.rb +100 -28
- data/test/docs/wrap_test.rb +45 -27
- data/test/operation/integration_test.rb +5 -7
- data/test/operation/pundit_test.rb +31 -25
- data/test/test_helper.rb +9 -2
- data/trailblazer-macro.gemspec +2 -2
- metadata +15 -6
@@ -0,0 +1,679 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
# step Model::Find(Song, find_method: :find_by, column_key: :id, params_key: :id)
|
4
|
+
# step Model::Find(Song, query: ->(ctx, params:, **) { where(id: params[:id_list]) })
|
5
|
+
# # syntax sugaring
|
6
|
+
# step Model::Find(Song, find_by: :id)
|
7
|
+
# step Model::Find(Song, :find)
|
8
|
+
# step Model::Find(query: ->(ctx, params:, **) { Song.where(id: params[:id_list]) })
|
9
|
+
|
10
|
+
class DocsModelFindTest < Minitest::Spec
|
11
|
+
Song = Struct.new(:id) do
|
12
|
+
def self.find_by(id:)
|
13
|
+
return if id.nil?
|
14
|
+
new(id)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Explicit options
|
20
|
+
#
|
21
|
+
# step Model::Find(Song, find_method: :find_method)
|
22
|
+
#
|
23
|
+
class Unit_ExplicitOptionsTest < Minitest::Spec
|
24
|
+
Song = Class.new(DocsModelFindTest::Song) do
|
25
|
+
def self.find_method(id:)
|
26
|
+
return if id.nil?
|
27
|
+
new(id)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
#:find_method
|
32
|
+
module Song::Activity
|
33
|
+
class Update < Trailblazer::Activity::Railway
|
34
|
+
# explicit style:
|
35
|
+
step Model::Find(Song, find_method: :find_method) # if it's _really_ Song.find_method(...)
|
36
|
+
|
37
|
+
step :validate
|
38
|
+
step :save
|
39
|
+
#~meths
|
40
|
+
include T.def_steps(:validate, :save)
|
41
|
+
#~meths end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
#:find_method end
|
45
|
+
|
46
|
+
#~ctx_to_result
|
47
|
+
it do
|
48
|
+
#:find_method-invoke
|
49
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: "1"}, seq: [])
|
50
|
+
ctx[:model] #=> #<struct Song id=1>
|
51
|
+
#:find_method-invoke end
|
52
|
+
|
53
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id="1">}
|
54
|
+
end
|
55
|
+
|
56
|
+
it do
|
57
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {short_id: nil}, seq: [])
|
58
|
+
assert_equal ctx[:model].inspect, %{nil}
|
59
|
+
end
|
60
|
+
#~ctx_to_result end
|
61
|
+
end
|
62
|
+
|
63
|
+
# NOTE: unit test
|
64
|
+
#
|
65
|
+
# step Model::Find(Song, find_method: :find_by, column_key: :slug, params_key: :params_slug)
|
66
|
+
#
|
67
|
+
class ExplicitColumnKeyAndParamsKeyTest < Minitest::Spec
|
68
|
+
Song = Class.new(DocsModelFindTest::Song) do
|
69
|
+
def self.find_by(slug:)
|
70
|
+
return if slug.nil?
|
71
|
+
new(slug)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
module Song::Activity
|
76
|
+
class Update < Trailblazer::Activity::Railway
|
77
|
+
# explicit style:
|
78
|
+
step Model::Find(Song, find_method: :find_by, column_key: :slug, params_key: :params_slug)
|
79
|
+
step :validate
|
80
|
+
step :save
|
81
|
+
#~meths
|
82
|
+
include T.def_steps(:validate, :save)
|
83
|
+
#~meths end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it do
|
88
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {params_slug: "1"}, seq: [])
|
89
|
+
ctx[:model] #=> #<struct Song id=1>
|
90
|
+
|
91
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id="1">}
|
92
|
+
end
|
93
|
+
|
94
|
+
it "fails" do
|
95
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {params_slug: nil}, seq: [])
|
96
|
+
assert_equal ctx[:model].inspect, %{nil}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
#
|
101
|
+
# step Model::Find(Song, query: ...)
|
102
|
+
#
|
103
|
+
class FindByQueryTest < Minitest::Spec
|
104
|
+
Song = Class.new(DocsModelFindTest::Song) do
|
105
|
+
def self.where(id:, user:)
|
106
|
+
return [] if id.nil?
|
107
|
+
[new([id, user])]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
#:query
|
112
|
+
module Song::Activity
|
113
|
+
class Update < Trailblazer::Activity::Railway
|
114
|
+
step Model::Find(
|
115
|
+
Song,
|
116
|
+
query: ->(ctx, id:, current_user:, **) { where(id: id, user: current_user).first }
|
117
|
+
)
|
118
|
+
step :validate
|
119
|
+
step :save
|
120
|
+
#~meths
|
121
|
+
include T.def_steps(:validate, :save)
|
122
|
+
#~meths end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
#:query end
|
126
|
+
|
127
|
+
it do
|
128
|
+
current_user = Module
|
129
|
+
|
130
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: "1"}, current_user: current_user, seq: [])
|
131
|
+
ctx[:model] #=> #<struct Song id=1>
|
132
|
+
|
133
|
+
assert_equal ctx[:model].inspect, %(#<struct FindByQueryTest::Song id=[\"1\", Module]>)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "fails" do
|
137
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: nil}, seq: [])
|
138
|
+
assert_equal ctx[:model].inspect, %{nil}
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
#
|
143
|
+
# step Model::Find(Song, query: ..., params_key: :slug)
|
144
|
+
#
|
145
|
+
class FindByQueryWithParamsKeyTest < Minitest::Spec
|
146
|
+
Song = Class.new(FindByQueryTest::Song)
|
147
|
+
|
148
|
+
module Song::Activity
|
149
|
+
class Update < Trailblazer::Activity::Railway
|
150
|
+
step Model::Find(Song,
|
151
|
+
query: ->(ctx, id:, current_user:, **) { where(id: id, user: current_user).first },
|
152
|
+
params_key: :slug
|
153
|
+
)
|
154
|
+
step :validate
|
155
|
+
step :save
|
156
|
+
#~meths
|
157
|
+
include T.def_steps(:validate, :save)
|
158
|
+
#~meths end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
it do
|
163
|
+
current_user = Module
|
164
|
+
|
165
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {slug: "1"}, current_user: current_user, seq: [])
|
166
|
+
ctx[:model] #=> #<struct Song id=1>
|
167
|
+
|
168
|
+
assert_equal ctx[:model].inspect, %(#<struct FindByQueryWithParamsKeyTest::Song id=[\"1\", Module]>)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "fails" do
|
172
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {slug: nil}, seq: [])
|
173
|
+
assert_equal ctx[:model].inspect, %{nil}
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
#
|
178
|
+
# step Model::Find(Song, query: ..., ) do ... end
|
179
|
+
#
|
180
|
+
# FIXME: allow Model::Find() do ... end as well as { ... }
|
181
|
+
class FindByQueryWithParamsBlockTest < Minitest::Spec
|
182
|
+
Song = Class.new(FindByQueryTest::Song)
|
183
|
+
|
184
|
+
module Song::Activity
|
185
|
+
class Update < Trailblazer::Activity::Railway
|
186
|
+
step Model::Find(Song, query: ->(ctx, id:, current_user:, **) { where(id: id, user: current_user).first }) { |ctx, params:, **|
|
187
|
+
params[:slug_from_params]
|
188
|
+
}
|
189
|
+
step :validate
|
190
|
+
step :save
|
191
|
+
#~meths
|
192
|
+
include T.def_steps(:validate, :save)
|
193
|
+
#~meths end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
it do
|
198
|
+
current_user = Module
|
199
|
+
|
200
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {slug_from_params: "1"}, current_user: current_user, seq: [])
|
201
|
+
ctx[:model] #=> #<struct Song id=1>
|
202
|
+
|
203
|
+
assert_equal ctx[:model].inspect, %(#<struct FindByQueryWithParamsBlockTest::Song id=[\"1\", Module]>)
|
204
|
+
end
|
205
|
+
|
206
|
+
it "fails" do
|
207
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {slug: nil}, seq: [])
|
208
|
+
assert_equal ctx[:model].inspect, %{nil}
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# Shorthand
|
213
|
+
#
|
214
|
+
# step Model::Find(Song, find_by: :id)
|
215
|
+
#
|
216
|
+
class DocsModelFindByColumnTest < Minitest::Spec
|
217
|
+
Song = Class.new(DocsModelFindTest::Song)
|
218
|
+
|
219
|
+
#:find_by_id
|
220
|
+
module Song::Activity
|
221
|
+
class Update < Trailblazer::Activity::Railway
|
222
|
+
step Model::Find(Song, find_by: :id)
|
223
|
+
step :validate
|
224
|
+
step :save
|
225
|
+
#~meths
|
226
|
+
include T.def_steps(:validate, :save)
|
227
|
+
#~meths end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
#:find_by_id end
|
231
|
+
|
232
|
+
#~ctx_to_result
|
233
|
+
it do
|
234
|
+
#:find_by_id-invoke
|
235
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: "1"}, seq: [])
|
236
|
+
ctx[:model] #=> #<struct Song id=1>
|
237
|
+
#:find_by_id-invoke end
|
238
|
+
|
239
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id="1">}
|
240
|
+
end
|
241
|
+
|
242
|
+
it do
|
243
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {short_id: nil}, seq: [])
|
244
|
+
assert_equal ctx[:model].inspect, %{nil}
|
245
|
+
end
|
246
|
+
#~ctx_to_result end
|
247
|
+
|
248
|
+
# TODO: put this test somewhere else
|
249
|
+
it "doesn't leak anything but {:model} to the outer world" do
|
250
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: "1"}, seq: [])
|
251
|
+
|
252
|
+
assert_equal ctx.keys.inspect, %([:params, :seq, :model])
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
# Shorthand
|
257
|
+
#
|
258
|
+
# step Model::Find(Song, find_by: :short_id)
|
259
|
+
#
|
260
|
+
class DocsModelFindByDifferentColumnTest < Minitest::Spec
|
261
|
+
Song = Struct.new(:short_id) do
|
262
|
+
def self.find_by(short_id:)
|
263
|
+
return if short_id.nil?
|
264
|
+
new(short_id)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
#:find_by_column
|
269
|
+
module Song::Activity
|
270
|
+
class Update < Trailblazer::Activity::Railway
|
271
|
+
puts "yoo"
|
272
|
+
step Model::Find(Song, find_by: :short_id) # Song.find_by(short_id: params[:short_id])
|
273
|
+
step :validate
|
274
|
+
step :save
|
275
|
+
#~meths
|
276
|
+
include T.def_steps(:validate, :save)
|
277
|
+
#~meths end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
#:find_by_column end
|
281
|
+
|
282
|
+
#~ctx_to_result
|
283
|
+
it do
|
284
|
+
#:find_by_column-invoke
|
285
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {short_id: "1f396"}, seq: [])
|
286
|
+
ctx[:model] #=> #<struct Song short_id="1f396">
|
287
|
+
#:find_by_column-invoke end
|
288
|
+
|
289
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} short_id="1f396">}
|
290
|
+
end
|
291
|
+
|
292
|
+
it do
|
293
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {short_id: nil}, seq: [])
|
294
|
+
assert_equal ctx[:model].inspect, %{nil}
|
295
|
+
end
|
296
|
+
#~ctx_to_result end
|
297
|
+
end
|
298
|
+
|
299
|
+
# Shorthand with options
|
300
|
+
#
|
301
|
+
# step Model::Find(Song, find_by: :id, params_key: :slug)
|
302
|
+
#
|
303
|
+
class DocsModelFindByDifferentParamsKeyTest < Minitest::Spec
|
304
|
+
Song = Class.new(DocsModelFindTest::Song)
|
305
|
+
|
306
|
+
#:params_key
|
307
|
+
module Song::Activity
|
308
|
+
class Update < Trailblazer::Activity::Railway
|
309
|
+
step Model::Find(Song, find_by: :id, params_key: :slug) # Song.find_by(id: params[:slug])
|
310
|
+
step :validate
|
311
|
+
step :save
|
312
|
+
#~meths
|
313
|
+
include T.def_steps(:validate, :save)
|
314
|
+
#~meths end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
#:params_key end
|
318
|
+
|
319
|
+
#~ctx_to_result
|
320
|
+
it do
|
321
|
+
#:params_key-invoke
|
322
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {slug: "1f396"}, seq: [])
|
323
|
+
ctx[:model] #=> #<struct Song id=2, id="1f396">
|
324
|
+
#:params_key-invoke end
|
325
|
+
|
326
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id="1f396">}
|
327
|
+
end
|
328
|
+
|
329
|
+
it do
|
330
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {slug: nil}, seq: [])
|
331
|
+
assert_equal ctx[:model].inspect, %{nil}
|
332
|
+
end
|
333
|
+
#~ctx_to_result end
|
334
|
+
end
|
335
|
+
|
336
|
+
# Shorthand with different finder method
|
337
|
+
#
|
338
|
+
# step Model::Find(Song, find_with: :id)
|
339
|
+
#
|
340
|
+
class DocsModelFindWithTest < Minitest::Spec
|
341
|
+
Song = Struct.new(:id) do
|
342
|
+
def self.find_with(id:)
|
343
|
+
return if id.nil?
|
344
|
+
new(id)
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
#:find_with
|
349
|
+
module Song::Activity
|
350
|
+
class Update < Trailblazer::Activity::Railway
|
351
|
+
step Model::Find(Song, find_with: :id)
|
352
|
+
step :validate
|
353
|
+
step :save
|
354
|
+
#~meths
|
355
|
+
include T.def_steps(:validate, :save)
|
356
|
+
#~meths end
|
357
|
+
end
|
358
|
+
end
|
359
|
+
#:find_with end
|
360
|
+
|
361
|
+
#~ctx_to_result
|
362
|
+
it do
|
363
|
+
#:find_with-invoke
|
364
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: 2}, seq: [])
|
365
|
+
ctx[:model] #=> #<struct Song id=2>
|
366
|
+
#:find_with-invoke end
|
367
|
+
|
368
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=2>}
|
369
|
+
end
|
370
|
+
|
371
|
+
it do
|
372
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {id: nil}, seq: [])
|
373
|
+
assert_equal ctx[:model].inspect, %{nil}
|
374
|
+
end
|
375
|
+
#~ctx_to_result end
|
376
|
+
end
|
377
|
+
|
378
|
+
# Shorthand with params_key block
|
379
|
+
#
|
380
|
+
# step Model::Find(Song, find_by: :id) { }
|
381
|
+
#
|
382
|
+
class DocsModelIdFromProcTest < Minitest::Spec
|
383
|
+
Song = Struct.new(:id) do
|
384
|
+
def self.find_by(id:)
|
385
|
+
return if id.nil?
|
386
|
+
new(id)
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
#:id_from
|
391
|
+
module Song::Activity
|
392
|
+
class Update < Trailblazer::Activity::Railway
|
393
|
+
step Model::Find(Song, find_by: :id) { |ctx, params:, **|
|
394
|
+
params[:song] && params[:song][:id]
|
395
|
+
}
|
396
|
+
step :validate
|
397
|
+
step :save
|
398
|
+
#~meths
|
399
|
+
include T.def_steps(:validate, :save)
|
400
|
+
#~meths end
|
401
|
+
end
|
402
|
+
end
|
403
|
+
#:id_from end
|
404
|
+
|
405
|
+
#~ctx_to_result
|
406
|
+
it do
|
407
|
+
#:id_from-invoke
|
408
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {song: {id: "1f396"}}, seq: [])
|
409
|
+
ctx[:model] #=> #<struct Song id="1f396">
|
410
|
+
#:id_from-invoke end
|
411
|
+
|
412
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id="1f396">}
|
413
|
+
assert_equal ctx[:seq].inspect, %([:validate, :save])
|
414
|
+
end
|
415
|
+
|
416
|
+
it do
|
417
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {}, seq: [])
|
418
|
+
|
419
|
+
assert_equal ctx[:model].inspect, %{nil}
|
420
|
+
assert_equal ctx[:seq].inspect, %([])
|
421
|
+
end
|
422
|
+
#~ctx_to_result end
|
423
|
+
end
|
424
|
+
|
425
|
+
# Positional
|
426
|
+
#
|
427
|
+
# step Model::Find(Song, :find)
|
428
|
+
#
|
429
|
+
class DocsModelFindPositionaTest < Minitest::Spec
|
430
|
+
Song = Struct.new(:id) do
|
431
|
+
def self.find(id)
|
432
|
+
return if id.nil?
|
433
|
+
new(id)
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
437
|
+
#:find
|
438
|
+
module Song::Activity
|
439
|
+
class Update < Trailblazer::Activity::Railway
|
440
|
+
step Model::Find(Song, :find) # Song.find(id)
|
441
|
+
step :validate
|
442
|
+
step :save
|
443
|
+
#~meths
|
444
|
+
include T.def_steps(:validate, :save)
|
445
|
+
#~meths end
|
446
|
+
end
|
447
|
+
end
|
448
|
+
#:find end
|
449
|
+
|
450
|
+
#~ctx_to_result
|
451
|
+
it do
|
452
|
+
#:find-ok
|
453
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, {params: {id: 1}, seq: []})
|
454
|
+
ctx[:model] #=> #<struct Song id=1, title="Roxanne">
|
455
|
+
#:find-ok end
|
456
|
+
|
457
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=1>}
|
458
|
+
end
|
459
|
+
#~ctx_to_result end
|
460
|
+
end
|
461
|
+
|
462
|
+
# Positional with params_key block
|
463
|
+
#
|
464
|
+
# step Model::Find(Song, :find) { ... }
|
465
|
+
#
|
466
|
+
class DocsModelFindPositionalWithParamsBlockTest < Minitest::Spec
|
467
|
+
Song = Class.new(DocsModelFindPositionaTest::Song)
|
468
|
+
|
469
|
+
module Song::Activity
|
470
|
+
class Update < Trailblazer::Activity::Railway
|
471
|
+
step Model::Find(Song, :find) { |ctx, params:, **| params[:params_slug] }
|
472
|
+
step :validate
|
473
|
+
step :save
|
474
|
+
#~meths
|
475
|
+
include T.def_steps(:validate, :save)
|
476
|
+
#~meths end
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
#~ctx_to_result
|
481
|
+
it do
|
482
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, {params: {params_slug: 1}, seq: []})
|
483
|
+
ctx[:model] #=> #<struct Song id=1, title="Roxanne">
|
484
|
+
|
485
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=1>}
|
486
|
+
end
|
487
|
+
#~ctx_to_result end
|
488
|
+
|
489
|
+
it "fails" do
|
490
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {params_slug: nil}, seq: [])
|
491
|
+
assert_equal ctx[:model].inspect, %{nil}
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
# Positional with #[]
|
496
|
+
#
|
497
|
+
# step Model::Find(Song, :[])
|
498
|
+
#
|
499
|
+
class DocsModelAccessorTest < Minitest::Spec
|
500
|
+
Song = Struct.new(:id, :title) do
|
501
|
+
def self.[](id)
|
502
|
+
id.nil? ? nil : new(id+99)
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
#:show
|
507
|
+
module Song::Activity
|
508
|
+
class Update < Trailblazer::Activity::Railway
|
509
|
+
step Model::Find(Song, :[])
|
510
|
+
step :validate
|
511
|
+
step :save
|
512
|
+
#~meths
|
513
|
+
include T.def_steps(:validate, :save)
|
514
|
+
#~meths end
|
515
|
+
end
|
516
|
+
end
|
517
|
+
#:show end
|
518
|
+
|
519
|
+
#~ctx_to_result
|
520
|
+
it do
|
521
|
+
#:show-ok
|
522
|
+
signal, (ctx, _) = Trailblazer::Developer.wtf?(Song::Activity::Update, [{params: {id: 1}, seq: []}])
|
523
|
+
ctx[:model] #=> #<struct Song id=1, title="Roxanne">
|
524
|
+
#:show-ok end
|
525
|
+
|
526
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=100, title=nil>}
|
527
|
+
end
|
528
|
+
#~ctx_to_result end
|
529
|
+
end
|
530
|
+
|
531
|
+
# class DocsModelBlockTest < Minitest::Spec
|
532
|
+
# Song = Class.new(DocsModelIdFromProcTest::Song)
|
533
|
+
|
534
|
+
# #:block
|
535
|
+
# module Song::Activity
|
536
|
+
# class Update < Trailblazer::Activity::Railway
|
537
|
+
# step Model() do |ctx, params:, **|
|
538
|
+
|
539
|
+
# end
|
540
|
+
# step :validate
|
541
|
+
# step :save
|
542
|
+
# #~meths
|
543
|
+
# include T.def_steps(:validate, :save)
|
544
|
+
# #~meths end
|
545
|
+
# end
|
546
|
+
# end
|
547
|
+
# #:block end
|
548
|
+
|
549
|
+
# #~ctx_to_result
|
550
|
+
# it do
|
551
|
+
# #:block-invoke
|
552
|
+
# signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {song: {id: "1f396"}}, seq: [])
|
553
|
+
# ctx[:model] #=> #<struct Song id="1f396">
|
554
|
+
# #:block-invoke end
|
555
|
+
|
556
|
+
# assert_equal ctx[:model].inspect, %{#<struct #{Song} id="1f396">}
|
557
|
+
# assert_equal ctx[:seq].inspect, %([:validate, :save])
|
558
|
+
# end
|
559
|
+
|
560
|
+
# it do
|
561
|
+
# signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {}, seq: [])
|
562
|
+
|
563
|
+
# assert_equal ctx[:model].inspect, %{nil}
|
564
|
+
# assert_equal ctx[:seq].inspect, %([])
|
565
|
+
# end
|
566
|
+
# #~ctx_to_result end
|
567
|
+
# end
|
568
|
+
|
569
|
+
# new
|
570
|
+
#
|
571
|
+
#
|
572
|
+
#
|
573
|
+
class DocsModelNewTest < Minitest::Spec
|
574
|
+
Song = Class.new(DocsModelFindTest::Song)
|
575
|
+
|
576
|
+
#:new
|
577
|
+
module Song::Activity
|
578
|
+
class Create < Trailblazer::Activity::Railway
|
579
|
+
step Model::Build(Song, :new)
|
580
|
+
step :validate
|
581
|
+
step :save
|
582
|
+
#~meths
|
583
|
+
include T.def_steps(:validate, :save)
|
584
|
+
#~meths end
|
585
|
+
end
|
586
|
+
end
|
587
|
+
#:new end
|
588
|
+
|
589
|
+
#~ctx_to_result
|
590
|
+
it do
|
591
|
+
#:new-invoke
|
592
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, seq: [])
|
593
|
+
ctx[:model] #=> #<struct Song id=1>
|
594
|
+
#:new-invoke end
|
595
|
+
|
596
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=nil>}
|
597
|
+
end
|
598
|
+
#~ctx_to_result end
|
599
|
+
end
|
600
|
+
|
601
|
+
# build
|
602
|
+
#
|
603
|
+
#
|
604
|
+
#
|
605
|
+
class DocsModelBuildTest < Minitest::Spec
|
606
|
+
Song = Struct.new(:id)
|
607
|
+
Song.singleton_class.alias_method :build, :new
|
608
|
+
|
609
|
+
#:build
|
610
|
+
module Song::Activity
|
611
|
+
class Create < Trailblazer::Activity::Railway
|
612
|
+
step Model::Build(Song, :build)
|
613
|
+
step :validate
|
614
|
+
step :save
|
615
|
+
#~meths
|
616
|
+
include T.def_steps(:validate, :save)
|
617
|
+
#~meths end
|
618
|
+
end
|
619
|
+
end
|
620
|
+
#:build end
|
621
|
+
|
622
|
+
#~ctx_to_result
|
623
|
+
it do
|
624
|
+
#:build-invoke
|
625
|
+
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, seq: [])
|
626
|
+
ctx[:model] #=> #<struct Song id=1>
|
627
|
+
#:build-invoke end
|
628
|
+
|
629
|
+
assert_equal ctx[:model].inspect, %{#<struct #{Song} id=nil>}
|
630
|
+
end
|
631
|
+
#~ctx_to_result end
|
632
|
+
end
|
633
|
+
|
634
|
+
# Explicit terminus
|
635
|
+
#
|
636
|
+
# step Model::Find(Song, find_by: :id, not_found_terminus: true)
|
637
|
+
#
|
638
|
+
class ModelFind404TerminusTest < Minitest::Spec
|
639
|
+
Song = Struct.new(:id) do
|
640
|
+
def self.find_by(id:)
|
641
|
+
return if id.nil?
|
642
|
+
return if id == 2
|
643
|
+
new(id)
|
644
|
+
end
|
645
|
+
end
|
646
|
+
|
647
|
+
#:not-found
|
648
|
+
class Song
|
649
|
+
module Activity
|
650
|
+
class Update < Trailblazer::Activity::Railway
|
651
|
+
step Model::Find(Song, find_by: :id, not_found_terminus: true)
|
652
|
+
step :validate
|
653
|
+
step :save
|
654
|
+
#~meths
|
655
|
+
include T.def_steps(:validate, :save)
|
656
|
+
#~meths end
|
657
|
+
end
|
658
|
+
end
|
659
|
+
end
|
660
|
+
#:not-found end
|
661
|
+
|
662
|
+
it "terminates on {not_found} for missing ID in {params}" do
|
663
|
+
assert_invoke Song::Activity::Update, params: {id: 1},
|
664
|
+
seq: "[:validate, :save]", expected_ctx_variables: {model: Song.find_by(id: 1)}
|
665
|
+
assert_invoke Song::Activity::Update, params: {id: nil}, terminus: :not_found
|
666
|
+
|
667
|
+
# no {params} at all.
|
668
|
+
assert_invoke Song::Activity::Update, terminus: :not_found
|
669
|
+
|
670
|
+
# no model matching ID.
|
671
|
+
# NOTE: we assign {model: nil} - do we want that?
|
672
|
+
assert_invoke Song::Activity::Update, params: {id: 2}, terminus: :not_found, expected_ctx_variables: {model: nil}
|
673
|
+
|
674
|
+
#:not-found-invoke
|
675
|
+
signal, (ctx, _) = Trailblazer::Activity::TaskWrap.invoke(Song::Activity::Update, [{params: {id: nil}},{}])
|
676
|
+
puts signal #=> #<Trailblazer::Activity::End semantic=:not_found>
|
677
|
+
#:not-found-invoke end
|
678
|
+
end
|
679
|
+
end
|
data/test/docs/model_test.rb
CHANGED
@@ -112,6 +112,7 @@ class DocsModelFindByTitleTest < Minitest::Spec
|
|
112
112
|
it do
|
113
113
|
#:key-title-fail
|
114
114
|
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Update, params: {title: nil}, seq: [])
|
115
|
+
|
115
116
|
assert_equal ctx[:model].inspect, %{nil}
|
116
117
|
#:key-title-fail end
|
117
118
|
end
|
@@ -203,6 +204,7 @@ class DocsModelEmptyDITest < Minitest::Spec
|
|
203
204
|
|
204
205
|
it do
|
205
206
|
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, :"model.class" => Hit, seq: [])
|
207
|
+
|
206
208
|
assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=nil, title=nil>}
|
207
209
|
end
|
208
210
|
end
|
@@ -223,6 +225,7 @@ class DocsModelIOTest < Minitest::Spec
|
|
223
225
|
#:in end
|
224
226
|
|
225
227
|
signal, (ctx, _) = Trailblazer::Activity.(Song::Activity::Create, params: {}, my_id: 1, :"model.class" => Hit)
|
228
|
+
|
226
229
|
assert_equal ctx[:model].inspect, %{#<struct #{Hit} id=1, title=nil>}
|
227
230
|
=begin
|
228
231
|
#:in-call
|
@@ -370,7 +370,7 @@ class DocsNestedDynamicTest < Minitest::Spec
|
|
370
370
|
end
|
371
371
|
end
|
372
372
|
#:dynamic-output end
|
373
|
-
|
373
|
+
end
|
374
374
|
end # B
|
375
375
|
|
376
376
|
assert_equal exception.message[0..34], %{No `unsupported_file_format` output}
|
@@ -663,9 +663,11 @@ Song::Activity::Create
|
|
663
663
|
Trailblazer::Developer.wtf?(Song::Activity::Create, [{params: {type: "vorbis"}, seq: []}])
|
664
664
|
|
665
665
|
output, _ = trace Song::Activity::Create, params: {type: "vorbis"}, seq: []
|
666
|
+
|
666
667
|
assert_equal output, trace.split("\n")[2..-2].join("\n").sub("Song::Activity::Create", "TOP")
|
667
668
|
|
668
669
|
output, _ = trace Song::Activity::Create, params: {type: "mp3"}, seq: []
|
670
|
+
|
669
671
|
assert_equal output, %{TOP
|
670
672
|
|-- Start.default
|
671
673
|
|-- model
|