trailblazer 2.0.7 → 2.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +35 -1
- data/Gemfile +6 -12
- data/README.md +3 -1
- data/Rakefile +6 -17
- data/lib/trailblazer.rb +7 -4
- data/lib/trailblazer/deprecation/call.rb +46 -0
- data/lib/trailblazer/deprecation/context.rb +43 -0
- data/lib/trailblazer/operation/contract.rb +40 -9
- data/lib/trailblazer/operation/deprecations.rb +21 -0
- data/lib/trailblazer/operation/guard.rb +5 -5
- data/lib/trailblazer/operation/model.rb +15 -10
- data/lib/trailblazer/operation/nested.rb +56 -85
- data/lib/trailblazer/operation/persist.rb +4 -2
- data/lib/trailblazer/operation/policy.rb +16 -7
- data/lib/trailblazer/operation/pundit.rb +3 -3
- data/lib/trailblazer/operation/representer.rb +5 -0
- data/lib/trailblazer/operation/rescue.rb +12 -9
- data/lib/trailblazer/operation/validate.rb +36 -29
- data/lib/trailblazer/operation/wrap.rb +49 -11
- data/lib/trailblazer/task.rb +20 -0
- data/lib/trailblazer/version.rb +1 -1
- data/test/benchmark.rb +63 -0
- data/test/deprecation/call_test.rb +42 -0
- data/test/deprecation/context_test.rb +19 -0
- data/test/docs/contract_test.rb +73 -53
- data/test/docs/dry_test.rb +2 -2
- data/test/docs/fast_test.rb +133 -13
- data/test/docs/guard_test.rb +28 -35
- data/test/docs/macro_test.rb +1 -1
- data/test/docs/model_test.rb +13 -13
- data/test/docs/nested_test.rb +54 -122
- data/test/docs/operation_test.rb +42 -43
- data/test/docs/pundit_test.rb +16 -16
- data/test/docs/representer_test.rb +18 -18
- data/test/docs/rescue_test.rb +29 -29
- data/test/docs/trace_test.rb +82 -0
- data/test/docs/wrap_test.rb +59 -26
- data/test/module_test.rb +75 -75
- data/test/nested_test.rb +293 -0
- data/test/operation/contract_test.rb +23 -153
- data/test/operation/dsl/contract_test.rb +9 -9
- data/test/operation/dsl/representer_test.rb +169 -169
- data/test/operation/model_test.rb +15 -21
- data/test/operation/persist_test.rb +18 -11
- data/test/operation/pundit_test.rb +25 -23
- data/test/operation/representer_test.rb +254 -254
- data/test/test_helper.rb +5 -2
- data/test/variables_test.rb +158 -0
- data/trailblazer.gemspec +1 -1
- data/untitled +33 -0
- metadata +25 -27
- data/lib/trailblazer/operation/callback.rb +0 -35
- data/lib/trailblazer/operation/procedural/contract.rb +0 -15
- data/lib/trailblazer/operation/procedural/validate.rb +0 -22
- data/test/operation/callback_test.rb +0 -70
- data/test/operation/dsl/callback_test.rb +0 -106
- data/test/operation/params_test.rb +0 -36
- data/test/operation/pipedream_test.rb +0 -59
- data/test/operation/pipetree_test.rb +0 -104
- data/test/operation/present_test.rb +0 -24
- data/test/operation/resolver_test.rb +0 -47
- data/test/operation_test.rb +0 -143
data/test/docs/guard_test.rb
CHANGED
@@ -3,46 +3,33 @@ require "test_helper"
|
|
3
3
|
#--
|
4
4
|
# with proc
|
5
5
|
class DocsGuardProcTest < Minitest::Spec
|
6
|
-
# test without KWs, only options.
|
7
|
-
class Update < Trailblazer::Operation
|
8
|
-
step Policy::Guard( ->(options) { options["params"][:pass] } )
|
9
|
-
step ->(options, *) { options["x"] = true }
|
10
|
-
end
|
11
|
-
|
12
|
-
it { Update.(pass: false)["x"].must_equal nil }
|
13
|
-
it { Update.(pass: true)["x"].must_equal true }
|
14
|
-
# TODO: test excp when current_user not available
|
15
|
-
|
16
6
|
#:proc
|
17
7
|
class Create < Trailblazer::Operation
|
18
|
-
step Policy::Guard( ->(options,
|
8
|
+
step Policy::Guard( ->(options, pass:, **) { pass } )
|
19
9
|
#~pipeonly
|
20
10
|
step :process
|
21
11
|
|
22
|
-
def process(
|
23
|
-
|
12
|
+
def process(options, **)
|
13
|
+
options["x"] = true
|
24
14
|
end
|
25
15
|
#~pipeonly end
|
26
16
|
end
|
27
17
|
#:proc end
|
28
18
|
|
29
|
-
it { Create.(pass: false)["x"].
|
19
|
+
it { Create.(pass: false)["x"].must_be_nil }
|
30
20
|
it { Create.(pass: true)["x"].must_equal true }
|
31
21
|
|
32
22
|
#- result object, guard
|
33
23
|
it { Create.(pass: true)["result.policy.default"].success?.must_equal true }
|
34
24
|
it { Create.(pass: false)["result.policy.default"].success?.must_equal false }
|
35
25
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
#---
|
26
|
+
#---
|
40
27
|
#- Guard inheritance
|
41
28
|
class New < Create
|
42
29
|
step Policy::Guard( ->(options, current_user:, **) { current_user } ), override: true
|
43
30
|
end
|
44
31
|
|
45
|
-
it { New
|
32
|
+
it { Trailblazer::Operation::Inspect.(New).must_equal %{[>policy.default.eval,>process]} }
|
46
33
|
end
|
47
34
|
|
48
35
|
#---
|
@@ -52,8 +39,8 @@ class DocsGuardTest < Minitest::Spec
|
|
52
39
|
class MyGuard
|
53
40
|
include Uber::Callable
|
54
41
|
|
55
|
-
def call(options,
|
56
|
-
|
42
|
+
def call(options, pass:, **)
|
43
|
+
pass
|
57
44
|
end
|
58
45
|
end
|
59
46
|
#:callable end
|
@@ -63,12 +50,15 @@ class DocsGuardTest < Minitest::Spec
|
|
63
50
|
step Policy::Guard( MyGuard.new )
|
64
51
|
#~pipe-only
|
65
52
|
step :process
|
66
|
-
|
53
|
+
|
54
|
+
def process(options, **)
|
55
|
+
options[:x] = true
|
56
|
+
end
|
67
57
|
#~pipe-only end
|
68
58
|
end
|
69
59
|
#:callable-op end
|
70
60
|
|
71
|
-
it { Create.(pass: false)[:x].
|
61
|
+
it { Create.(pass: false)[:x].must_be_nil }
|
72
62
|
it { Create.(pass: true)[:x].must_equal true }
|
73
63
|
end
|
74
64
|
|
@@ -79,12 +69,15 @@ class DocsGuardMethodTest < Minitest::Spec
|
|
79
69
|
class Create < Trailblazer::Operation
|
80
70
|
step Policy::Guard( :pass? )
|
81
71
|
|
82
|
-
def pass?(options,
|
83
|
-
|
72
|
+
def pass?(options, pass:, **)
|
73
|
+
pass
|
84
74
|
end
|
85
75
|
#~pipe-onlyy
|
86
76
|
step :process
|
87
|
-
|
77
|
+
|
78
|
+
def process(options, **)
|
79
|
+
options["x"] = true
|
80
|
+
end
|
88
81
|
#~pipe-onlyy end
|
89
82
|
end
|
90
83
|
#:method end
|
@@ -103,12 +96,12 @@ class DocsGuardNamedTest < Minitest::Spec
|
|
103
96
|
end
|
104
97
|
#:name end
|
105
98
|
|
106
|
-
it { Create.(
|
107
|
-
it { Create.(
|
99
|
+
it { Create.(:current_user => nil )["result.policy.user"].success?.must_equal false }
|
100
|
+
it { Create.(:current_user => Module)["result.policy.user"].success?.must_equal true }
|
108
101
|
|
109
102
|
it {
|
110
103
|
#:name-result
|
111
|
-
result = Create.(
|
104
|
+
result = Create.(:current_user => true)
|
112
105
|
result["result.policy.user"].success? #=> true
|
113
106
|
#:name-result end
|
114
107
|
}
|
@@ -123,13 +116,13 @@ class DocsGuardInjectionTest < Minitest::Spec
|
|
123
116
|
end
|
124
117
|
#:di-op end
|
125
118
|
|
126
|
-
it { Create.(
|
119
|
+
it { Create.(:current_user => Module).inspect("").must_equal %{<Result:true [nil] >} }
|
127
120
|
it {
|
128
121
|
result =
|
129
122
|
#:di-call
|
130
123
|
Create.({},
|
131
|
-
|
132
|
-
"policy.default.eval" => Trailblazer::Operation::Policy::Guard.build(->(options) { false })
|
124
|
+
:current_user => Module,
|
125
|
+
"policy.default.eval" => Trailblazer::Operation::Policy::Guard.build(->(options, **) { false })
|
133
126
|
)
|
134
127
|
#:di-call end
|
135
128
|
result.inspect("").must_equal %{<Result:false [nil] >} }
|
@@ -143,7 +136,7 @@ class DocsGuardMissingKeywordTest < Minitest::Spec
|
|
143
136
|
end
|
144
137
|
|
145
138
|
it { assert_raises(ArgumentError) { Create.() } }
|
146
|
-
it { Create.(
|
139
|
+
it { Create.(:current_user => Module).success?.must_equal true }
|
147
140
|
end
|
148
141
|
|
149
142
|
#---
|
@@ -157,10 +150,10 @@ class DocsGuardPositionTest < Minitest::Spec
|
|
157
150
|
end
|
158
151
|
#:before end
|
159
152
|
|
160
|
-
it { Create
|
153
|
+
it { Trailblazer::Operation::Inspect.(Create).must_equal %{[>policy.default.eval,>model!]} }
|
161
154
|
it do
|
162
155
|
#:before-pipe
|
163
|
-
|
156
|
+
Trailblazer::Operation::Inspect.(Create, style: :rows) #=>
|
164
157
|
# 0 ========================>operation.new
|
165
158
|
# 1 ==================>policy.default.eval
|
166
159
|
# 2 ===============================>model!
|
data/test/docs/macro_test.rb
CHANGED
data/test/docs/model_test.rb
CHANGED
@@ -20,11 +20,11 @@ class DocsModelTest < Minitest::Spec
|
|
20
20
|
|
21
21
|
it do
|
22
22
|
#:create
|
23
|
-
result = Create.({})
|
24
|
-
result[
|
23
|
+
result = Create.(params: {})
|
24
|
+
result[:model] #=> #<struct Song id=nil, title=nil>
|
25
25
|
#:create end
|
26
26
|
|
27
|
-
result[
|
27
|
+
result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=nil, title=nil>}
|
28
28
|
end
|
29
29
|
|
30
30
|
#:update
|
@@ -36,21 +36,21 @@ class DocsModelTest < Minitest::Spec
|
|
36
36
|
|
37
37
|
it do
|
38
38
|
#:update-ok
|
39
|
-
result = Update.({ id: 1 })
|
40
|
-
result[
|
39
|
+
result = Update.(params: { id: 1 })
|
40
|
+
result[:model] #=> #<struct Song id=1, title="Roxanne">
|
41
41
|
#:update-ok end
|
42
42
|
|
43
|
-
result[
|
43
|
+
result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=1, title=nil>}
|
44
44
|
end
|
45
45
|
|
46
46
|
it do
|
47
47
|
#:update-fail
|
48
|
-
result = Update.({})
|
49
|
-
result[
|
48
|
+
result = Update.(params: {})
|
49
|
+
result[:model] #=> nil
|
50
50
|
result.success? #=> false
|
51
51
|
#:update-fail end
|
52
52
|
|
53
|
-
result[
|
53
|
+
result[:model].must_be_nil
|
54
54
|
result.success?.must_equal false
|
55
55
|
end
|
56
56
|
|
@@ -62,14 +62,14 @@ class DocsModelTest < Minitest::Spec
|
|
62
62
|
#:show end
|
63
63
|
|
64
64
|
it do
|
65
|
-
result = Show.({ id: 1 })
|
65
|
+
result = Show.(params: { id: 1 })
|
66
66
|
|
67
67
|
#:show-ok
|
68
|
-
result = Show.({ id: 1 })
|
69
|
-
result[
|
68
|
+
result = Show.(params: { id: 1 })
|
69
|
+
result[:model] #=> #<struct Song id=1, title="Roxanne">
|
70
70
|
#:show-ok end
|
71
71
|
|
72
72
|
result.success?.must_equal true
|
73
|
-
result[
|
73
|
+
result[:model].inspect.must_equal %{#<struct DocsModelTest::Song id=100, title=nil>}
|
74
74
|
end
|
75
75
|
end
|
data/test/docs/nested_test.rb
CHANGED
@@ -11,6 +11,7 @@ class DocsNestedOperationTest < Minitest::Spec
|
|
11
11
|
#- nested operations
|
12
12
|
#:edit
|
13
13
|
class Edit < Trailblazer::Operation
|
14
|
+
extend ClassDependencies
|
14
15
|
extend Contract::DSL
|
15
16
|
|
16
17
|
contract do
|
@@ -26,97 +27,63 @@ class DocsNestedOperationTest < Minitest::Spec
|
|
26
27
|
#:update
|
27
28
|
class Update < Trailblazer::Operation
|
28
29
|
step Nested( Edit )
|
30
|
+
# step ->(options, **) { puts options.keys.inspect }
|
29
31
|
step Contract::Validate()
|
32
|
+
|
30
33
|
step Contract::Persist( method: :sync )
|
31
34
|
end
|
32
35
|
#:update end
|
33
36
|
|
34
|
-
puts Update["pipetree"].inspect(style: :rows)
|
37
|
+
# puts Update["pipetree"].inspect(style: :rows)
|
35
38
|
|
36
39
|
#-
|
37
40
|
# Edit allows grabbing model and contract
|
38
41
|
it do
|
39
42
|
#:edit-call
|
40
|
-
result = Edit.(id: 1)
|
43
|
+
result = Edit.(params: {id: 1})
|
41
44
|
|
42
|
-
result[
|
45
|
+
result[:model] #=> #<Song id=1, title=\"Bristol\">
|
43
46
|
result["contract.default"] #=> #<Reform::Form ..>
|
44
47
|
#:edit-call end
|
45
|
-
result.inspect(
|
46
|
-
result["contract.default"].model.must_equal result[
|
48
|
+
result.inspect(:model).must_equal %{<Result:true [#<struct DocsNestedOperationTest::Song id=1, title=\"Bristol\">] >}
|
49
|
+
result["contract.default"].model.must_equal result[:model]
|
50
|
+
end
|
51
|
+
|
52
|
+
it "provides all steps for Introspect" do
|
53
|
+
Trailblazer::Activity::Trace.compute_debug( Edit ).values.must_equal [{:id=>"model.build"}, {:id=>"contract.build"}]
|
54
|
+
Trailblazer::Activity::Trace.compute_debug( Update ).values.must_equal [{:id=>"Nested(DocsNestedOperationTest::Edit)"}, {:id=>"contract.default.validate"}, {:id=>"persist.save"}, {:id=>"model.build"}, {:id=>"contract.build"}, {:id=>"contract.default.params_extract"}, {:id=>"contract.default.call"}]
|
47
55
|
end
|
48
56
|
|
57
|
+
#- test Edit circuit-level.
|
58
|
+
it do
|
59
|
+
signal, (result, _) = Edit.__call__( [Trailblazer::Context( params: {id: 1} ), {}] )
|
60
|
+
result[:model].inspect.must_equal %{#<struct DocsNestedOperationTest::Song id=1, title=\"Bristol\">}
|
61
|
+
end
|
62
|
+
|
49
63
|
#-
|
50
|
-
# Update also allows grabbing model and contract
|
64
|
+
# Update also allows grabbing Edit/model and Edit/contract
|
51
65
|
it do
|
52
66
|
#:update-call
|
53
|
-
result = Update.(id: 1, title: "Call It A Night")
|
67
|
+
result = Update.(params: {id: 1, title: "Call It A Night"})
|
54
68
|
|
55
|
-
result[
|
69
|
+
result[:model] #=> #<Song id=1 , title=\"Call It A Night\">
|
56
70
|
result["contract.default"] #=> #<Reform::Form ..>
|
57
71
|
#:update-call end
|
58
|
-
result.inspect(
|
59
|
-
result["contract.default"].model.must_equal result[
|
72
|
+
result.inspect(:model).must_equal %{<Result:true [#<struct DocsNestedOperationTest::Song id=1, title=\"Call It A Night\">] >}
|
73
|
+
result["contract.default"].model.must_equal result[:model]
|
60
74
|
end
|
61
75
|
|
62
76
|
#-
|
63
77
|
# Edit is successful.
|
64
78
|
it do
|
65
|
-
result = Update.({ id: 1, title: "Miami" },
|
66
|
-
result.inspect(
|
79
|
+
result = Update.(params: { id: 1, title: "Miami" }, current_user: Module)
|
80
|
+
result.inspect(:model).must_equal %{<Result:true [#<struct DocsNestedOperationTest::Song id=1, title="Miami">] >}
|
67
81
|
end
|
68
82
|
|
69
83
|
# Edit fails
|
70
84
|
it do
|
71
|
-
Update.(id: 2).inspect(
|
72
|
-
end
|
73
|
-
|
74
|
-
#---
|
75
|
-
#- shared data
|
76
|
-
class B < Trailblazer::Operation
|
77
|
-
success ->(options) { options["can.B.see.it?"] = options["this.should.not.be.visible.in.B"] }
|
78
|
-
success ->(options) { options["can.B.see.current_user?"] = options["current_user"] }
|
79
|
-
success ->(options) { options["can.B.see.params?"] = options["params"] }
|
80
|
-
success ->(options) { options["can.B.see.A.class.data?"] = options["A.class.data"] }
|
81
|
-
end
|
82
|
-
|
83
|
-
class A < Trailblazer::Operation
|
84
|
-
self["A.class.data"] = true
|
85
|
-
|
86
|
-
success ->(options) { options["this.should.not.be.visible.in.B"] = true }
|
87
|
-
step Nested( B )
|
88
|
-
end
|
89
|
-
|
90
|
-
# mutual data from A doesn't bleed into B.
|
91
|
-
it { A.()["can.B.see.it?"].must_equal nil }
|
92
|
-
it { A.()["this.should.not.be.visible.in.B"].must_equal true }
|
93
|
-
# runtime dependencies are visible in B.
|
94
|
-
it { A.({}, "current_user" => Module)["can.B.see.current_user?"].must_equal Module }
|
95
|
-
it { A.({ a: 1 })["can.B.see.params?"].must_equal({ a: 1 }) }
|
96
|
-
# class data from A doesn't bleed into B.
|
97
|
-
it { A.()["can.B.see.A.class.data?"].must_equal nil }
|
98
|
-
|
99
|
-
|
100
|
-
# cr_result = Create.({}, "result" => result)
|
101
|
-
# puts cr_result["model"]
|
102
|
-
# puts cr_result["contract.default"]
|
103
|
-
|
104
|
-
#---
|
105
|
-
#- Nested( .., input: )
|
106
|
-
class C < Trailblazer::Operation
|
107
|
-
self["C.class.data"] = true
|
108
|
-
|
109
|
-
success ->(options) { options["this.should.not.be.visible.in.B"] = true }
|
110
|
-
|
111
|
-
step Nested( B, input: ->(options, runtime_data:, mutable_data:, **) {
|
112
|
-
runtime_data.merge( "this.should.not.be.visible.in.B" => mutable_data["this.should.not.be.visible.in.B"] )
|
113
|
-
} )
|
85
|
+
Update.(params: {id: 2}).inspect(:model).must_equal %{<Result:false [nil] >}
|
114
86
|
end
|
115
|
-
|
116
|
-
it { C.()["can.B.see.it?"].must_equal true }
|
117
|
-
it { C.()["this.should.not.be.visible.in.B"].must_equal true } # this IS visible since we use :input!
|
118
|
-
it { C.({}, "current_user" => Module)["can.B.see.current_user?"].must_equal Module }
|
119
|
-
it { C.()["can.B.see.A.class.data?"].must_equal nil }
|
120
87
|
end
|
121
88
|
|
122
89
|
class NestedInput < Minitest::Spec
|
@@ -128,19 +95,20 @@ class NestedInput < Minitest::Spec
|
|
128
95
|
|
129
96
|
#:input-pi
|
130
97
|
class MultiplyByPi < Trailblazer::Operation
|
131
|
-
step ->(options) { options["pi_constant"] = 3.14159 }
|
132
|
-
step Nested( Multiplier, input: ->(options,
|
133
|
-
{ "y" =>
|
134
|
-
"x" =>
|
98
|
+
step ->(options, **) { options["pi_constant"] = 3.14159 }
|
99
|
+
step Nested( Multiplier, input: ->(options, **) do
|
100
|
+
{ "y" => options["pi_constant"],
|
101
|
+
"x" => options["x"]
|
102
|
+
}
|
135
103
|
end )
|
136
104
|
end
|
137
105
|
#:input-pi end
|
138
106
|
|
139
|
-
it { MultiplyByPi.(
|
107
|
+
it { MultiplyByPi.("x" => 9).inspect("product").must_equal %{<Result:true [28.27431] >} }
|
140
108
|
|
141
109
|
it do
|
142
110
|
#:input-result
|
143
|
-
result = MultiplyByPi.(
|
111
|
+
result = MultiplyByPi.("x" => 9)
|
144
112
|
result["product"] #=> [28.27431]
|
145
113
|
#:input-result end
|
146
114
|
end
|
@@ -151,12 +119,10 @@ class NestedInputCallable < Minitest::Spec
|
|
151
119
|
|
152
120
|
#:input-callable
|
153
121
|
class MyInput
|
154
|
-
|
155
|
-
|
156
|
-
def self.call(options, mutable_data:, runtime_data:, **)
|
122
|
+
def self.call(options, **)
|
157
123
|
{
|
158
|
-
"y" =>
|
159
|
-
"x" =>
|
124
|
+
"y" => options["pi_constant"],
|
125
|
+
"x" => options["x"]
|
160
126
|
}
|
161
127
|
end
|
162
128
|
end
|
@@ -164,12 +130,12 @@ class NestedInputCallable < Minitest::Spec
|
|
164
130
|
|
165
131
|
#:input-callable-op
|
166
132
|
class MultiplyByPi < Trailblazer::Operation
|
167
|
-
step ->(options) { options["pi_constant"] = 3.14159 }
|
133
|
+
step ->(options, **) { options["pi_constant"] = 3.14159 }
|
168
134
|
step Nested( Multiplier, input: MyInput )
|
169
135
|
end
|
170
136
|
#:input-callable-op end
|
171
137
|
|
172
|
-
it { MultiplyByPi.(
|
138
|
+
it { MultiplyByPi.("x" => 9).inspect("product").must_equal %{<Result:true [28.27431] >} }
|
173
139
|
end
|
174
140
|
|
175
141
|
#---
|
@@ -179,10 +145,10 @@ class NestedOutput < Minitest::Spec
|
|
179
145
|
|
180
146
|
#:output
|
181
147
|
class Update < Trailblazer::Operation
|
182
|
-
step Nested( Edit, output: ->(options,
|
148
|
+
step Nested( Edit, output: ->(options, **) do
|
183
149
|
{
|
184
|
-
"contract.my" =>
|
185
|
-
|
150
|
+
"contract.my" => options["contract.default"],
|
151
|
+
model: options[:model]
|
186
152
|
}
|
187
153
|
end )
|
188
154
|
step Contract::Validate( name: "my" )
|
@@ -190,55 +156,21 @@ class NestedOutput < Minitest::Spec
|
|
190
156
|
end
|
191
157
|
#:output end
|
192
158
|
|
193
|
-
it { Update.( id: 1, title: "Call It A Night" ).inspect(
|
159
|
+
it { Update.( params: {id: 1, title: "Call It A Night"} ).inspect(:model, "contract.default").
|
160
|
+
must_equal %{<Result:true [#<struct DocsNestedOperationTest::Song id=1, title=\"Call It A Night\">, nil] >} }
|
194
161
|
|
195
162
|
it do
|
196
|
-
result = Update.( id: 1, title: "Call It A Night" )
|
163
|
+
result = Update.( params: {id: 1, title: "Call It A Night"} )
|
197
164
|
|
198
|
-
result[
|
165
|
+
result[:model] #=> #<Song id=1 , title=\"Call It A Night\">
|
199
166
|
end
|
200
167
|
end
|
201
168
|
|
202
|
-
class NestedClassLevelTest < Minitest::Spec
|
203
|
-
#:class-level
|
204
|
-
class New < Trailblazer::Operation
|
205
|
-
step ->(options) { options["class"] = true }, before: "operation.new"
|
206
|
-
step ->(options) { options["x"] = true }
|
207
|
-
end
|
208
|
-
|
209
|
-
class Create < Trailblazer::Operation
|
210
|
-
step Nested( New )
|
211
|
-
step ->(options) { options["y"] = true }
|
212
|
-
end
|
213
|
-
#:class-level end
|
214
|
-
|
215
|
-
it { Create.().inspect("x", "y").must_equal %{<Result:true [true, true] >} }
|
216
|
-
it { Create.(); Create["class"].must_equal nil }
|
217
|
-
end
|
218
|
-
|
219
169
|
#---
|
220
170
|
# Nested( ->{} )
|
221
171
|
class NestedWithCallableTest < Minitest::Spec
|
222
172
|
Song = Struct.new(:id, :title)
|
223
173
|
|
224
|
-
class X < Trailblazer::Operation
|
225
|
-
step ->(options, params:, **) { options["params.original"] = params }
|
226
|
-
step ->(options) { options["x"] = true }
|
227
|
-
end
|
228
|
-
|
229
|
-
class Y < Trailblazer::Operation
|
230
|
-
step ->(options) { options["y"] = true }
|
231
|
-
end
|
232
|
-
|
233
|
-
class A < Trailblazer::Operation
|
234
|
-
step ->(options) { options["z"] = true }
|
235
|
-
step Nested( ->(options, *) { options["class"] } )
|
236
|
-
end
|
237
|
-
|
238
|
-
it { A.({ a: 1 }, "class" => X).inspect("x", "y", "z", "params.original").must_equal "<Result:true [true, nil, true, {:a=>1}] >" }
|
239
|
-
it { A.({}, "class" => Y).inspect("x", "y", "z").must_equal "<Result:true [nil, true, true] >" }
|
240
|
-
# it { Create.({}).inspect("x", "y", "z").must_equal "<Result:true [nil, true, true] >" }
|
241
|
-
|
242
174
|
class Song
|
243
175
|
module Contract
|
244
176
|
class Create < Reform::Form
|
@@ -277,8 +209,8 @@ class NestedWithCallableTest < Minitest::Spec
|
|
277
209
|
let (:admin) { User.new(true) }
|
278
210
|
let (:anonymous) { User.new(false) }
|
279
211
|
|
280
|
-
it { Create.({},
|
281
|
-
it { Create.({},
|
212
|
+
it { Create.(params: {}, current_user: anonymous).inspect("x").must_equal %{<Result:true [true] >} }
|
213
|
+
it { Create.(params: {}, current_user: admin) .inspect("x").must_equal %{<Result:true [nil] >} }
|
282
214
|
|
283
215
|
#---
|
284
216
|
#:method
|
@@ -286,13 +218,13 @@ class NestedWithCallableTest < Minitest::Spec
|
|
286
218
|
step Nested( :build! )
|
287
219
|
|
288
220
|
def build!(options, current_user:nil, **)
|
289
|
-
|
221
|
+
current_user.admin? ? Create::Admin : Create::NeedsModeration
|
290
222
|
end
|
291
223
|
end
|
292
224
|
#:method end
|
293
225
|
|
294
|
-
it { Update.({},
|
295
|
-
it { Update.({},
|
226
|
+
it { Update.(params: {}, current_user: anonymous).inspect("x").must_equal %{<Result:true [true] >} }
|
227
|
+
it { Update.(params: {}, current_user: admin) .inspect("x").must_equal %{<Result:true [nil] >} }
|
296
228
|
|
297
229
|
#---
|
298
230
|
#:callable-builder
|
@@ -312,8 +244,8 @@ class NestedWithCallableTest < Minitest::Spec
|
|
312
244
|
end
|
313
245
|
#:callable end
|
314
246
|
|
315
|
-
it { Delete.({},
|
316
|
-
it { Delete.({},
|
247
|
+
it { Delete.(params: {}, current_user: anonymous).inspect("x").must_equal %{<Result:true [true] >} }
|
248
|
+
it { Delete.(params: {}, current_user: admin) .inspect("x").must_equal %{<Result:true [nil] >} }
|
317
249
|
end
|
318
250
|
|
319
251
|
# builder: Nested + deviate to left if nil / skip_track if true
|
@@ -330,5 +262,5 @@ class NestedNameTest < Minitest::Spec
|
|
330
262
|
# ...
|
331
263
|
end
|
332
264
|
|
333
|
-
it { Create
|
265
|
+
it { Operation::Inspect.(Create).must_equal %{[>>Nested(NestedNameTest::Create::Present)]} }
|
334
266
|
end
|