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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -6
  3. data/lib/rails/use_case.rb +16 -0
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ede9e2243fddfc8ed59a395ca97b7f14062c4180e27d1f956f793a435559e90
4
- data.tar.gz: 3f56c4fe1f24f74af0226aa5adacbb1341ac042e52b0caf767d61005c8d04b9a
3
+ metadata.gz: c46cd0289d56e4cf86a62f9e1dd780b5fe311c9f7426dfdb942cab5a1b8b8b74
4
+ data.tar.gz: cd02462628e21a6fbda7bed59f237d0b1fdbf1a082d148747404ded5c2b0e365
5
5
  SHA512:
6
- metadata.gz: e21f3ab1cc85466ec37bccfcc1dae363c478bb4d49a15bdcd5b1e093e5d63d270d5be3befccd66f41f0b81cd79015cdcf41f124f9321fa3d968f17300d7ba1a8
7
- data.tar.gz: 34ec8bbeb3e95a0ffce45ef6945d859e71b3122b53c1a01b7d7ad122b9c2d884f681c67a47a355bd0905b4e19ca482defcd7ba572b2aaf209f29920911a53862
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 argument will try to
71
- save that record or raises an exception. Also the `@record` will automatically passed into the outcome.
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 :build_post
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 build_post
93
- @record = BlogPost.new(
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
- case BlogPosts::Create.perform(parameters).code
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('/')
@@ -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.11
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-05 00:00:00.000000000 Z
11
+ date: 2021-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel