rails_use_case 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96f24873c4a93a2084c377f236b387605286fd7cbae9915fc53fd85d8f5b07c1
4
- data.tar.gz: 7b2d97ab8d3ac97e3cbe5a9b78ec6f0e1590a8f7692bce3a9b207e5891d40577
3
+ metadata.gz: 7ede9e2243fddfc8ed59a395ca97b7f14062c4180e27d1f956f793a435559e90
4
+ data.tar.gz: 3f56c4fe1f24f74af0226aa5adacbb1341ac042e52b0caf767d61005c8d04b9a
5
5
  SHA512:
6
- metadata.gz: dca166f3929ce6782995814218a95cd9d485f2c83ce4f7cd0282d0aa7e3fc83d58c79a65a70a407c8157883f8d6833c371dfc2b64948ba8c9f74aec368b6fe79
7
- data.tar.gz: 03b6342cdbd9c244a07ffd034d9873fff5baaef554dd69a1da07f406f4e6dff27da095f1210a4914398b7bf1ba55c005586707c9b59c3e35b467857507a90eb6
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 CreateBlogPost < Rails::UseCase
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 = CreateBlogPost.perform(
110
+ result = BlogPosts::Create.perform(
111
111
  title: 'Super Awesome Stuff!',
112
112
  content: 'Lorem Ipsum Dolor Sit Amet',
113
- created_by: current_user,
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
 
@@ -23,7 +23,7 @@ module Rails
23
23
  @record = record
24
24
  @exception = exception
25
25
  @message = message || exception&.message || errors&.full_messages
26
- @code = code&.to_sym
26
+ @code = success ? :success : code&.to_sym
27
27
  end
28
28
  # rubocop:enable Metrics/ParameterLists
29
29
 
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.10
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-04 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel