interactor_support 1.0.1
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +285 -0
- data/Rakefile +51 -0
- data/interactor_support.gemspec +35 -0
- data/lib/interactor_support/actions.rb +12 -0
- data/lib/interactor_support/concerns/findable.rb +83 -0
- data/lib/interactor_support/concerns/skippable.rb +46 -0
- data/lib/interactor_support/concerns/transactionable.rb +21 -0
- data/lib/interactor_support/concerns/transformable.rb +65 -0
- data/lib/interactor_support/concerns/updatable.rb +57 -0
- data/lib/interactor_support/configuration.rb +18 -0
- data/lib/interactor_support/core.rb +10 -0
- data/lib/interactor_support/request_object.rb +86 -0
- data/lib/interactor_support/rubocop/cop/base_interactor_cop.rb +94 -0
- data/lib/interactor_support/rubocop/cop/require_required_for_interactor_support.rb +29 -0
- data/lib/interactor_support/rubocop/cop/unused_included_modules.rb +67 -0
- data/lib/interactor_support/rubocop/cop/used_unincluded_modules.rb +46 -0
- data/lib/interactor_support/rubocop.rb +2 -0
- data/lib/interactor_support/validations.rb +116 -0
- data/lib/interactor_support/version.rb +5 -0
- data/lib/interactor_support.rb +33 -0
- data/sig/interactor_support.rbs +4 -0
- metadata +73 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 99b9bd6793db476faba16087e24a939982b13974853c2a022e865e011509b143
|
|
4
|
+
data.tar.gz: 6980cdf3351816cbd8209825c03b58b82c195ac21c337d0638223a97c68814f8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ce6d5d43e59d7a70b8fa5e2183da93fc52644de5b12c796cf9b617e5a5eb5d78b32439e884cc40860e1bf2e7da6d1a926b58f9ed3257d2ced5ceb77a00c75715
|
|
7
|
+
data.tar.gz: 940bafa5fbf6d6178b0c8f5f603ff37995c178511aab9cb50d12acc15492a9ace497dbd1e66b76c12fbc3e6943d41d97a9ec557d5451bd3bb6e741e590316194
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
inherit_gem:
|
|
2
|
+
rubocop-shopify: rubocop.yml
|
|
3
|
+
|
|
4
|
+
Style/FrozenStringLiteralComment:
|
|
5
|
+
Enabled: false
|
|
6
|
+
Style/Documentation:
|
|
7
|
+
Enabled: false
|
|
8
|
+
Layout/ArgumentAlignment:
|
|
9
|
+
Enabled: false
|
|
10
|
+
Style/StringLiterals:
|
|
11
|
+
EnforcedStyle: single_quotes
|
|
12
|
+
Enabled: true
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
+
advances of any kind
|
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
+
* Public or private harassment
|
|
25
|
+
* Publishing others' private information, such as a physical or email
|
|
26
|
+
address, without their explicit permission
|
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
+
professional setting
|
|
29
|
+
|
|
30
|
+
## Enforcement Responsibilities
|
|
31
|
+
|
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
+
|
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
+
|
|
36
|
+
## Scope
|
|
37
|
+
|
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
+
|
|
40
|
+
## Enforcement
|
|
41
|
+
|
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at <charliesemailis@gmail.com>. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
+
|
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
+
|
|
46
|
+
## Enforcement Guidelines
|
|
47
|
+
|
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
+
|
|
50
|
+
### 1. Correction
|
|
51
|
+
|
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
+
|
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
+
|
|
56
|
+
### 2. Warning
|
|
57
|
+
|
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
+
|
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
+
|
|
62
|
+
### 3. Temporary Ban
|
|
63
|
+
|
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
+
|
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
+
|
|
68
|
+
### 4. Permanent Ban
|
|
69
|
+
|
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
+
|
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
+
|
|
74
|
+
## Attribution
|
|
75
|
+
|
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
+
available at <https://www.contributor-covenant.org/version/2/0/code_of_conduct.html>.
|
|
78
|
+
|
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
+
|
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
|
82
|
+
|
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
+
<https://www.contributor-covenant.org/faq>. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Charlie Mitchell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# **InteractorSupport** 🚀
|
|
2
|
+
|
|
3
|
+
Make your **Rails interactors** clean, powerful, and less error-prone!
|
|
4
|
+
|
|
5
|
+
[](https://coveralls.io/github/charliemitchell/interactor_support)
|
|
6
|
+
[](https://github.com/charliemitchell/interactor_support/actions/workflows/main.yml)
|
|
7
|
+
[](https://app.codacy.com/gh/charliemitchell/interactor_support/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 🚀 **What Is InteractorSupport?**
|
|
12
|
+
|
|
13
|
+
`InteractorSupport` **extends the Interactor pattern** to make your **business logic more concise, expressive, and robust.**
|
|
14
|
+
|
|
15
|
+
### ✅ **Why Use It?**
|
|
16
|
+
|
|
17
|
+
- **Automatic Validations** – Validate inputs before execution
|
|
18
|
+
- **Data Transformations** – Trim, downcase, and sanitize with ease
|
|
19
|
+
- **Transactional Execution** – Keep data safe with rollback support
|
|
20
|
+
- **Conditional Skipping** – Skip execution based on logic
|
|
21
|
+
- **Auto Record Lookup & Updates** – Reduce boilerplate code
|
|
22
|
+
- **Request Objects** – Lean on **ActiveModel** for structured, validated inputs
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 📦 **Installation**
|
|
27
|
+
|
|
28
|
+
Add to your Gemfile:
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
bundle add interactor_support
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or install manually:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
gem install interactor_support
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
It is reccommended that you use Rails 7.1.x and above with this gem. However, it will work with older versions of rails. You may encounter the issue below when using Rails versions prior to 7.1.
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
<module:LoggerThreadSafeLevel>: uninitialized constant ActiveSupport::LoggerThreadSafeLevel::Logger (NameError)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
In that case, you need to add `require "logger"` as the first line to your `boot.rb file` or pin the concurrent-ruby version to `gem 'concurrent-ruby', '1.3.4'`. If feasible, consider upgrading your Rails application to version 7.1 or newer, where this compatibility issue has been addressed.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 🚦 **Getting Started**
|
|
51
|
+
|
|
52
|
+
### **The Problem: Messy Interactors**
|
|
53
|
+
|
|
54
|
+
Without `InteractorSupport`
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
class UpdateTodoTitle
|
|
58
|
+
include Interactor
|
|
59
|
+
|
|
60
|
+
def call
|
|
61
|
+
todo = Todo.find_by(id: context.todo_id)
|
|
62
|
+
return context.fail!(error: "Todo not found") if todo.nil?
|
|
63
|
+
|
|
64
|
+
todo.update!(title: context.title.strip, completed: context.completed)
|
|
65
|
+
context.todo = todo
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Problems:**
|
|
71
|
+
|
|
72
|
+
❌ Repetitive boilerplate
|
|
73
|
+
❌ Manual failure handling
|
|
74
|
+
❌ No automatic transformations
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
### **The Solution: InteractorSupport**
|
|
79
|
+
|
|
80
|
+
With InteractorSupport, your interactor is now elegant and expressive:
|
|
81
|
+
|
|
82
|
+
```rb
|
|
83
|
+
class UpdateTodoTitle
|
|
84
|
+
include Interactor
|
|
85
|
+
include InteractorSupport
|
|
86
|
+
|
|
87
|
+
requires :todo_id, :title
|
|
88
|
+
transform :title, with: :strip
|
|
89
|
+
find_by :todo, query: { id: :todo_id }, required: true
|
|
90
|
+
update :todo, attributes: { title: :title }
|
|
91
|
+
|
|
92
|
+
def call
|
|
93
|
+
context.message = "Todo updated!"
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**🚀 What changed?**
|
|
99
|
+
|
|
100
|
+
✅ Self documenting validation using `requires`
|
|
101
|
+
✅ Trimmed the title with `transform`
|
|
102
|
+
✅ Automatic record lookup with `find_by`
|
|
103
|
+
✅ Automatic update with `update`
|
|
104
|
+
|
|
105
|
+
### Making it even more powerful
|
|
106
|
+
|
|
107
|
+
```rb
|
|
108
|
+
class CompleteTodo
|
|
109
|
+
include Interactor
|
|
110
|
+
include InteractorSupport
|
|
111
|
+
|
|
112
|
+
transaction
|
|
113
|
+
requires :todo_id
|
|
114
|
+
find_by :todo, query: { id: :todo_id }, required: true
|
|
115
|
+
|
|
116
|
+
update :todo, attributes: {
|
|
117
|
+
completed: true,
|
|
118
|
+
completed_at: -> { Time.current }
|
|
119
|
+
}
|
|
120
|
+
end
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
✅ Wraps the interactor in an active record transaction
|
|
124
|
+
✅ Self documenting validation using `requires`
|
|
125
|
+
✅ Automatic record lookup with `find_by`
|
|
126
|
+
✅ Automatic update with `update` using static values, and a context aware lambda.
|
|
127
|
+
|
|
128
|
+
### Skipping execution when it's not needed
|
|
129
|
+
|
|
130
|
+
```rb
|
|
131
|
+
class CompleteTodo
|
|
132
|
+
include Interactor
|
|
133
|
+
include InteractorSupport
|
|
134
|
+
|
|
135
|
+
transaction
|
|
136
|
+
requires :todo
|
|
137
|
+
skip if: -> { todo.completed? }
|
|
138
|
+
update :todo, attributes: { completed: true, completed_at: -> { Time.current } }
|
|
139
|
+
end
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## 📜 **Request Objects – Lean on Rails Conventions**
|
|
143
|
+
|
|
144
|
+
Instead of raw hashes, **Request Objects** provide **validation, transformation, and structure**.
|
|
145
|
+
|
|
146
|
+
### ✅ **Built on ActiveModel**
|
|
147
|
+
|
|
148
|
+
- Works just like an **ActiveRecord model**
|
|
149
|
+
- Supports **validations** out of the box
|
|
150
|
+
- Automatically **transforms & sanitizes** data
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
class TodoRequest
|
|
154
|
+
include InteractorSupport::RequestObject
|
|
155
|
+
|
|
156
|
+
attribute :title, transform: :strip
|
|
157
|
+
attribute :email, transform: [:strip, :downcase]
|
|
158
|
+
attribute :completed, type: Boolean, default: false
|
|
159
|
+
|
|
160
|
+
validates :title, presence: true
|
|
161
|
+
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
|
162
|
+
end
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## 📖 **Documentation**
|
|
168
|
+
|
|
169
|
+
### **Modules & Features**
|
|
170
|
+
|
|
171
|
+
#### 🔹 **`InteractorSupport::Concerns::Updatable`**
|
|
172
|
+
|
|
173
|
+
Provides the `update` method to **automatically update records** based on context.
|
|
174
|
+
|
|
175
|
+
```rb
|
|
176
|
+
# example incantations
|
|
177
|
+
update :todo, attributes: { title: :title } # -> context.todo.update!(title: context.title)
|
|
178
|
+
update :todo, attributes: { title: :title }, context_key: :updated_todo # -> context.updated_todo = context.todo.update!(title: context.title)
|
|
179
|
+
update :todo, attributes: { request: { title: :title } } # -> context.todo.update!(title: context.request.title)
|
|
180
|
+
update :todo, attributes: { request: [:title, :completed] } # -> context.todo.update!(title: context.request.title, completed: context.request.completed)
|
|
181
|
+
update :todo, attributes: :request # -> context.todo.update!(context.request)
|
|
182
|
+
update :todo, attributes: [:title, :completed] # -> context.todo.update!(title: context.title, completed: context.completed)
|
|
183
|
+
update :todo, attributes: { title: :title, completed: true, completed_at: -> { Time.zone.now } } # -> context.todo.update!(title: context.title, completed: true, completed_at: Time.zone.now)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
#### 🔹 **`InteractorSupport::Concerns::Findable`**
|
|
187
|
+
|
|
188
|
+
Provides `find_by` and `find_where` to **automatically locate records**.
|
|
189
|
+
|
|
190
|
+
```rb
|
|
191
|
+
# example incantations
|
|
192
|
+
|
|
193
|
+
# Genre.find_by(id: context.genre_id)
|
|
194
|
+
find_by :genre, context_key: :current_genre
|
|
195
|
+
|
|
196
|
+
# lambdas are executed within the interactor's context, can be anything needed to compute at runtime
|
|
197
|
+
# Genre.find_by(name: context.name, created_at: context.some_context_value )
|
|
198
|
+
find_by :genre, query: { name: :name, created_at: -> { some_context_value } }
|
|
199
|
+
find_by :genre, query: { name: :name, created_at: -> { 7.days.ago...1.day.ago } }
|
|
200
|
+
|
|
201
|
+
# be careful here, this is not advisable.
|
|
202
|
+
find_by :genre, query: { name: :name, created_at: 7.days.ago...1.day.ago }
|
|
203
|
+
|
|
204
|
+
# Genre.find_by(name: context.name)
|
|
205
|
+
find_by :genre, query: { name: :name }
|
|
206
|
+
|
|
207
|
+
# context.current_genre = Genre.find_by(id: context.genre_id)
|
|
208
|
+
find_by :genre, context_key: :current_genre
|
|
209
|
+
|
|
210
|
+
# Genre.find_by(id: context.genre_id), fails the context if the result is nil
|
|
211
|
+
find_by :genre, required: true
|
|
212
|
+
|
|
213
|
+
# find_where
|
|
214
|
+
# Post.where(user_id: context.user_id)
|
|
215
|
+
find_where :post, where: { user_id: :user_id }
|
|
216
|
+
|
|
217
|
+
# Same as above, but will fail the context if the results are empty
|
|
218
|
+
find_where :post, where: { user_id: :user_id }, required: true
|
|
219
|
+
|
|
220
|
+
# lambdas are executed within the interactor's context
|
|
221
|
+
# Post.where(user_id: context.user_id, created_at: context.some_context_value )
|
|
222
|
+
find_where :post, where: { user_id: :user_id, created_at: -> { some_context_value } }
|
|
223
|
+
|
|
224
|
+
# Post.where(user_id: context.user_id).where.not(active: false)
|
|
225
|
+
find_where :post, where: { user_id: :user_id }, where_not: { active: false }
|
|
226
|
+
|
|
227
|
+
# Post.active.where(user_id: context.user_id)
|
|
228
|
+
find_where :post, where: { user_id: :user_id }, scope: :active
|
|
229
|
+
|
|
230
|
+
# context.user_posts = Post.where(user_id: context.user_id)
|
|
231
|
+
find_where :post, where: { user_id: :user_id }, context_key: :user_posts
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### 🔹 **`InteractorSupport::Concerns::Transformable`**
|
|
235
|
+
|
|
236
|
+
Provides `transform` to **sanitize and normalize inputs**.
|
|
237
|
+
|
|
238
|
+
#### 🔹 **`InteractorSupport::Concerns::Skippable`**
|
|
239
|
+
|
|
240
|
+
Allows an interactor to **skip execution** if a condition is met.
|
|
241
|
+
|
|
242
|
+
#### 🔹 **`InteractorSupport::Validations`**
|
|
243
|
+
|
|
244
|
+
Provides **automatic input validation** before execution. This includes `ActiveModel::Validations` and
|
|
245
|
+
`ActiveModel::Validations::Callbacks`, and adds a few extra methods.
|
|
246
|
+
|
|
247
|
+
| method | description |
|
|
248
|
+
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
249
|
+
| required | a self documenting helper method that registers the attribute as an accessor, applies any active model validations passed to it. If the attribute is missing, it will fail the context |
|
|
250
|
+
| optional | a self documenting helper method that registers the attribute as an accessor, applies any active model validations passed to it using `:if_assigned` |
|
|
251
|
+
| validates_after | An after validator to ensure context consistancy. |
|
|
252
|
+
| validates_before | will likely be deprecated in the future. |
|
|
253
|
+
|
|
254
|
+
```rb
|
|
255
|
+
class CreateUser
|
|
256
|
+
include Interactor
|
|
257
|
+
include InteractorSupport
|
|
258
|
+
|
|
259
|
+
required email: { format: { with: URI::MailTo::EMAIL_REGEXP } },
|
|
260
|
+
password: { length: { minimum: 6 } }
|
|
261
|
+
|
|
262
|
+
optional age: { numericality: { greater_than: 18 } }
|
|
263
|
+
validates_after :user, persisted: true
|
|
264
|
+
end
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
✅ email must be present and match a valid format
|
|
268
|
+
✅ password must be present and at least 6 characters long
|
|
269
|
+
✅ age is optional but must be greater than 18 if provided
|
|
270
|
+
|
|
271
|
+
If any validation fails, context.fail!(errors: errors.full_messages) will automatically halt execution.
|
|
272
|
+
|
|
273
|
+
#### 🔹 **`InteractorSupport::RequestObject`**
|
|
274
|
+
|
|
275
|
+
Provides structured, validated request objects based on **ActiveModel**.
|
|
276
|
+
|
|
277
|
+
## 🤝 **Contributing**
|
|
278
|
+
|
|
279
|
+
Pull requests are welcome on [GitHub](https://github.com/charliemitchell/interactor_support).
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## 📜 **License**
|
|
284
|
+
|
|
285
|
+
Released under the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# filepath: ./Rakefile
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/gem_tasks"
|
|
5
|
+
require "rails"
|
|
6
|
+
require "rspec/core/rake_task"
|
|
7
|
+
require "rubocop/rake_task"
|
|
8
|
+
require 'active_record'
|
|
9
|
+
require 'rake'
|
|
10
|
+
require 'yaml'
|
|
11
|
+
|
|
12
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
13
|
+
RuboCop::RakeTask.new
|
|
14
|
+
|
|
15
|
+
task default: %i[spec rubocop]
|
|
16
|
+
|
|
17
|
+
# Load the Rails environment
|
|
18
|
+
ENV['RAILS_ENV'] ||= 'test'
|
|
19
|
+
db_config = YAML.load_file(File.expand_path('spec/database.yml', __dir__))
|
|
20
|
+
ActiveRecord::Base.configurations = db_config
|
|
21
|
+
ActiveRecord::Base.establish_connection(:test)
|
|
22
|
+
|
|
23
|
+
# Define the migration directory
|
|
24
|
+
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [File.expand_path('spec/migrations', __dir__)]
|
|
25
|
+
|
|
26
|
+
# Load the ActiveRecord tasks manually
|
|
27
|
+
namespace :db do
|
|
28
|
+
desc "Migrate the database"
|
|
29
|
+
task :migrate do
|
|
30
|
+
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
|
31
|
+
ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths).migrate
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
desc "Create the database"
|
|
35
|
+
task :create do
|
|
36
|
+
ActiveRecord::Tasks::DatabaseTasks.create_current
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
desc "Drop the database"
|
|
40
|
+
task :drop do
|
|
41
|
+
ActiveRecord::Tasks::DatabaseTasks.drop_current
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
desc "Reset the database"
|
|
45
|
+
task :reset => [:drop, :create, :migrate]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Load the Rake tasks
|
|
49
|
+
Rake::Task.define_task(:environment) do
|
|
50
|
+
ActiveRecord::Base.establish_connection(:test)
|
|
51
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/interactor_support/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'interactor_support'
|
|
7
|
+
spec.version = InteractorSupport::VERSION
|
|
8
|
+
spec.authors = ['Charlie Mitchell']
|
|
9
|
+
spec.email = ['charliesemailis@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'A collection of support classes for Interactor.'
|
|
12
|
+
# spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
|
13
|
+
spec.license = 'MIT'
|
|
14
|
+
spec.required_ruby_version = '>= 2.6.0'
|
|
15
|
+
|
|
16
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
17
|
+
spec.metadata['homepage_uri'] = 'https://github.com/charliemitchell/interactor_support'
|
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/charliemitchell/interactor_support.git'
|
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/charliemitchell/interactor_support/blob/master/CHANGELOG.md'
|
|
20
|
+
|
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
|
24
|
+
%x(git ls-files -z).split("\x0").reject do |f|
|
|
25
|
+
(File.expand_path(f) == __FILE__) ||
|
|
26
|
+
f.start_with?('bin/', 'test/', 'spec/', 'features/', '.git', '.circleci', 'appveyor', 'Gemfile')
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
spec.bindir = 'exe'
|
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
31
|
+
spec.require_paths = ['lib']
|
|
32
|
+
|
|
33
|
+
# For more information and examples about making a new gem, check out our
|
|
34
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
35
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module InteractorSupport
|
|
2
|
+
module Actions
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
included do
|
|
5
|
+
include InteractorSupport::Concerns::Skippable
|
|
6
|
+
include InteractorSupport::Concerns::Transactionable
|
|
7
|
+
include InteractorSupport::Concerns::Updatable
|
|
8
|
+
include InteractorSupport::Concerns::Findable
|
|
9
|
+
include InteractorSupport::Concerns::Transformable
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module InteractorSupport
|
|
2
|
+
module Concerns
|
|
3
|
+
module Findable
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
include InteractorSupport::Core
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
class << self
|
|
9
|
+
# This method finds a single record using query parameters.
|
|
10
|
+
# Supports symbols (context keys) and lambdas for dynamic values.
|
|
11
|
+
#
|
|
12
|
+
# Examples:
|
|
13
|
+
# find_by :post, query: { slug: :slug }, context_key: :current_post
|
|
14
|
+
# find_by :post, query: { id: :post_id, published: true }
|
|
15
|
+
# find_by :post, query: { created_at: -> { 1.week.ago..Time.current } }
|
|
16
|
+
def find_by(model, query: {}, context_key: nil, required: false)
|
|
17
|
+
context_key ||= model
|
|
18
|
+
before do
|
|
19
|
+
record =
|
|
20
|
+
if query.empty?
|
|
21
|
+
id = context["#{model}_id".to_sym]
|
|
22
|
+
model.to_s.classify.constantize.find_by(id: id)
|
|
23
|
+
else
|
|
24
|
+
model.to_s.classify.constantize.find_by(
|
|
25
|
+
query.transform_values do |v|
|
|
26
|
+
case v
|
|
27
|
+
when Symbol then context[v] # Lookup symbol in context
|
|
28
|
+
when Proc then instance_exec(&v) # Evaluate lambda dynamically
|
|
29
|
+
else v # Use raw value
|
|
30
|
+
end
|
|
31
|
+
end,
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
context[context_key] = record
|
|
36
|
+
context.fail!(errors: ["#{model} not found"]) if required && record.nil?
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# This method finds multiple records using where conditions.
|
|
41
|
+
# Supports symbols (context keys) and lambdas for dynamic values.
|
|
42
|
+
#
|
|
43
|
+
# Examples:
|
|
44
|
+
# find_where :post, where: { user_id: :user_id }
|
|
45
|
+
# find_where :post, where: { created_at: -> { 5.days.ago..Time.current } }
|
|
46
|
+
# find_where :post, where: { user_id: :user_id }, where_not: { active: false }
|
|
47
|
+
# find_where :post, where: { user_id: :user_id }, scope: :active
|
|
48
|
+
def find_where(model, where: {}, where_not: {}, scope: nil, context_key: nil, required: false)
|
|
49
|
+
context_key ||= model.to_s.pluralize.to_sym
|
|
50
|
+
before do
|
|
51
|
+
query = model.to_s.classify.constantize
|
|
52
|
+
|
|
53
|
+
query = query.where(
|
|
54
|
+
where.transform_values do |v|
|
|
55
|
+
case v
|
|
56
|
+
when Symbol then context[v] # Lookup symbol in context
|
|
57
|
+
when Proc then instance_exec(&v) # Evaluate lambda dynamically
|
|
58
|
+
else v # Use raw value
|
|
59
|
+
end
|
|
60
|
+
end,
|
|
61
|
+
) if where.present?
|
|
62
|
+
|
|
63
|
+
query = query.where.not(
|
|
64
|
+
where_not.transform_values do |v|
|
|
65
|
+
case v
|
|
66
|
+
when Symbol then context[v]
|
|
67
|
+
when Proc then instance_exec(&v)
|
|
68
|
+
else v
|
|
69
|
+
end
|
|
70
|
+
end,
|
|
71
|
+
) if where_not.present?
|
|
72
|
+
|
|
73
|
+
query = query.send(scope) if scope.present?
|
|
74
|
+
|
|
75
|
+
context[context_key] = query
|
|
76
|
+
context.fail!(errors: ["no #{model}s were found"]) if required && query.empty?
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module InteractorSupport
|
|
2
|
+
module Concerns
|
|
3
|
+
module Skippable
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
include InteractorSupport::Core
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
class << self
|
|
9
|
+
def skip(**options)
|
|
10
|
+
around do |interactor|
|
|
11
|
+
unless options[:if].nil?
|
|
12
|
+
condition = if options[:if].is_a?(Proc)
|
|
13
|
+
context.instance_exec(&options[:if])
|
|
14
|
+
elsif options[:if].is_a?(Symbol) && respond_to?(options[:if])
|
|
15
|
+
send(options[:if])
|
|
16
|
+
elsif options[:if].is_a?(Symbol)
|
|
17
|
+
context[options[:if]]
|
|
18
|
+
else
|
|
19
|
+
options[:if]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
next if condition
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
unless options[:unless].nil?
|
|
26
|
+
condition = if options[:unless].is_a?(Proc)
|
|
27
|
+
context.instance_exec(&options[:unless])
|
|
28
|
+
elsif options[:unless].is_a?(Symbol) && respond_to?(options[:unless])
|
|
29
|
+
send(options[:unless])
|
|
30
|
+
elsif options[:unless].is_a?(Symbol)
|
|
31
|
+
context[options[:unless]]
|
|
32
|
+
else
|
|
33
|
+
options[:unless]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
next unless condition
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
interactor.call
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module InteractorSupport
|
|
2
|
+
module Concerns
|
|
3
|
+
module Transactionable
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
include InteractorSupport::Core
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
class << self
|
|
9
|
+
def transaction(isolation: nil, joinable: true, requires_new: false)
|
|
10
|
+
around do |interactor|
|
|
11
|
+
ActiveRecord::Base.transaction(isolation: isolation, joinable: joinable, requires_new: requires_new) do
|
|
12
|
+
interactor.call
|
|
13
|
+
raise ActiveRecord::Rollback if context.failure?
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|