rails_use_case 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -6
- data/lib/rails/use_case.rb +16 -0
- 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: c46cd0289d56e4cf86a62f9e1dd780b5fe311c9f7426dfdb942cab5a1b8b8b74
|
4
|
+
data.tar.gz: cd02462628e21a6fbda7bed59f237d0b1fdbf1a082d148747404ded5c2b0e365
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6bd525db88d52a6b9563fed40b80e69d9bc035f115fa77ae6a5668688df7576d5426e4f8caba222986e741b4fa61c9790b75751b582913b009217089998fd8ac
|
7
|
+
data.tar.gz: 2274ca1dfa0312c2bd9f1e2cc8245665bde540b30919c4eefe0875e3d0eba1a3609d2a55c4acac40849e8d237a20bd228ad6aabcaabac8f2c4c50f79cbd9e1bc
|
data/README.md
CHANGED
@@ -67,8 +67,19 @@ The error_code can also passed as first argument to the `failure` step definitio
|
|
67
67
|
|
68
68
|
### Record
|
69
69
|
|
70
|
-
The UseCase should assign the main record to `@record`. Calling `save!` without
|
71
|
-
save that record or raises an exception. Also the
|
70
|
+
The UseCase should assign the main record to `@record`. Calling `save!` without
|
71
|
+
argument will try to save that record or raises an exception. Also the
|
72
|
+
`@record` will automatically passed into the outcome.
|
73
|
+
|
74
|
+
You can either set the `@record` manually or via the `record` method. This comes
|
75
|
+
in two flavors:
|
76
|
+
|
77
|
+
Either passing the name of a param as symbol. Let's assume the UseCase
|
78
|
+
has a parameter called `user` (defined via `attr_accessor`), then you can assign
|
79
|
+
the user to `@record` by adding `record :user` to your use case.
|
80
|
+
|
81
|
+
The alternative way is to pass a block which returns the value for `@record`
|
82
|
+
like in the example UseCase below.
|
72
83
|
|
73
84
|
|
74
85
|
### Example UseCase
|
@@ -81,16 +92,18 @@ class BlogPosts::Create < Rails::UseCase
|
|
81
92
|
validates :content, presence: true
|
82
93
|
validates :author, presence: true
|
83
94
|
|
95
|
+
record { BlogPost.new }
|
96
|
+
|
84
97
|
failure :access_denied, message: 'No permission', unless: -> { author.can_publish_blog_posts? }
|
85
|
-
step :
|
98
|
+
step :assign_attributes
|
86
99
|
step :save!
|
87
100
|
succeed unless: -> { publish }
|
88
101
|
step :publish, do: -> { record.publish! }
|
89
102
|
step :notify_subscribers, unless: -> { skip_notifications }
|
90
103
|
|
91
104
|
|
92
|
-
private def
|
93
|
-
@record
|
105
|
+
private def assign_attributes
|
106
|
+
@record.assign_attributes(
|
94
107
|
title: title,
|
95
108
|
content: content,
|
96
109
|
created_by: author,
|
@@ -154,7 +167,9 @@ class BlogPostsController < ApplicationController
|
|
154
167
|
author: current_user
|
155
168
|
}
|
156
169
|
|
157
|
-
|
170
|
+
outcome = BlogPosts::Create.perform(parameters).code
|
171
|
+
|
172
|
+
case outcome.code
|
158
173
|
when :success then redirect_to(outcome.record)
|
159
174
|
when :access_denied then render(:new, flash: { error: "Access Denied!" })
|
160
175
|
when :foo then redirect_to('/')
|
data/lib/rails/use_case.rb
CHANGED
@@ -67,8 +67,24 @@ module Rails
|
|
67
67
|
end
|
68
68
|
|
69
69
|
|
70
|
+
# DSL to set the record source.
|
71
|
+
# @param [Symbol|nil] Name of the param.
|
72
|
+
# @yields
|
73
|
+
def self.record(param = nil, &block)
|
74
|
+
block = -> { send(param.to_sym) } unless block_given?
|
75
|
+
|
76
|
+
define_method(:determine_record, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
70
80
|
# Will run the steps of the use case.
|
71
81
|
def process
|
82
|
+
@record = determine_record if respond_to?(:determine_record)
|
83
|
+
run_steps
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
def run_steps
|
72
88
|
self.class.steps.each do |step|
|
73
89
|
# Check wether to skip when :if or :unless are set.
|
74
90
|
next if skip_step?(step)
|
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.12
|
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-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|