2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
require "rails/generators/named_base"
|
2014-06-29 11:59:38 -04:00
|
|
|
|
2015-04-29 21:44:37 -04:00
|
|
|
module Rails # :nodoc:
|
2014-06-29 11:59:38 -04:00
|
|
|
module Generators # :nodoc:
|
|
|
|
class JobGenerator < Rails::Generators::NamedBase # :nodoc:
|
2016-08-06 12:40:07 -04:00
|
|
|
desc "This generator creates an active job file at app/jobs"
|
2014-06-29 11:59:38 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
class_option :queue, type: :string, default: "default", desc: "The queue name for the generated job"
|
2014-06-29 11:59:38 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
check_class_collision suffix: "Job"
|
2014-08-22 15:30:36 -04:00
|
|
|
|
|
|
|
hook_for :test_framework
|
|
|
|
|
2014-06-29 11:59:38 -04:00
|
|
|
def self.default_generator_root
|
2017-05-15 10:17:28 -04:00
|
|
|
__dir__
|
2014-06-29 11:59:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_job_file
|
2016-08-06 12:40:07 -04:00
|
|
|
template "job.rb", File.join("app/jobs", class_path, "#{file_name}_job.rb")
|
2016-03-12 01:00:55 -05:00
|
|
|
|
|
|
|
in_root do
|
2017-01-05 03:20:57 -05:00
|
|
|
if behavior == :invoke && !File.exist?(application_job_file_name)
|
2016-08-06 12:40:07 -04:00
|
|
|
template "application_job.rb", application_job_file_name
|
2016-03-12 01:00:55 -05:00
|
|
|
end
|
|
|
|
end
|
2014-06-29 11:59:38 -04:00
|
|
|
end
|
2016-03-12 01:00:55 -05:00
|
|
|
|
|
|
|
private
|
2018-04-21 23:09:04 -04:00
|
|
|
def file_name
|
2018-04-22 01:30:07 -04:00
|
|
|
@_file_name ||= super.sub(/_job\z/i, "")
|
2018-04-21 23:09:04 -04:00
|
|
|
end
|
|
|
|
|
2016-03-12 01:00:55 -05:00
|
|
|
def application_job_file_name
|
2016-08-06 13:55:02 -04:00
|
|
|
@application_job_file_name ||= if mountable_engine?
|
2017-01-03 21:51:18 -05:00
|
|
|
"app/jobs/#{namespaced_path}/application_job.rb"
|
2016-08-06 13:55:02 -04:00
|
|
|
else
|
|
|
|
"app/jobs/application_job.rb"
|
|
|
|
end
|
2016-03-12 01:00:55 -05:00
|
|
|
end
|
2014-06-29 11:59:38 -04:00
|
|
|
end
|
|
|
|
end
|
2014-08-12 06:53:46 -04:00
|
|
|
end
|