rails_use_case 0.0.10 → 0.0.11
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/README.md +41 -3
- data/lib/rails/use_case/outcome.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ede9e2243fddfc8ed59a395ca97b7f14062c4180e27d1f956f793a435559e90
|
4
|
+
data.tar.gz: 3f56c4fe1f24f74af0226aa5adacbb1341ac042e52b0caf767d61005c8d04b9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e21f3ab1cc85466ec37bccfcc1dae363c478bb4d49a15bdcd5b1e093e5d63d270d5be3befccd66f41f0b81cd79015cdcf41f124f9321fa3d968f17300d7ba1a8
|
7
|
+
data.tar.gz: 34ec8bbeb3e95a0ffce45ef6945d859e71b3122b53c1a01b7d7ad122b9c2d884f681c67a47a355bd0905b4e19ca482defcd7ba572b2aaf209f29920911a53862
|
data/README.md
CHANGED
@@ -74,7 +74,7 @@ save that record or raises an exception. Also the `@record` will automatically p
|
|
74
74
|
### Example UseCase
|
75
75
|
|
76
76
|
```ruby
|
77
|
-
class
|
77
|
+
class BlogPosts::Create < Rails::UseCase
|
78
78
|
attr_accessor :title, :content, :author, :skip_notifications, :publish
|
79
79
|
|
80
80
|
validates :title, presence: true
|
@@ -107,10 +107,10 @@ end
|
|
107
107
|
Example usage of that UseCase:
|
108
108
|
|
109
109
|
```ruby
|
110
|
-
result =
|
110
|
+
result = BlogPosts::Create.perform(
|
111
111
|
title: 'Super Awesome Stuff!',
|
112
112
|
content: 'Lorem Ipsum Dolor Sit Amet',
|
113
|
-
|
113
|
+
author: current_user,
|
114
114
|
skip_notifications: false
|
115
115
|
)
|
116
116
|
|
@@ -130,6 +130,44 @@ puts result.inspect
|
|
130
130
|
- You can stop the UseCase process with a error message via throwing `Rails::UseCase::Error` exception.
|
131
131
|
|
132
132
|
|
133
|
+
### Working with the result
|
134
|
+
|
135
|
+
The `perform` method of a UseCase returns an outcome object which contains a
|
136
|
+
`code` field with the error code or `:success` otherwise. This comes handy when
|
137
|
+
using in controller actions for example and is a great way to delegate the
|
138
|
+
business logic part of a controller action to the respective UseCase.
|
139
|
+
Everything the controller has to do, is to setup the params and dispatch the
|
140
|
+
result.
|
141
|
+
|
142
|
+
Given the Example above, here is the same call within a controller action with
|
143
|
+
an case statement.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
class BlogPostsController < ApplicationController
|
147
|
+
# ...
|
148
|
+
|
149
|
+
def create
|
150
|
+
parameters = {
|
151
|
+
title: params[:post][:title],
|
152
|
+
content: params[:post][:content],
|
153
|
+
publish: params[:post][:publish],
|
154
|
+
author: current_user
|
155
|
+
}
|
156
|
+
|
157
|
+
case BlogPosts::Create.perform(parameters).code
|
158
|
+
when :success then redirect_to(outcome.record)
|
159
|
+
when :access_denied then render(:new, flash: { error: "Access Denied!" })
|
160
|
+
when :foo then redirect_to('/')
|
161
|
+
else render(:new, flash: { error: outcome.message })
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# ...
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
However this is not rails specific and can be used in any context.
|
170
|
+
|
133
171
|
|
134
172
|
## Behavior
|
135
173
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_use_case
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Klein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-10-
|
11
|
+
date: 2021-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|