Skip to content

Commit 9a9fc01

Browse files
committed
generate ApplicationJob if it does not already exist
ActiveJob jobs now inherit from ApplicationJob by default. However, when updating to Rails 5 from the old Rails, since there is a possibility that ApplicationJob does not exist.
1 parent 520e81b commit 9a9fc01

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ def self.default_generator_root
1717

1818
def create_job_file
1919
template 'job.rb', File.join('app/jobs', class_path, "#{file_name}_job.rb")
20+
21+
in_root do
22+
if self.behavior == :invoke && !File.exist?(application_job_file_name)
23+
template 'application_job.rb', application_job_file_name
24+
end
25+
end
2026
end
27+
28+
private
29+
def application_job_file_name
30+
@application_job_file_name ||= if mountable_engine?
31+
"app/jobs/#{namespaced_path}/application_job.rb"
32+
else
33+
'app/jobs/application_job.rb'
34+
end
35+
end
2136
end
2237
end
2338
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<% module_namespacing do -%>
2+
class ApplicationJob < ActiveJob::Base
3+
end
4+
<% end -%>

railties/test/generators/job_generator_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ def test_job_namespace
2626
assert_match(/queue_as :admin/, job)
2727
end
2828
end
29+
30+
def test_application_job_skeleton_is_created
31+
run_generator ["refresh_counters"]
32+
assert_file "app/jobs/application_job.rb" do |job|
33+
assert_match(/class ApplicationJob < ActiveJob::Base/, job)
34+
end
35+
end
2936
end

railties/test/generators/plugin_generator_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,19 @@ def test_generate_application_mailer_when_does_not_exist_in_mountable_engine
669669
end
670670
end
671671

672+
def test_generate_application_job_when_does_not_exist_in_mountable_engine
673+
run_generator [destination_root, '--mountable']
674+
FileUtils.rm "#{destination_root}/app/jobs/bukkits/application_job.rb"
675+
capture(:stdout) do
676+
`#{destination_root}/bin/rails g job refresh_counters`
677+
end
678+
679+
assert_file "#{destination_root}/app/jobs/bukkits/application_job.rb" do |record|
680+
assert_match(/module Bukkits/, record)
681+
assert_match(/class ApplicationJob < ActiveJob::Base/, record)
682+
end
683+
end
684+
672685
def test_after_bundle_callback
673686
path = 'http://example.org/rails_template'
674687
template = %{ after_bundle { run 'echo ran after_bundle' } }

0 commit comments

Comments
 (0)