Skip to content

Instantly share code, notes, and snippets.

@javierav
Last active August 4, 2023 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierav/3fe2cff3b02ba25c73f9b9fbe0aaeb35 to your computer and use it in GitHub Desktop.
Save javierav/3fe2cff3b02ba25c73f9b9fbe0aaeb35 to your computer and use it in GitHub Desktop.
aasm column: :status do
  state :pending, initial: true
  state :running, :completed, :failed

  event :start, success: :success_event_start, after: :after_event_start, after_commit: :after_commit_event_start do
    transitions from: :pending, to: :running,
                after: :after_transition_from_pending_to_running,
                success: :success_transition_from_pending_to_running
  end

  event :complete do
    transitions from: :running, to: :completed
  end

  event :fail do
    transitions from: :running, to: :failed
  end
end

start

transition - after

Se ejecuta el método after_transition_from_pending_to_running.

  • status_was: pending
  • status_previously_was: nil

event - after

Se ejecuta el método after_event_start.

  • status_was: pending
  • status_previously_was: nil

start!

transition - after

Se ejecuta el método after_transition_from_pending_to_running.

  • status_was: pending
  • status_previously_was: nil

Si fallan las validaciones, se termina aquí y no sigue ejecutando las siguientes callbacks.

begin sql transaction

Se ejecuta la sentencia SQL necesaria para actualizar el campo. En este punto siempre que estemos dentro de la misma transacción, veremos el campo actualizado si hacemos otra consulta.

transition - success

Se ejecuta el método success_transition_from_pending_to_running.

  • status_was: running
  • status_previously_was: pending

event - success

Se ejecuta el método success_event_start.

  • status_was: running
  • status_previously_was: pending

event - after

Se ejecuta el método after_event_start.

  • status_was: running
  • status_previously_was: pending

transaction commit

En este punto si se consulta a la base de datos desde fuera de la transacción, se tienen acceso a los cambios.

event - after_commit

Se ejecuta el método after_commit_event_start.

  • status_was: running
  • status_previously_was: pending

More info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment