Class: ActiveModel::Jobs::Performer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/jobs/performer.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, model_name) ⇒ Performer

Returns a new instance of Performer.

Parameters:

  • method_name (String)

    A method corresponding to a job.

  • model_name (String)

    The model we are calling this from.



22
23
24
25
# 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

Instance Attribute Details

#method_nameObject (readonly)

The method name given to the class as a String.



12
13
14
# File 'lib/active_model/jobs/performer.rb', line 12

def method_name
  @method_name
end

#model_nameObject (readonly)

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



18
19
20
# File 'lib/active_model/jobs/performer.rb', line 18

def model_name
  @model_name
end

Instance Method Details

#action_nameString

Strip the ‘!’ off of the end of the method.

Returns:

  • (String)

    ‘!’-stripped version of the method name.



56
57
58
# File 'lib/active_model/jobs/performer.rb', line 56

def action_name
  method_name.gsub ACTION_SUFFIX, ''
end

#call(model) ⇒ TrueClass, FalseClass

Perform this action on the given model.

performing the job on enqueue.

Parameters:

  • model (ActiveModel::Model)

    The model object we are

Returns:

  • (TrueClass, FalseClass)

    whether the job succeeded to



66
67
68
# File 'lib/active_model/jobs/performer.rb', line 66

def call(model)
  job_class.perform_later model
end

#job?Boolean

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

Returns:

  • (Boolean)

    whether this job exists or not



31
32
33
# File 'lib/active_model/jobs/performer.rb', line 31

def job?
  job_class.present?
end

#job_classActiveJob::Base

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

Returns:

  • (ActiveJob::Base)

    a job class or nil



39
40
41
42
43
# File 'lib/active_model/jobs/performer.rb', line 39

def job_class
  job_name.classify.constantize
rescue NameError
  nil
end

#job_nameString

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

Returns:

  • (String)

    the underscored job class name



49
50
51
# File 'lib/active_model/jobs/performer.rb', line 49

def job_name
  "#{action_name}_#{model_name}_job"
end