rails_use_case 0.0.11 → 0.0.13
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 +26 -9
- data/lib/rails/service.rb +2 -2
- data/lib/rails/use_case.rb +17 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0eb1a5ed5601e1c4e070962f4d19c2e35a4ea23980151fa35ec02c6328a2fd0d
|
4
|
+
data.tar.gz: 20821b8d657c535ee16ae2c920e1e93a8d0b21c384d99b25e5c986ff0914b0eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64c64c919a75d87bdea7a339fd9322c33b97a30b46bfa668a3eb970b3e542af9416176e5cc9783a0f133349648beb022983d66cafd30473f77708eb0de6a9963
|
7
|
+
data.tar.gz: 9f26874c317b868bd84c6232a2af3562bf54feddb4851552fbbccdc8294e38bdd2a89b420cdf387f623d1f5e361de9d24232a46b77d1dec13fb885a8bb1ab99d
|
data/README.md
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
# Rails Use Case gem
|
2
2
|
|
3
|
-
|
3
|
+
This gem introduces UseCases and Services to Rails which allow you to keep your Models and Controllers slim.
|
4
4
|
|
5
5
|
Read more: https://dev.to/phortx/pimp-your-rails-application-32d0
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
Clean Architecture suggests to put the business logic in UseCases which are classes, that contain reusable high
|
8
|
+
level business logic which otherwise would normally be located in the controller. UseCases are easy to read,
|
9
|
+
clean, reusable, testable and extendable.
|
10
|
+
Examples are: Place an item in the cart, create a new user or delete a comment.
|
9
11
|
|
10
12
|
The purpose of a Service is to contain low level non-domain code like communication with a API,
|
11
13
|
generating an export, upload via FTP or generating a PDF.
|
@@ -67,8 +69,19 @@ The error_code can also passed as first argument to the `failure` step definitio
|
|
67
69
|
|
68
70
|
### Record
|
69
71
|
|
70
|
-
The UseCase should assign the main record to `@record`. Calling `save!` without
|
71
|
-
save that record or raises an exception. Also the
|
72
|
+
The UseCase should assign the main record to `@record`. Calling `save!` without
|
73
|
+
argument will try to save that record or raises an exception. Also the
|
74
|
+
`@record` will automatically passed into the outcome.
|
75
|
+
|
76
|
+
You can either set the `@record` manually or via the `record` method. This comes
|
77
|
+
in two flavors:
|
78
|
+
|
79
|
+
Either passing the name of a param as symbol. Let's assume the UseCase
|
80
|
+
has a parameter called `user` (defined via `attr_accessor`), then you can assign
|
81
|
+
the user to `@record` by adding `record :user` to your use case.
|
82
|
+
|
83
|
+
The alternative way is to pass a block which returns the value for `@record`
|
84
|
+
like in the example UseCase below.
|
72
85
|
|
73
86
|
|
74
87
|
### Example UseCase
|
@@ -81,16 +94,18 @@ class BlogPosts::Create < Rails::UseCase
|
|
81
94
|
validates :content, presence: true
|
82
95
|
validates :author, presence: true
|
83
96
|
|
97
|
+
record { BlogPost.new }
|
98
|
+
|
84
99
|
failure :access_denied, message: 'No permission', unless: -> { author.can_publish_blog_posts? }
|
85
|
-
step :
|
100
|
+
step :assign_attributes
|
86
101
|
step :save!
|
87
102
|
succeed unless: -> { publish }
|
88
103
|
step :publish, do: -> { record.publish! }
|
89
104
|
step :notify_subscribers, unless: -> { skip_notifications }
|
90
105
|
|
91
106
|
|
92
|
-
private def
|
93
|
-
@record
|
107
|
+
private def assign_attributes
|
108
|
+
@record.assign_attributes(
|
94
109
|
title: title,
|
95
110
|
content: content,
|
96
111
|
created_by: author,
|
@@ -154,7 +169,9 @@ class BlogPostsController < ApplicationController
|
|
154
169
|
author: current_user
|
155
170
|
}
|
156
171
|
|
157
|
-
|
172
|
+
outcome = BlogPosts::Create.perform(parameters).code
|
173
|
+
|
174
|
+
case outcome.code
|
158
175
|
when :success then redirect_to(outcome.record)
|
159
176
|
when :access_denied then render(:new, flash: { error: "Access Denied!" })
|
160
177
|
when :foo then redirect_to('/')
|
data/lib/rails/service.rb
CHANGED
@@ -38,7 +38,7 @@ module Rails
|
|
38
38
|
#
|
39
39
|
# @raise [RuntimeError] When no service_name is given
|
40
40
|
def initialize(service_name = nil)
|
41
|
-
raise NotImplementedError if
|
41
|
+
raise NotImplementedError if instance_of?(Service)
|
42
42
|
raise 'Please provide a service name!' if service_name.nil?
|
43
43
|
|
44
44
|
@service_name = service_name
|
@@ -67,7 +67,7 @@ module Rails
|
|
67
67
|
# Will setup the logger for logging to STDOUT. This can be useful for
|
68
68
|
# Heroku for example.
|
69
69
|
private def setup_stdout_logger
|
70
|
-
@logger = Logger.new(
|
70
|
+
@logger = Logger.new($stdout)
|
71
71
|
|
72
72
|
@logger.formatter = proc do |severity, datetime, progname, msg|
|
73
73
|
"[#{@service_name}] #{msg}"
|
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)
|
@@ -82,7 +98,7 @@ module Rails
|
|
82
98
|
fail!(code: opts[:code], message: opts[:message]) if name == :failure
|
83
99
|
|
84
100
|
# Run the lambda, when :do is set. Otherwise call the method.
|
85
|
-
next if opts[:do] ?
|
101
|
+
next if opts[:do] ? instance_exec(&opts[:do]) : send(name)
|
86
102
|
|
87
103
|
# result is false, so we have a failure.
|
88
104
|
fail! code: :step_false, message: "Step '#{name}' returned false"
|
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.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Klein
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -195,7 +195,7 @@ homepage: https://github.com/phortx/rails-use-case
|
|
195
195
|
licenses:
|
196
196
|
- MIT
|
197
197
|
metadata: {}
|
198
|
-
post_install_message:
|
198
|
+
post_install_message:
|
199
199
|
rdoc_options: []
|
200
200
|
require_paths:
|
201
201
|
- lib
|
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
211
211
|
version: '0'
|
212
212
|
requirements: []
|
213
213
|
rubygems_version: 3.1.2
|
214
|
-
signing_key:
|
214
|
+
signing_key:
|
215
215
|
specification_version: 4
|
216
216
|
summary: Rails UseCase and Service classes
|
217
217
|
test_files: []
|