Skip to content

Commit e240daa

Browse files
authored
Merge pull request rails#45528 from gmcgibbon/job_parent_option
Add `--parent` option to job generator to specify parent class of job.
2 parents e722b74 + 02444d9 commit e240daa

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

activejob/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* Add `--parent` option to job generator to specify parent class of job.
2+
3+
Example:
4+
5+
`bin/rails g job process_payment --parent=payment_job` generates:
6+
7+
```ruby
8+
class ProcessPaymentJob < PaymentJob
9+
# ...
10+
end
11+
```
12+
13+
*Gannon McGibbon*
14+
115
* Add more detailed description to job generator.
216

317
*Gannon McGibbon*

activejob/lib/rails/generators/job/USAGE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ Examples:
1313
`bin/rails generate job send_sms --queue=sms`
1414

1515
Creates a job and test with a custom sms queue.
16+
17+
`bin/rails generate job process_payment --parent=payment_job`
18+
19+
Creates a job and test with a `PaymentJob` parent class.

activejob/lib/rails/generators/job/job_generator.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Generators # :nodoc:
77
class JobGenerator < Rails::Generators::NamedBase # :nodoc:
88
class_option :queue, type: :string, default: "default", desc: "The queue name for the generated job"
99

10+
class_option :parent, type: :string, desc: "The parent class for the generated job"
11+
1012
check_class_collision suffix: "Job"
1113

1214
hook_for :test_framework
@@ -26,6 +28,18 @@ def create_job_file
2628
end
2729

2830
private
31+
def parent
32+
options[:parent]
33+
end
34+
35+
def parent_class_name
36+
if parent
37+
parent
38+
else
39+
"ApplicationJob"
40+
end
41+
end
42+
2943
def file_name
3044
@_file_name ||= super.sub(/_job\z/i, "")
3145
end

activejob/lib/rails/generators/job/templates/job.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<% module_namespacing do -%>
2-
class <%= class_name %>Job < ApplicationJob
2+
class <%= class_name %>Job < <%= parent_class_name.classify %>
33
queue_as :<%= options[:queue] %>
44

55
def perform(*args)

railties/test/generators/job_generator_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ def test_job_queue_param
2121
end
2222
end
2323

24+
def test_job_parent_param
25+
run_generator ["refresh_counters", "--parent", "awesome_job"]
26+
assert_file "app/jobs/refresh_counters_job.rb" do |job|
27+
assert_match(/class RefreshCountersJob < AwesomeJob/, job)
28+
end
29+
end
30+
2431
def test_job_namespace
2532
run_generator ["admin/refresh_counters", "--queue", "admin"]
2633
assert_file "app/jobs/admin/refresh_counters_job.rb" do |job|

0 commit comments

Comments
 (0)