Class: Spree::StateChangeTrackingJob

Inherits:
BaseJob
  • Object
show all
Defined in:
app/jobs/spree/state_change_tracking_job.rb

Overview

Background job to track state changes asynchronously This avoids performance impact during checkout and prevents callback-related issues with recent versions of the state_machines gem.

Instance Method Summary collapse

Instance Method Details

#perform(stateful, previous_state, current_state, transition_timestamp, name = stateful.class.model_name.element) ⇒ Object

Parameters:

  • stateful (GlobalId)

    The stateful object to track changes for

  • previous_state (String)

    The previous state of the order

  • current_state (String)

    The current state of the order

  • transition_timestamp (Time)

    When the state transition occurred

  • name (String) (defaults to: stateful.class.model_name.element)

    The element name of the state transition being tracked. It defaults to the stateful model element name.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/jobs/spree/state_change_tracking_job.rb', line 14

def perform(
  stateful,
  previous_state,
  current_state,
  transition_timestamp,
  name = stateful.class.model_name.element
)
  Spree::StateChange.create!(
    name: name,
    stateful: stateful,
    previous_state: previous_state,
    next_state: current_state,
    created_at: transition_timestamp,
    updated_at: transition_timestamp,
    user_id: stateful.try(:user_id) || stateful.try(:order)&.user_id
  )
end