class ActiveModel::Jobs::Performer

A support class for finding the ActiveJob::Base that corresponds to a given action method on a given model. When the job class is found, the action method fires off a new instance of the job.

@private

Attributes

method_name[R]

The method name given to the class as a String.

@attr_reader [String]

model_name[R]

The model name given to the class by ActiveModel::Naming.

@attr_reader [String] @see api.rubyonrails.org/classes/ActiveModel/Naming.html

Public Class Methods

new(method_name, model_name) click to toggle source

@param [String] method_name A method corresponding to a job. @param [String] model_name The model we are calling this from.

# File lib/active_model/jobs/performer.rb, line 22
def initialize(method_name, model_name)
  @method_name = method_name.to_s
  @model_name = model_name.to_s
end

Public Instance Methods

action_name() click to toggle source

Strip the '!' off of the end of the method.

@return [String] '!'-stripped version of the method name.

# File lib/active_model/jobs/performer.rb, line 56
def action_name
  method_name.gsub ACTION_SUFFIX, ''
end
call(model) click to toggle source

Perform this action on the given model.

@param [ActiveModel::Model] model The model object we are performing the job on @return [TrueClass, FalseClass] whether the job succeeded to enqueue.

# File lib/active_model/jobs/performer.rb, line 66
def call(model)
  job_class.perform_later model
end
job?() click to toggle source

Tests whether this method name corresponds to a job class in the application.

@return [Boolean] whether this job exists or not

# File lib/active_model/jobs/performer.rb, line 31
def job?
  job_class.present?
end
job_class() click to toggle source

Attempts to find the job class for this method and return it, otherwise it returns nil when encountering a NameError.

@return [ActiveJob::Base] a job class or nil

# File lib/active_model/jobs/performer.rb, line 39
def job_class
  job_name.classify.constantize
rescue NameError
  nil
end
job_name() click to toggle source

Build the conventional job name from the given method and model. Suffix with job and separate with underscores.

@return [String] the underscored job class name

# File lib/active_model/jobs/performer.rb, line 49
def job_name
  "#{action_name}_#{model_name}_job"
end