Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate ApplicationJob if it does not already exist #24165

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions activejob/lib/rails/generators/job/job_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ def self.default_generator_root

def create_job_file
template 'job.rb', File.join('app/jobs', class_path, "#{file_name}_job.rb")

in_root do
if self.behavior == :invoke && !File.exist?(application_job_file_name)
template 'application_job.rb', application_job_file_name
end
end
end

private
def application_job_file_name
@application_job_file_name ||= if mountable_engine?
"app/jobs/#{namespaced_path}/application_job.rb"
else
'app/jobs/application_job.rb'
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<% module_namespacing do -%>
class ApplicationJob < ActiveJob::Base
end
<% end -%>
7 changes: 7 additions & 0 deletions railties/test/generators/job_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ def test_job_namespace
assert_match(/queue_as :admin/, job)
end
end

def test_application_job_skeleton_is_created
run_generator ["refresh_counters"]
assert_file "app/jobs/application_job.rb" do |job|
assert_match(/class ApplicationJob < ActiveJob::Base/, job)
end
end
end
13 changes: 13 additions & 0 deletions railties/test/generators/plugin_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,19 @@ def test_generate_application_mailer_when_does_not_exist_in_mountable_engine
end
end

def test_generate_application_job_when_does_not_exist_in_mountable_engine
run_generator [destination_root, '--mountable']
FileUtils.rm "#{destination_root}/app/jobs/bukkits/application_job.rb"
capture(:stdout) do
`#{destination_root}/bin/rails g job refresh_counters`
end

assert_file "#{destination_root}/app/jobs/bukkits/application_job.rb" do |record|
assert_match(/module Bukkits/, record)
assert_match(/class ApplicationJob < ActiveJob::Base/, record)
end
end

def test_after_bundle_callback
path = 'http://example.org/rails_template'
template = %{ after_bundle { run 'echo ran after_bundle' } }
Expand Down