trailblazer 1.1.2 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -7
- data/CHANGES.md +108 -0
- data/COMM-LICENSE +91 -0
- data/Gemfile +18 -4
- data/LICENSE.txt +7 -20
- data/README.md +55 -15
- data/Rakefile +21 -2
- data/draft-1.2.rb +7 -0
- data/lib/trailblazer.rb +17 -4
- data/lib/trailblazer/dsl.rb +47 -0
- data/lib/trailblazer/operation/auto_inject.rb +47 -0
- data/lib/trailblazer/operation/builder.rb +18 -18
- data/lib/trailblazer/operation/callback.rb +31 -38
- data/lib/trailblazer/operation/contract.rb +46 -0
- data/lib/trailblazer/operation/controller.rb +45 -27
- data/lib/trailblazer/operation/guard.rb +24 -0
- data/lib/trailblazer/operation/model.rb +41 -33
- data/lib/trailblazer/operation/nested.rb +43 -0
- data/lib/trailblazer/operation/params.rb +13 -0
- data/lib/trailblazer/operation/persist.rb +13 -0
- data/lib/trailblazer/operation/policy.rb +26 -72
- data/lib/trailblazer/operation/present.rb +19 -0
- data/lib/trailblazer/operation/procedural/contract.rb +15 -0
- data/lib/trailblazer/operation/procedural/validate.rb +22 -0
- data/lib/trailblazer/operation/pundit.rb +42 -0
- data/lib/trailblazer/operation/representer.rb +25 -92
- data/lib/trailblazer/operation/rescue.rb +23 -0
- data/lib/trailblazer/operation/resolver.rb +18 -24
- data/lib/trailblazer/operation/validate.rb +50 -0
- data/lib/trailblazer/operation/wrap.rb +37 -0
- data/lib/trailblazer/version.rb +1 -1
- data/test/{operation/controller_test.rb → controller_test.rb} +8 -4
- data/test/docs/auto_inject_test.rb +30 -0
- data/test/docs/contract_test.rb +429 -0
- data/test/docs/dry_test.rb +31 -0
- data/test/docs/guard_test.rb +143 -0
- data/test/docs/nested_test.rb +117 -0
- data/test/docs/policy_test.rb +2 -0
- data/test/docs/pundit_test.rb +109 -0
- data/test/docs/representer_test.rb +268 -0
- data/test/docs/rescue_test.rb +153 -0
- data/test/docs/wrap_test.rb +174 -0
- data/test/gemfiles/Gemfile.ruby-1.9 +3 -0
- data/test/gemfiles/Gemfile.ruby-2.0 +12 -0
- data/test/gemfiles/Gemfile.ruby-2.3 +12 -0
- data/test/module_test.rb +22 -15
- data/test/operation/builder_test.rb +66 -18
- data/test/operation/callback_test.rb +70 -0
- data/test/operation/contract_test.rb +385 -15
- data/test/operation/dsl/callback_test.rb +18 -30
- data/test/operation/dsl/contract_test.rb +209 -19
- data/test/operation/dsl/representer_test.rb +42 -15
- data/test/operation/guard_test.rb +1 -147
- data/test/operation/model_test.rb +105 -0
- data/test/operation/params_test.rb +36 -0
- data/test/operation/persist_test.rb +44 -0
- data/test/operation/pipedream_test.rb +59 -0
- data/test/operation/pipetree_test.rb +104 -0
- data/test/operation/present_test.rb +24 -0
- data/test/operation/pundit_test.rb +104 -0
- data/test/{representer_test.rb → operation/representer_test.rb} +58 -42
- data/test/operation/resolver_test.rb +34 -70
- data/test/operation_test.rb +57 -189
- data/test/test_helper.rb +23 -3
- data/trailblazer.gemspec +8 -7
- metadata +91 -59
- data/gemfiles/Gemfile.rails.lock +0 -130
- data/gemfiles/Gemfile.reform-2.0 +0 -6
- data/gemfiles/Gemfile.reform-2.1 +0 -7
- data/lib/trailblazer/autoloading.rb +0 -15
- data/lib/trailblazer/endpoint.rb +0 -31
- data/lib/trailblazer/operation.rb +0 -175
- data/lib/trailblazer/operation/collection.rb +0 -6
- data/lib/trailblazer/operation/dispatch.rb +0 -3
- data/lib/trailblazer/operation/model/dsl.rb +0 -29
- data/lib/trailblazer/operation/model/external.rb +0 -34
- data/lib/trailblazer/operation/policy/guard.rb +0 -35
- data/lib/trailblazer/operation/uploaded_file.rb +0 -77
- data/test/callback_test.rb +0 -104
- data/test/collection_test.rb +0 -57
- data/test/model_test.rb +0 -148
- data/test/operation/external_model_test.rb +0 -71
- data/test/operation/policy_test.rb +0 -97
- data/test/operation/reject_test.rb +0 -34
- data/test/rollback_test.rb +0 -47
@@ -1,97 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
require "trailblazer/operation/policy"
|
3
|
-
|
4
|
-
|
5
|
-
class OpPunditPolicyTest < MiniTest::Spec
|
6
|
-
Song = Struct.new(:name)
|
7
|
-
User = Struct.new(:name)
|
8
|
-
|
9
|
-
class BlaPolicy
|
10
|
-
def initialize(user, song)
|
11
|
-
@user = user
|
12
|
-
@song = song
|
13
|
-
end
|
14
|
-
|
15
|
-
def create?
|
16
|
-
@user.is_a?(User) and @song.is_a?(Song)
|
17
|
-
end
|
18
|
-
|
19
|
-
def edit?
|
20
|
-
"yepp"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class BlaOperation < Trailblazer::Operation
|
25
|
-
include Policy
|
26
|
-
policy BlaPolicy, :create?
|
27
|
-
|
28
|
-
def model!(*)
|
29
|
-
Song.new
|
30
|
-
end
|
31
|
-
|
32
|
-
def process(*)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# valid.
|
37
|
-
it do
|
38
|
-
op = BlaOperation.({current_user: User.new})
|
39
|
-
|
40
|
-
# #policy provides the Policy instance.
|
41
|
-
op.policy.edit?.must_equal "yepp"
|
42
|
-
end
|
43
|
-
|
44
|
-
# invalid.
|
45
|
-
it do
|
46
|
-
assert_raises Trailblazer::NotAuthorizedError do
|
47
|
-
op = BlaOperation.({current_user: nil})
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
# no policy set
|
53
|
-
class NoPolicyOperation < Trailblazer::Operation
|
54
|
-
include Policy
|
55
|
-
# no policy.
|
56
|
-
|
57
|
-
def process(*)
|
58
|
-
@model = Song.new
|
59
|
-
end
|
60
|
-
|
61
|
-
class Delete < self
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
|
-
class LocalPolicy
|
66
|
-
def initialize(user, song)
|
67
|
-
@user = user
|
68
|
-
@song = song
|
69
|
-
end
|
70
|
-
|
71
|
-
def update?; false end
|
72
|
-
end
|
73
|
-
|
74
|
-
class Update < self
|
75
|
-
policy LocalPolicy, :update?
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
# valid.
|
80
|
-
it do
|
81
|
-
op = NoPolicyOperation.({})
|
82
|
-
op.model.must_be_instance_of Song
|
83
|
-
end
|
84
|
-
|
85
|
-
# inherited without config works.
|
86
|
-
it do
|
87
|
-
op = NoPolicyOperation::Delete.({})
|
88
|
-
op.model.must_be_instance_of Song
|
89
|
-
end
|
90
|
-
|
91
|
-
# inherited can override.
|
92
|
-
it do
|
93
|
-
assert_raises Trailblazer::NotAuthorizedError do
|
94
|
-
op = NoPolicyOperation::Update.({})
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class OperationRejectTest < MiniTest::Spec
|
4
|
-
class Operation < Trailblazer::Operation
|
5
|
-
def process(params)
|
6
|
-
invalid! if params == false
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
it do
|
11
|
-
run = nil
|
12
|
-
Operation.run(true) { run = true }
|
13
|
-
run.must_equal true
|
14
|
-
end
|
15
|
-
|
16
|
-
it do
|
17
|
-
run = nil
|
18
|
-
Operation.run(false) { run = true }
|
19
|
-
run.must_equal nil
|
20
|
-
end
|
21
|
-
|
22
|
-
it do
|
23
|
-
run = nil
|
24
|
-
op = Operation.reject(true) { run = true }
|
25
|
-
run.must_equal nil
|
26
|
-
op.must_be_instance_of Operation
|
27
|
-
end
|
28
|
-
|
29
|
-
it do
|
30
|
-
run = nil
|
31
|
-
Operation.reject(false) { run = true }
|
32
|
-
run.must_equal true
|
33
|
-
end
|
34
|
-
end
|
data/test/rollback_test.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
# TODO: rollback for composed operations: this is basically implemented already as every operation knows how to rollback.
|
4
|
-
# however, this has to be run for composed operations.
|
5
|
-
# we can also add Transaction and Lock for real uniqueness validators, etc.
|
6
|
-
#
|
7
|
-
# i am keen to try integrating https://github.com/collectiveidea/interactor organizers!
|
8
|
-
module Trailblazer::Operation::Rollback
|
9
|
-
def run
|
10
|
-
begin
|
11
|
-
super
|
12
|
-
rescue
|
13
|
-
rollback!(@params, $!)
|
14
|
-
[false, self]
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class RollbackTest < MiniTest::Spec
|
20
|
-
class ExceptionalOperation < Trailblazer::Operation
|
21
|
-
include Rollback
|
22
|
-
|
23
|
-
def process(params)
|
24
|
-
@_params = params
|
25
|
-
raise # something happens.
|
26
|
-
end
|
27
|
-
|
28
|
-
attr_reader :_params, :_rollback_args
|
29
|
-
|
30
|
-
def rollback!(params, exception)
|
31
|
-
@_rollback_args = [params, exception]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
module Comparable
|
36
|
-
def ==(other)
|
37
|
-
self.class == other.class
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
it do
|
42
|
-
op = ExceptionalOperation.("amazing")
|
43
|
-
op._params.must_equal "amazing"
|
44
|
-
|
45
|
-
op._rollback_args.must_equal ["amazing", RuntimeError.new.extend(Comparable)]
|
46
|
-
end
|
47
|
-
end
|