gocardless / statesman

A statesmanlike state machine library.
https://gocardless.com/blog/statesman/
MIT License
1.79k stars 163 forks source link

Add update_most_recent as part of the Transition adapter module #518

Open dmitry opened 1 year ago

dmitry commented 1 year ago

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]