aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/rails/generators/job/job_generator.rb
blob: 474f181f652d93e9a79893219b09b573f5950211 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require "rails/generators/named_base"

module Rails # :nodoc:
  module Generators # :nodoc:
    class JobGenerator < Rails::Generators::NamedBase # :nodoc:
      desc "This generator creates an active job file at app/jobs"

      class_option :queue, type: :string, default: "default", desc: "The queue name for the generated job"

      check_class_collision suffix: "Job"

      hook_for :test_framework

      def self.default_generator_root
        __dir__
      end

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

        in_root do
          if 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