Rails 7.1 adds the --parent option to the Job Generator
source link: https://blog.kiprosh.com/add-parent-option-to-job-generator-to-specify-parent-class-of-job/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
rails_7 Published on 12 October 2022 12 October 2022 • 1 min read
Rails 7.1 adds the --parent option to the Job Generator
The Active Job framework allows the declaration of jobs and their execution on numerous queueing backends. Everything from regular clean-ups to billing charges, to mailers, can be included in these jobs. Basically, anything that can be divided up into small pieces and run in parallel.
Before Rails 7.1
Prior to Rails 7.1, adding new jobs with a superclass required manually creating the file and declaring the class.
Rails 7.1 onwards
The Active Job
generator provides a --parent
option using which we can create a job that inherits from a particular superclass. This is similar to the model generator's implementation of --parent
. The following will create a job in the app/jobs
directory with PaymentJob
as its superclass:
$ rails g job stripe_process_payment --parent=payment_job
The --parent
option also accepts the superclass name in CamelCase
:
$ rails g job StripeProcessPaymentJob --parent=PaymentJob
Example:
git:(master) ✗ rails g job stripe_process_payment --parent=payment_job
invoke test_unit
create test/jobs/stripe_process_payment_job_test.rb
create app/jobs/stripe_process_payment_job.rb
Which will generate the following code:
# app/jobs/stripe_process_payment_job.rb
class StripeProcessPaymentJob < PaymentJob
queue_as :default
def perform(*args)
# Do something later
end
end
-
Generate Jobs in sub-directories
Using the namespace in the command, you can generate jobs in subdirectories.
It can be written in the following two ways:
$ rails g job stripe/process_payment --parent=payment_job
$ rails g job Stripe::ProcessPaymentJob --parent=PaymentJob
git:(master) ✗ rails g job stripe/process_payment --parent=payment_job invoke test_unit create test/jobs/stripe/process_payment_job_test.rb create app/jobs/stripe/process_payment_job.rb
class Stripe::ProcessPaymentJob < PaymentJob queue_as :default def perform(*args) # Do something later end end
-
Ignore the test file
When we run the
rails generator
command, this also creates the test files. In case you want to ignore these files you can pass the flag--no-test-framework
.git:(master) ✗ rails g job stripe_process_payment --parent=payment_job --no-test-framework create app/jobs/stripe_process_payment_job.rb
-
Rollback
Let's say to create the jobs you ran the command which adds the required files. But then realized you wanted to edit the file name. To undo this, simply run
rails destroy
command as below:rails d job StripeProcessPaymentJob
orrails d job stripe_process_payment
orrails d job stripe_process_payment.rb
git:(master) ✗ rails d job StripeProcessPaymentJob invoke test_unit remove test/jobs/stripe_process_payment_job_test.rb remove app/jobs/stripe_process_payment_job.rb
References
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK