Rails adds the --parent option to the job generator


Rails generators allow developers to quickly create files that follow Rails conventions. The most power-packed one is the scaffold generator which creates a model, controller, views, and tests for a given resource. Of course, each file can be created individually using the model, controller, and job generators.

Before

The job generator can be used to create an ApplicationJob class. This class is the parent class for all jobs in the application. However, it is not possible to create a job that inherits from a different class.

   rails generate job MyJob
      invoke  test_unit
      create    test/jobs/my_job_test.rb
      create  app/jobs/my_job.rb
   cat app/jobs/my_job.rb 
  class MyJob < ApplicationJob
    queue_as :default

    def perform(*args)
      # Do something later
    end
  end

After

Thanks to this PR, the job generator now allows to specify a parent class for the job. This is similar to the working of the model generator.

   rails generate job MyJob --parent=YourJob
      invoke  test_unit
      create    test/jobs/my_job_test.rb
      create  app/jobs/my_job.rb
   cat app/jobs/my_job.rb 
  class MyJob < YourJob
    queue_as :default

    def perform(*args)
      # Do something later
    end
  end

Join Our Newsletter