Skip to content

Add update_most_recent as part of the Transition adapter module #518

@dmitry

Description

@dmitry

I propose adding the update_most_recent method to the Transition adapter module. This enhancement will help eliminate duplicated code and improve maintainability by providing a standard way to update the most recent transition on destruction.

Motivation:

Currently, users need to implement this logic themselves, resulting in duplicated code and potential errors. By adding this method to the core, we can ensure consistency and improve code quality.

At present, there are only two modules, ActiveRecordTransition and MemoryTransition. The proposed update is to add code duplication to the ActiveRecordTransition module.

  after_destroy :update_most_recent, if: :most_recent?

  private

  def update_most_recent
    last_transition = parent.send(transition_name).order(:sort_key).last
    return unless last_transition.present?
    last_transition.update_column(:most_recent, true)
  end

The following code demonstrates how to use this method in a transition model:

  include Statesman::Adapters::ActiveRecordTransition[transition_name: :transitions]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions