active_job-performs 0.3.1 → 0.3.2
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/Gemfile.lock +15 -15
- data/README.md +85 -10
- data/lib/active_job/performs/version.rb +1 -1
- data/lib/active_job/performs.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 566acd14649609a2e16b658a93fe276f80609866c61fb37c11e720b581bc0f76
|
|
4
|
+
data.tar.gz: 5174b18e562c53966639bb16e3d729885d99f1ebe01163175a1513dded11cbff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f2fd767c6bd1c66d26a046e84dea4cd2ea0bfdb0ec50452699151e90fba9856b14875ab1a98371cf2727423a31871db4af432a965784a9cf61d92b58917ce0d2
|
|
7
|
+
data.tar.gz: cd9628dce5378c14ecae35da1fc83ae4487ea0ece02a5e4a78226572778ca7d370c5975e99ff880fa9bf030f026c244d1d3cf2abac838fb7d1bec373f7327cbf
|
data/Gemfile.lock
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
active_job-performs (0.3.
|
|
4
|
+
active_job-performs (0.3.2)
|
|
5
5
|
activejob (>= 6.1)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
activejob (7.1.3.
|
|
11
|
-
activesupport (= 7.1.3.
|
|
10
|
+
activejob (7.1.3.4)
|
|
11
|
+
activesupport (= 7.1.3.4)
|
|
12
12
|
globalid (>= 0.3.6)
|
|
13
|
-
activemodel (7.1.3.
|
|
14
|
-
activesupport (= 7.1.3.
|
|
15
|
-
activerecord (7.1.3.
|
|
16
|
-
activemodel (= 7.1.3.
|
|
17
|
-
activesupport (= 7.1.3.
|
|
13
|
+
activemodel (7.1.3.4)
|
|
14
|
+
activesupport (= 7.1.3.4)
|
|
15
|
+
activerecord (7.1.3.4)
|
|
16
|
+
activemodel (= 7.1.3.4)
|
|
17
|
+
activesupport (= 7.1.3.4)
|
|
18
18
|
timeout (>= 0.4.0)
|
|
19
|
-
activesupport (7.1.3.
|
|
19
|
+
activesupport (7.1.3.4)
|
|
20
20
|
base64
|
|
21
21
|
bigdecimal
|
|
22
22
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
@@ -28,7 +28,7 @@ GEM
|
|
|
28
28
|
tzinfo (~> 2.0)
|
|
29
29
|
base64 (0.2.0)
|
|
30
30
|
bigdecimal (3.1.8)
|
|
31
|
-
concurrent-ruby (1.
|
|
31
|
+
concurrent-ruby (1.3.3)
|
|
32
32
|
connection_pool (2.4.1)
|
|
33
33
|
debug (1.9.2)
|
|
34
34
|
irb (~> 1.10)
|
|
@@ -39,10 +39,10 @@ GEM
|
|
|
39
39
|
i18n (1.14.5)
|
|
40
40
|
concurrent-ruby (~> 1.0)
|
|
41
41
|
io-console (0.7.2)
|
|
42
|
-
irb (1.13.
|
|
42
|
+
irb (1.13.2)
|
|
43
43
|
rdoc (>= 4.0.0)
|
|
44
44
|
reline (>= 0.4.2)
|
|
45
|
-
minitest (5.
|
|
45
|
+
minitest (5.24.0)
|
|
46
46
|
minitest-sprint (1.2.2)
|
|
47
47
|
path_expander (~> 1.1)
|
|
48
48
|
mutex_m (0.2.0)
|
|
@@ -52,11 +52,11 @@ GEM
|
|
|
52
52
|
rake (13.2.1)
|
|
53
53
|
rdoc (6.7.0)
|
|
54
54
|
psych (>= 4.0.0)
|
|
55
|
-
reline (0.5.
|
|
55
|
+
reline (0.5.9)
|
|
56
56
|
io-console (~> 0.5)
|
|
57
57
|
sqlite3 (1.7.3-arm64-darwin)
|
|
58
58
|
sqlite3 (1.7.3-x86_64-linux)
|
|
59
|
-
stringio (3.1.
|
|
59
|
+
stringio (3.1.1)
|
|
60
60
|
timeout (0.4.1)
|
|
61
61
|
tzinfo (2.0.6)
|
|
62
62
|
concurrent-ruby (~> 1.0)
|
|
@@ -76,4 +76,4 @@ DEPENDENCIES
|
|
|
76
76
|
sqlite3 (~> 1.4)
|
|
77
77
|
|
|
78
78
|
BUNDLED WITH
|
|
79
|
-
2.5.
|
|
79
|
+
2.5.14
|
data/README.md
CHANGED
|
@@ -1,8 +1,80 @@
|
|
|
1
1
|
# ActiveJob::Performs
|
|
2
2
|
|
|
3
|
-
`ActiveJob::Performs` adds
|
|
3
|
+
`ActiveJob::Performs` adds a `performs` class method to make the model + job loop vastly more conventional. You use it like this:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```ruby
|
|
6
|
+
class Post < ApplicationRecord
|
|
7
|
+
performs :publish
|
|
8
|
+
# Or `performs def publish`!
|
|
9
|
+
|
|
10
|
+
def publish
|
|
11
|
+
# Some logic to publish a post
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Then we build a job for the instance method and define a `post.publish_later` instance method, and more:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
class Post < ApplicationRecord
|
|
20
|
+
class Job < ApplicationJob; end # We build a general Job class to share configuration between method jobs.
|
|
21
|
+
|
|
22
|
+
# Individual method jobs inherit from the `Post::Job` defined above.
|
|
23
|
+
class PublishJob < Job
|
|
24
|
+
# We generate the required `perform` method passing in the `post` and calling `publish` on it.
|
|
25
|
+
def perform(post, *, **) = post.publish(*, **)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# On Rails 7.1, where `ActiveJob.perform_all_later` exists, we also generate
|
|
29
|
+
# a bulk method to enqueue many jobs at once. So you can do this:
|
|
30
|
+
#
|
|
31
|
+
# Post.unpublished.in_batches.each(&:publish_later_bulk)
|
|
32
|
+
def self.publish_later_bulk
|
|
33
|
+
ActiveJob.perform_all_later all.map { PublishJob.new(_1) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# We generate `publish_later` to wrap the job execution forwarding arguments and options.
|
|
37
|
+
def publish_later(*, **) = PublishJob.perform_later(self, *, **)
|
|
38
|
+
|
|
39
|
+
def publish
|
|
40
|
+
# Some logic to publish a post.
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Benefits
|
|
46
|
+
|
|
47
|
+
1. Conventional Jobs: they'll now mostly call instance methods like `publish_later` -> `publish`.
|
|
48
|
+
1. Follows Rails' internal conventions: this borrows from `ActionMailbox::InboundEmail#process_later` calling `process` and `ActionMailer::Base#deliver_later` calling `deliver`.
|
|
49
|
+
1. Clarity & less guess work: the `_later` methods standardize how you call jobs throughout your app, so you can instantly tell what's happening.
|
|
50
|
+
1. Less tedium: getting an instance method run in the background is just now a `performs` call with some potential configuration.
|
|
51
|
+
1. Fewer files to manage: you don't have to dig up something in `app/jobs` just to learn almost nothing from the boilerplate in there.
|
|
52
|
+
1. Remaining jobs stand out: `app/jobs` is way lighter, so any jobs in there that don't fit the `performs` pattern now stand out way more.
|
|
53
|
+
1. More consolidated logic: sometimes Job classes house model-level logic, but now it's all the way out in `app/jobs` instead of `app/models`, huh?
|
|
54
|
+
|
|
55
|
+
> [!TIP]
|
|
56
|
+
> On that last point, `performs` does put more logic back within your Active Records, so if you need further encapsulation to prevent them growing too large, consider checking out [active_record-associated_object](https://github.com/kaspth/active_record-associated_object).
|
|
57
|
+
|
|
58
|
+
### Praise from people
|
|
59
|
+
|
|
60
|
+
Here's what [@claudiob](https://github.com/claudiob) had to say after using `ActiveJob::Performs`:
|
|
61
|
+
|
|
62
|
+
> I’ve been using active_job-performs for the last month and I love it love it love it!!
|
|
63
|
+
>
|
|
64
|
+
> Your thought process behind it is so thorough. I have a bunch of jobs now attached to models and my app/jobs folder… is empty!!
|
|
65
|
+
>
|
|
66
|
+
> This saves me a lot of mental hoops, I don’t have to switch between files anymore, everything is self-contained. Thank you!!!
|
|
67
|
+
|
|
68
|
+
From [@andycroll](https://github.com/andycroll) in a [writeup](https://andycroll.com/ruby/launching-usingrails) about launching [UsingRails](https://usingrails.com):
|
|
69
|
+
|
|
70
|
+
> I’ve also adopted a couple of gems—with exceptional Rails-level taste and author pedigree—that I hadn’t used in anger before, including `active_job-performs` from Kasper […]. Would recommend both.
|
|
71
|
+
|
|
72
|
+
And [@nshki](https://github.com/nshki) after trying it:
|
|
73
|
+
|
|
74
|
+
> Spent some time playing with [@kaspth](https://github.com/kaspth)'s [`ActiveRecord::AssociatedObject`](https://github.com/kaspth/active_record-associated_object) and `ActiveJob::Performs` and wow! The conventions these gems put in place help simplify a codebase drastically. I particularly love `ActiveJob::Performs`—it helped me refactor out all `ApplicationJob` classes I had and keep important context in the right domain model.
|
|
75
|
+
|
|
76
|
+
## Usage
|
|
77
|
+
### with `ActiveRecord::Base` & other `GlobalID::Identification` objects
|
|
6
78
|
|
|
7
79
|
`ActiveJob::Performs` works with any object that has `include GlobalID::Identification` and responds to that interface.
|
|
8
80
|
|
|
@@ -46,6 +118,10 @@ class Post < ActiveRecord::Base
|
|
|
46
118
|
# a bulk method to enqueue many jobs at once. So you can do this:
|
|
47
119
|
#
|
|
48
120
|
# Post.unpublished.in_batches.each(&:publish_later_bulk)
|
|
121
|
+
#
|
|
122
|
+
# Or pass in a subset of posts as an argument:
|
|
123
|
+
#
|
|
124
|
+
# Post.publish_later_bulk Post.unpublished
|
|
49
125
|
def self.publish_later_bulk
|
|
50
126
|
ActiveJob.perform_all_later all.map { PublishJob.new(_1) }
|
|
51
127
|
end
|
|
@@ -61,7 +137,7 @@ class Post < ActiveRecord::Base
|
|
|
61
137
|
end
|
|
62
138
|
```
|
|
63
139
|
|
|
64
|
-
We generate the `Post::Job` class above to share configuration between method level jobs. E.g. if you had a retract method that was setup very similar, you could do:
|
|
140
|
+
We generate the `Post::Job` class above to share configuration between method level jobs. E.g. if you had a `retract` method that was setup very similar, you could do:
|
|
65
141
|
|
|
66
142
|
```ruby
|
|
67
143
|
class Post < ActiveRecord::Base
|
|
@@ -184,7 +260,7 @@ end
|
|
|
184
260
|
|
|
185
261
|
### Usage with `ActiveRecord::AssociatedObject`
|
|
186
262
|
|
|
187
|
-
The [`ActiveRecord::AssociatedObject`](https://github.com/kaspth/active_record-associated_object) gem also implements `GlobalID::Identification`, so you
|
|
263
|
+
The [`ActiveRecord::AssociatedObject`](https://github.com/kaspth/active_record-associated_object) gem also implements `GlobalID::Identification`, so you use `performs` exactly like you would on Active Records:
|
|
188
264
|
|
|
189
265
|
```ruby
|
|
190
266
|
class Post::Publisher < ActiveRecord::AssociatedObject
|
|
@@ -204,6 +280,11 @@ class Post::Publisher < ActiveRecord::AssociatedObject
|
|
|
204
280
|
end
|
|
205
281
|
```
|
|
206
282
|
|
|
283
|
+
> [!NOTE]
|
|
284
|
+
> There's one difference with Active Record: you must pass in a set to `_later_bulk` methods. Like so:
|
|
285
|
+
>
|
|
286
|
+
> `Post::Publisher.publish_later_bulk Post::Publisher.first(10)`
|
|
287
|
+
|
|
207
288
|
### Passing `wait` to `performs`
|
|
208
289
|
|
|
209
290
|
If there's a job you want to defer, `performs` can set it for each invocation:
|
|
@@ -230,12 +311,6 @@ class Post < ActiveRecord::Base
|
|
|
230
311
|
end
|
|
231
312
|
```
|
|
232
313
|
|
|
233
|
-
### Praise from people
|
|
234
|
-
|
|
235
|
-
Here's what [@nshki](https://github.com/nshki) found when they tried `ActiveJob::Performs`:
|
|
236
|
-
|
|
237
|
-
> Spent some time playing with [@kaspth](https://github.com/kaspth)'s [`ActiveRecord::AssociatedObject`](https://github.com/kaspth/active_record-associated_object) and `ActiveJob::Performs` and wow! The conventions these gems put in place help simplify a codebase drastically. I particularly love `ActiveJob::Performs`—it helped me refactor out all `ApplicationJob` classes I had and keep important context in the right domain model.
|
|
238
|
-
|
|
239
314
|
## Installation
|
|
240
315
|
|
|
241
316
|
Install the gem and add to the application's Gemfile by executing:
|
data/lib/active_job/performs.rb
CHANGED
|
@@ -46,10 +46,10 @@ module ActiveJob::Performs
|
|
|
46
46
|
end
|
|
47
47
|
RUBY
|
|
48
48
|
|
|
49
|
-
if ActiveJob.respond_to?(:perform_all_later)
|
|
49
|
+
if ActiveJob.respond_to?(:perform_all_later)
|
|
50
50
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
51
|
-
def self.#{method}_later_bulk#{suffix}
|
|
52
|
-
ActiveJob.perform_all_later
|
|
51
|
+
def self.#{method}_later_bulk#{suffix}(set#{" = all" if respond_to?(:all)})
|
|
52
|
+
ActiveJob.perform_all_later set.map { #{job}.scoped_by_wait(_1).new(_1) }
|
|
53
53
|
end
|
|
54
54
|
RUBY
|
|
55
55
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: active_job-performs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kasper Timm Hansen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-
|
|
11
|
+
date: 2024-12-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activejob
|
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
62
62
|
- !ruby/object:Gem::Version
|
|
63
63
|
version: '0'
|
|
64
64
|
requirements: []
|
|
65
|
-
rubygems_version: 3.5.
|
|
65
|
+
rubygems_version: 3.5.18
|
|
66
66
|
signing_key:
|
|
67
67
|
specification_version: 4
|
|
68
68
|
summary: ActiveJob::Performs adds the `performs` macro to set up jobs by convention.
|