trailblazer 1.1.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml +101 -0
  4. data/.rubocop.yml +20 -0
  5. data/.rubocop_todo.yml +556 -0
  6. data/.travis.yml +6 -7
  7. data/CHANGES.md +224 -0
  8. data/COMM-LICENSE +62 -0
  9. data/CONTRIBUTING.md +179 -0
  10. data/Gemfile +0 -10
  11. data/LICENSE +9 -0
  12. data/README.md +68 -189
  13. data/Rakefile +3 -3
  14. data/lib/trailblazer/version.rb +3 -1
  15. data/lib/trailblazer.rb +3 -6
  16. data/test/test_helper.rb +32 -3
  17. data/trailblazer.gemspec +11 -15
  18. metadata +28 -132
  19. data/LICENSE.txt +0 -22
  20. data/TODO.md +0 -11
  21. data/doc/Trb-The-Stack.png +0 -0
  22. data/doc/trb.jpg +0 -0
  23. data/gemfiles/Gemfile.rails.lock +0 -130
  24. data/gemfiles/Gemfile.reform-2.0 +0 -6
  25. data/gemfiles/Gemfile.reform-2.1 +0 -7
  26. data/lib/trailblazer/autoloading.rb +0 -15
  27. data/lib/trailblazer/endpoint.rb +0 -31
  28. data/lib/trailblazer/operation/builder.rb +0 -26
  29. data/lib/trailblazer/operation/callback.rb +0 -53
  30. data/lib/trailblazer/operation/collection.rb +0 -6
  31. data/lib/trailblazer/operation/controller.rb +0 -72
  32. data/lib/trailblazer/operation/dispatch.rb +0 -3
  33. data/lib/trailblazer/operation/model/dsl.rb +0 -29
  34. data/lib/trailblazer/operation/model/external.rb +0 -34
  35. data/lib/trailblazer/operation/model.rb +0 -50
  36. data/lib/trailblazer/operation/module.rb +0 -29
  37. data/lib/trailblazer/operation/policy/guard.rb +0 -35
  38. data/lib/trailblazer/operation/policy.rb +0 -85
  39. data/lib/trailblazer/operation/representer.rb +0 -98
  40. data/lib/trailblazer/operation/resolver.rb +0 -30
  41. data/lib/trailblazer/operation/uploaded_file.rb +0 -77
  42. data/lib/trailblazer/operation.rb +0 -175
  43. data/test/callback_test.rb +0 -104
  44. data/test/collection_test.rb +0 -57
  45. data/test/model_test.rb +0 -148
  46. data/test/module_test.rb +0 -93
  47. data/test/operation/builder_test.rb +0 -41
  48. data/test/operation/contract_test.rb +0 -30
  49. data/test/operation/controller_test.rb +0 -111
  50. data/test/operation/dsl/callback_test.rb +0 -118
  51. data/test/operation/dsl/contract_test.rb +0 -104
  52. data/test/operation/dsl/representer_test.rb +0 -142
  53. data/test/operation/external_model_test.rb +0 -71
  54. data/test/operation/guard_test.rb +0 -152
  55. data/test/operation/policy_test.rb +0 -97
  56. data/test/operation/reject_test.rb +0 -34
  57. data/test/operation/resolver_test.rb +0 -83
  58. data/test/operation_test.rb +0 -275
  59. data/test/representer_test.rb +0 -238
  60. data/test/rollback_test.rb +0 -47
metadata CHANGED
@@ -1,51 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trailblazer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-27 00:00:00.000000000 Z
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: uber
14
+ name: trailblazer-macro
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.15
19
+ version: 2.1.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: 0.0.15
29
+ version: 2.1.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 2.2.0
27
33
  - !ruby/object:Gem::Dependency
28
- name: reform
34
+ name: trailblazer-macro-contract
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
33
- version: 2.0.0
39
+ version: 2.1.0
34
40
  - - "<"
35
41
  - !ruby/object:Gem::Version
36
- version: 3.0.0
42
+ version: 2.2.0
37
43
  type: :runtime
38
44
  prerelease: false
39
45
  version_requirements: !ruby/object:Gem::Requirement
40
46
  requirements:
41
47
  - - ">="
42
48
  - !ruby/object:Gem::Version
43
- version: 2.0.0
49
+ version: 2.1.0
44
50
  - - "<"
45
51
  - !ruby/object:Gem::Version
46
- version: 3.0.0
52
+ version: 2.2.0
47
53
  - !ruby/object:Gem::Dependency
48
- name: declarative
54
+ name: trailblazer-operation
49
55
  requirement: !ruby/object:Gem::Requirement
50
56
  requirements:
51
57
  - - ">="
@@ -58,20 +64,6 @@ dependencies:
58
64
  - - ">="
59
65
  - !ruby/object:Gem::Version
60
66
  version: '0'
61
- - !ruby/object:Gem::Dependency
62
- name: activemodel
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- version: '0'
75
67
  - !ruby/object:Gem::Dependency
76
68
  name: bundler
77
69
  requirement: !ruby/object:Gem::Requirement
@@ -114,50 +106,8 @@ dependencies:
114
106
  - - ">="
115
107
  - !ruby/object:Gem::Version
116
108
  version: '0'
117
- - !ruby/object:Gem::Dependency
118
- name: sqlite3
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - ">="
122
- - !ruby/object:Gem::Version
123
- version: '0'
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - ">="
129
- - !ruby/object:Gem::Version
130
- version: '0'
131
- - !ruby/object:Gem::Dependency
132
- name: database_cleaner
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - ">="
136
- - !ruby/object:Gem::Version
137
- version: '0'
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - ">="
143
- - !ruby/object:Gem::Version
144
- version: '0'
145
- - !ruby/object:Gem::Dependency
146
- name: roar
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - ">="
150
- - !ruby/object:Gem::Version
151
- version: '0'
152
- type: :development
153
- prerelease: false
154
- version_requirements: !ruby/object:Gem::Requirement
155
- requirements:
156
- - - ">="
157
- - !ruby/object:Gem::Version
158
- version: '0'
159
- description: A high-level, modular architecture for Ruby framworks with domain and
160
- form objects, view models, twin decorators and representers.
109
+ description: A high-level architecture introducing new abstractions such as operations
110
+ and control flow, form objects and policies.
161
111
  email:
162
112
  - apotonick@gmail.com
163
113
  executables: []
@@ -165,61 +115,25 @@ extensions: []
165
115
  extra_rdoc_files: []
166
116
  files:
167
117
  - ".gitignore"
118
+ - ".rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml"
119
+ - ".rubocop.yml"
120
+ - ".rubocop_todo.yml"
168
121
  - ".travis.yml"
169
122
  - CHANGES.md
123
+ - COMM-LICENSE
124
+ - CONTRIBUTING.md
170
125
  - Gemfile
171
- - LICENSE.txt
126
+ - LICENSE
172
127
  - README.md
173
128
  - Rakefile
174
129
  - THOUGHTS
175
- - TODO.md
176
- - doc/Trb-The-Stack.png
177
- - doc/trb.jpg
178
- - gemfiles/Gemfile.rails.lock
179
- - gemfiles/Gemfile.reform-2.0
180
- - gemfiles/Gemfile.reform-2.1
181
130
  - lib/trailblazer.rb
182
- - lib/trailblazer/autoloading.rb
183
- - lib/trailblazer/endpoint.rb
184
- - lib/trailblazer/operation.rb
185
- - lib/trailblazer/operation/builder.rb
186
- - lib/trailblazer/operation/callback.rb
187
- - lib/trailblazer/operation/collection.rb
188
- - lib/trailblazer/operation/controller.rb
189
- - lib/trailblazer/operation/dispatch.rb
190
- - lib/trailblazer/operation/model.rb
191
- - lib/trailblazer/operation/model/dsl.rb
192
- - lib/trailblazer/operation/model/external.rb
193
- - lib/trailblazer/operation/module.rb
194
- - lib/trailblazer/operation/policy.rb
195
- - lib/trailblazer/operation/policy/guard.rb
196
- - lib/trailblazer/operation/representer.rb
197
- - lib/trailblazer/operation/resolver.rb
198
- - lib/trailblazer/operation/uploaded_file.rb
199
131
  - lib/trailblazer/version.rb
200
- - test/callback_test.rb
201
- - test/collection_test.rb
202
- - test/model_test.rb
203
- - test/module_test.rb
204
- - test/operation/builder_test.rb
205
- - test/operation/contract_test.rb
206
- - test/operation/controller_test.rb
207
- - test/operation/dsl/callback_test.rb
208
- - test/operation/dsl/contract_test.rb
209
- - test/operation/dsl/representer_test.rb
210
- - test/operation/external_model_test.rb
211
- - test/operation/guard_test.rb
212
- - test/operation/policy_test.rb
213
- - test/operation/reject_test.rb
214
- - test/operation/resolver_test.rb
215
- - test/operation_test.rb
216
- - test/representer_test.rb
217
- - test/rollback_test.rb
218
132
  - test/test_helper.rb
219
133
  - trailblazer.gemspec
220
134
  homepage: http://trailblazer.to
221
135
  licenses:
222
- - MIT
136
+ - LGPL-3.0
223
137
  metadata: {}
224
138
  post_install_message:
225
139
  rdoc_options: []
@@ -229,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
229
143
  requirements:
230
144
  - - ">="
231
145
  - !ruby/object:Gem::Version
232
- version: '0'
146
+ version: 2.1.0
233
147
  required_rubygems_version: !ruby/object:Gem::Requirement
234
148
  requirements:
235
149
  - - ">="
@@ -237,27 +151,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
151
  version: '0'
238
152
  requirements: []
239
153
  rubyforge_project:
240
- rubygems_version: 2.4.8
154
+ rubygems_version: 2.7.6
241
155
  signing_key:
242
156
  specification_version: 4
243
157
  summary: A high-level architecture for Ruby and Rails.
244
158
  test_files:
245
- - test/callback_test.rb
246
- - test/collection_test.rb
247
- - test/model_test.rb
248
- - test/module_test.rb
249
- - test/operation/builder_test.rb
250
- - test/operation/contract_test.rb
251
- - test/operation/controller_test.rb
252
- - test/operation/dsl/callback_test.rb
253
- - test/operation/dsl/contract_test.rb
254
- - test/operation/dsl/representer_test.rb
255
- - test/operation/external_model_test.rb
256
- - test/operation/guard_test.rb
257
- - test/operation/policy_test.rb
258
- - test/operation/reject_test.rb
259
- - test/operation/resolver_test.rb
260
- - test/operation_test.rb
261
- - test/representer_test.rb
262
- - test/rollback_test.rb
263
159
  - test/test_helper.rb
data/LICENSE.txt DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2013 Nick Sutterer
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/TODO.md DELETED
@@ -1,11 +0,0 @@
1
- # Operation: External API
2
-
3
- * allow `Op[{body: "Great!"}, {additional: true}]` to save merge.
4
- * make `Op[]` not require wrap like `comment: {}`
5
- * in tests, make Op[].model return the reloaded model!
6
-
7
- # Operation: Internal API
8
-
9
- * don't populate the form in #present context, we don't need it (only the representer goes nuts)
10
- * don't pass contract in validate, we have #contract.
11
- * abstract validate/success/fail into methods to make it easily overrideable.
Binary file
data/doc/trb.jpg DELETED
Binary file
@@ -1,130 +0,0 @@
1
- PATH
2
- remote: ../
3
- specs:
4
- trailblazer (0.2.2)
5
- actionpack (>= 3.0.0)
6
- reform (>= 1.2.0)
7
- uber (>= 0.0.10)
8
-
9
- GEM
10
- remote: http://rubygems.org/
11
- specs:
12
- actionmailer (4.1.1)
13
- actionpack (= 4.1.1)
14
- actionview (= 4.1.1)
15
- mail (~> 2.5.4)
16
- actionpack (4.1.1)
17
- actionview (= 4.1.1)
18
- activesupport (= 4.1.1)
19
- rack (~> 1.5.2)
20
- rack-test (~> 0.6.2)
21
- actionview (4.1.1)
22
- activesupport (= 4.1.1)
23
- builder (~> 3.1)
24
- erubis (~> 2.7.0)
25
- activemodel (4.1.1)
26
- activesupport (= 4.1.1)
27
- builder (~> 3.1)
28
- activerecord (4.1.1)
29
- activemodel (= 4.1.1)
30
- activesupport (= 4.1.1)
31
- arel (~> 5.0.0)
32
- activesupport (4.1.1)
33
- i18n (~> 0.6, >= 0.6.9)
34
- json (~> 1.7, >= 1.7.7)
35
- minitest (~> 5.1)
36
- thread_safe (~> 0.1)
37
- tzinfo (~> 1.1)
38
- arel (5.0.1.20140414130214)
39
- builder (3.2.2)
40
- celluloid (0.16.0)
41
- timers (~> 4.0.0)
42
- connection_pool (2.0.0)
43
- disposable (0.0.9)
44
- representable (~> 2.0)
45
- uber
46
- erubis (2.7.0)
47
- hitimes (1.2.2)
48
- i18n (0.6.11)
49
- json (1.8.1)
50
- mail (2.5.4)
51
- mime-types (~> 1.16)
52
- treetop (~> 1.4.8)
53
- mime-types (1.25.1)
54
- mini_portile (0.6.2)
55
- minitest (5.4.2)
56
- multi_json (1.11.0)
57
- nokogiri (1.6.6.2)
58
- mini_portile (~> 0.6.0)
59
- polyglot (0.3.5)
60
- rack (1.5.2)
61
- rack-test (0.6.2)
62
- rack (>= 1.0)
63
- rails (4.1.1)
64
- actionmailer (= 4.1.1)
65
- actionpack (= 4.1.1)
66
- actionview (= 4.1.1)
67
- activemodel (= 4.1.1)
68
- activerecord (= 4.1.1)
69
- activesupport (= 4.1.1)
70
- bundler (>= 1.3.0, < 2.0)
71
- railties (= 4.1.1)
72
- sprockets-rails (~> 2.0)
73
- railties (4.1.1)
74
- actionpack (= 4.1.1)
75
- activesupport (= 4.1.1)
76
- rake (>= 0.8.7)
77
- thor (>= 0.18.1, < 2.0)
78
- rake (10.3.2)
79
- redis (3.1.0)
80
- redis-namespace (1.5.1)
81
- redis (~> 3.0, >= 3.0.4)
82
- reform (1.2.6)
83
- activemodel
84
- disposable (~> 0.0.5)
85
- representable (~> 2.1.0)
86
- uber (~> 0.0.11)
87
- representable (2.1.8)
88
- multi_json
89
- nokogiri
90
- uber (~> 0.0.7)
91
- responders (1.1.2)
92
- railties (>= 3.2, < 4.2)
93
- sidekiq (3.1.4)
94
- celluloid (>= 0.15.2)
95
- connection_pool (>= 2.0.0)
96
- json
97
- redis (>= 3.0.6)
98
- redis-namespace (>= 1.3.1)
99
- sprockets (3.1.0)
100
- rack (~> 1.0)
101
- sprockets-rails (2.3.1)
102
- actionpack (>= 3.0)
103
- activesupport (>= 3.0)
104
- sprockets (>= 2.8, < 4.0)
105
- sqlite3 (1.3.9)
106
- thor (0.19.1)
107
- thread_safe (0.3.4)
108
- timers (4.0.1)
109
- hitimes
110
- treetop (1.4.15)
111
- polyglot
112
- polyglot (>= 0.3.1)
113
- tzinfo (1.2.2)
114
- thread_safe (~> 0.1)
115
- uber (0.0.13)
116
-
117
- PLATFORMS
118
- ruby
119
-
120
- DEPENDENCIES
121
- activerecord
122
- bundler
123
- minitest
124
- rails
125
- railties
126
- rake
127
- responders
128
- sidekiq (~> 3.1.0)
129
- sqlite3
130
- trailblazer!
@@ -1,6 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gemspec path: '../'
4
-
5
- gem "reform", "~> 2.0.5"
6
- gem "multi_json"
@@ -1,7 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gemspec path: '../'
4
-
5
- gem "reform", github: "apotonick/reform" #"~> 2.1.0.rc1"
6
- gem "roar", github: "apotonick/roar" # "1.1.0"
7
- gem "multi_json"
@@ -1,15 +0,0 @@
1
- Trailblazer.class_eval do
2
- autoload :NotAuthorizedError, "trailblazer/operation/policy"
3
- end
4
-
5
- Trailblazer::Operation.class_eval do
6
- autoload :Controller, "trailblazer/operation/controller"
7
- autoload :Model, "trailblazer/operation/model"
8
- autoload :Collection, "trailblazer/operation/collection"
9
- autoload :Dispatch, "trailblazer/operation/dispatch" # TODO: remove in 1.2.
10
- autoload :Callback, "trailblazer/operation/callback"
11
- autoload :Module, "trailblazer/operation/module"
12
- autoload :Representer,"trailblazer/operation/representer"
13
- autoload :Policy, "trailblazer/operation/policy"
14
- autoload :Resolver, "trailblazer/operation/resolver"
15
- end
@@ -1,31 +0,0 @@
1
- module Trailblazer
2
- # Encapsulates HTTP-specific logic needed before running an operation.
3
- # Right now, all this does is #document_body! which figures out whether or not to pass the request body
4
- # into params, so the operation can use a representer to deserialize the original document.
5
- # To be used in Hanami, Roda, Rails, etc.
6
- class Endpoint
7
- def initialize(operation_class, params, request, options)
8
- @operation_class = operation_class
9
- @params = params
10
- @request = request
11
- @is_document = options[:is_document]
12
- end
13
-
14
- def call
15
- document_body! if @is_document
16
- yield @params# Create.run(params)
17
- end
18
-
19
- private
20
- attr_reader :params, :operation_class, :request
21
-
22
- def document_body!
23
- # this is what happens:
24
- # respond_with Comment::Update::JSON.run(params.merge(comment: request.body.string))
25
- concept_name = operation_class.model_class.to_s.underscore # this could be renamed to ::concept_class soon.
26
- request_body = request.body.respond_to?(:string) ? request.body.string : request.body.read
27
-
28
- params.merge!(concept_name => request_body)
29
- end
30
- end
31
- end
@@ -1,26 +0,0 @@
1
- require "uber/builder"
2
-
3
- # Allows to add builders via ::builds.
4
- module Trailblazer::Operation::Builder
5
- def self.extended(extender)
6
- extender.send(:include, Uber::Builder)
7
- end
8
-
9
- def builder_class
10
- @builders
11
- end
12
-
13
- def builder_class=(constant)
14
- @builders = constant
15
- end
16
-
17
- private
18
- # Runs the builders for this operation class to figure out the actual class.
19
- def build_operation_class(*args)
20
- class_builder(self).(*args) # Uber::Builder::class_builder(context)
21
- end
22
-
23
- def build_operation(params, options={})
24
- build_operation_class(params).new(params, options)
25
- end
26
- end
@@ -1,53 +0,0 @@
1
- require "declarative"
2
- require "disposable/callback"
3
-
4
- module Trailblazer::Operation::Callback
5
- def self.included(base)
6
- base.extend ClassMethods
7
-
8
- base.extend Declarative::Heritage::Inherited
9
- base.extend Declarative::Heritage::DSL
10
- end
11
-
12
- def callback!(name=:default, options={ operation: self, contract: contract, params: @params }) # FIXME: test options.
13
- config = self.class.callbacks.fetch(name) # TODO: test exception
14
- group = config[:group].new(contract)
15
-
16
- options[:context] ||= (config[:context] == :operation ? self : group)
17
- group.(options)
18
-
19
- invocations[name] = group
20
- end
21
-
22
- def dispatch!(*args, &block)
23
- callback!(*args, &block)
24
- end
25
-
26
- def invocations
27
- @invocations ||= {}
28
- end
29
-
30
- module ClassMethods
31
- def callbacks
32
- @callbacks ||= {}
33
- end
34
-
35
- def callback(name=:default, constant=nil, &block)
36
- return callbacks[name][:group] unless constant or block_given?
37
-
38
- add_callback(name, constant, &block)
39
- end
40
-
41
- private
42
- def add_callback(name, constant, &block)
43
- heritage.record(:add_callback, name, constant, &block)
44
-
45
- callbacks[name] ||= {
46
- group: Class.new(constant || Disposable::Callback::Group),
47
- context: constant ? nil : :operation # `context: :operation` when the callback is inline. `context: group` otherwise.
48
- }
49
-
50
- callbacks[name][:group].class_eval(&block) if block_given?
51
- end
52
- end
53
- end
@@ -1,6 +0,0 @@
1
- module Trailblazer::Operation::Collection
2
- # Collection does not produce a contract.
3
- def present
4
- self
5
- end
6
- end
@@ -1,72 +0,0 @@
1
- require "trailblazer/endpoint"
2
-
3
- module Trailblazer::Operation::Controller
4
- private
5
- def form(operation_class, options={})
6
- res, op, options = operation!(operation_class, options)
7
- op.contract.prepopulate!(options) # equals to @form.prepopulate!
8
-
9
- op.contract
10
- end
11
-
12
- # Provides the operation instance, model and contract without running #process.
13
- # Returns the operation.
14
- def present(operation_class, options={})
15
- res, op = operation!(operation_class, options.merge(skip_form: true))
16
- op
17
- end
18
-
19
- def collection(*args)
20
- res, op = operation!(*args)
21
- @collection = op.model
22
- op
23
- end
24
-
25
- def run(operation_class, options={}, &block)
26
- res, op = operation_for!(operation_class, options) { |params| operation_class.run(params) }
27
-
28
- yield op if res and block_given?
29
-
30
- op
31
- end
32
-
33
- # The block passed to #respond is always run, regardless of the validity result.
34
- def respond(operation_class, options={}, &block)
35
- res, op = operation_for!(operation_class, options) { |params| operation_class.run(params) }
36
- namespace = options.delete(:namespace) || []
37
-
38
- return respond_with *namespace, op, options if not block_given?
39
- respond_with *namespace, op, options, &Proc.new { |formats| block.call(op, formats) } if block_given?
40
- end
41
-
42
- private
43
- def operation!(operation_class, options={}) # or #model or #setup.
44
- operation_for!(operation_class, options) { |params| [true, operation_class.present(params)] }
45
- end
46
-
47
- def process_params!(params)
48
- end
49
-
50
- # Override and return arbitrary params object.
51
- def params!(params)
52
- params
53
- end
54
-
55
- # Normalizes parameters and invokes the operation (including its builders).
56
- def operation_for!(operation_class, options, &block)
57
- params = options[:params] || self.params # TODO: test params: parameter properly in all 4 methods.
58
- params = params!(params)
59
- process_params!(params) # deprecate or rename to #setup_params!
60
-
61
- res, op = Trailblazer::Endpoint.new(operation_class, params, request, options).(&block)
62
- setup_operation_instance_variables!(op, options)
63
-
64
- [res, op, options.merge(params: params)]
65
- end
66
-
67
- def setup_operation_instance_variables!(operation, options)
68
- @operation = operation
69
- @model = operation.model
70
- @form = operation.contract unless options[:skip_form]
71
- end
72
- end
@@ -1,3 +0,0 @@
1
- require "trailblazer/operation/callback"
2
- # TODO: deprecate this module.
3
- Trailblazer::Operation::Dispatch = Trailblazer::Operation::Callback
@@ -1,29 +0,0 @@
1
- class Trailblazer::Operation
2
- module Model
3
- # Imports ::model and ::action into an operation.
4
- module DSL
5
- def self.extended(extender)
6
- extender.extend Uber::InheritableAttr
7
- extender.inheritable_attr :config
8
- extender.config = {}
9
- end
10
-
11
- def model(name, action=nil)
12
- self.config[:model] = name
13
- action(action) if action # coolest line ever.
14
- end
15
-
16
- def action(name)
17
- self.config[:action] = name
18
- end
19
-
20
- def action_name # considered private.
21
- self.config[:action] or :create
22
- end
23
-
24
- def model_class # considered private.
25
- self.config[:model] or raise "[Trailblazer] You didn't call Operation::model."
26
- end
27
- end
28
- end
29
- end