123456789_123456789_123456789_123456789_123456789_

Module: ActiveJob::Attributes

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Defined in: activejob/lib/active_job/attributes.rb

Overview

The Attributes module provides typed attributes for jobs using the Active Model Attributes API. Declared attributes are automatically serialized with the job data and restored when the job is deserialized.

This is especially useful with Continuable, where a job may be interrupted and resumed multiple times and you need to persist attributes across steps until the job finishes.

class SubmitEnrollmentJob < ApplicationJob
include ActiveJob::Continuable

attribute :payment_token, :string
attribute :billing_profile_id, :integer

def perform(enrollment)
  step(:tokenize_payment_instrument) do
    self.payment_token = PaymentGateway.tokenize(enrollment.user.payment_instrument)
  end

  step(:create_billing_profile) do
    self.billing_profile_id = BillingProfileApi.create(customer_id: enrollment.user_id)
  end

  step(:submit_enrollment) do
    submission_id = EnrollmentApi.submit(enrollment, billing_profile_id)
    enrollment.update!(status: 'processing', submission_id: submission_id)
  end
end
end

Attributes also work without Continuable, persisting across retries.

Attributes support all built-in Active Model types, see ::ActiveModel::Attribute for details. For custom types attribute values must be serializable as Active Job arguments. See Arguments for the full list of supported types.

Constant Summary

::ActiveModel::AttributeMethods - Included

CALL_COMPILABLE_REGEXP, NAME_COMPILABLE_REGEXP

Class Method Summary

::ActiveSupport::Concern - Extended

class_methods

Define class methods from given block.

included

Evaluate given block in context of base class, so that you can write class macros here.

prepended

Evaluate given block in context of base class, so that you can write class macros here.

append_features, prepend_features

Instance Attribute Summary

Instance Method Summary

::ActiveModel::Attributes - Included

#attribute_names

Returns an array of attribute names as strings.

#attributes

Returns a hash of all the attributes with their names as keys and the values of the attributes as values.

#_write_attribute, #attribute,
#attribute=
#freeze, #initialize, #initialize_dup

::ActiveModel::AttributeMethods - Included

#attribute_missing

attribute_missing is like method_missing, but for attributes.

#method_missing

Allows access to the object attributes, which are held in the hash returned by attributes, as though they were first-class methods.

#respond_to?,
#respond_to_without_attributes?

A Person instance with a name attribute can ask person.respond_to?(:name), person.respond_to?(:name=), and person.respond_to?(:name?) which will all return true.

#_read_attribute, #attribute_method?,
#matched_attribute_method

Returns a struct representing the matching attribute method.

#missing_attribute

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods

Instance Method Details

#deserialize(job_data)

This method is for internal use only.
[ GitHub ]

  
# File 'activejob/lib/active_job/attributes.rb', line 52

def deserialize(job_data) # :nodoc:
  super
  deserialize_attribute_values(job_data["attributes"]) if job_data["attributes"]
end

#deserialize_attribute_values(serialized) (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/attributes.rb', line 66

def deserialize_attribute_values(serialized)
  values = Arguments.deserialize([serialized]).first
  values.each do |name, value|
    name = name.to_s
    @attributes.write_cast_value(name, value) if self.class.attribute_types.key?(name)
  end
end

#serialize

This method is for internal use only.
[ GitHub ]

  
# File 'activejob/lib/active_job/attributes.rb', line 48

def serialize # :nodoc:
  super.merge("attributes" => serialize_attribute_values)
end

#serialize_attribute_values (private)

[ GitHub ]

  
# File 'activejob/lib/active_job/attributes.rb', line 58

def serialize_attribute_values
  values = {}
  self.class.attribute_names.each do |name|
    values[name] = @attributes.fetch_value(name)
  end
  Arguments.serialize([values]).first
end