Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_most_recent as part of the Transition adapter module #518

Open
dmitry opened this issue Oct 11, 2023 · 0 comments
Open

Add update_most_recent as part of the Transition adapter module #518

dmitry opened this issue Oct 11, 2023 · 0 comments

Comments

@dmitry
Copy link
Contributor

dmitry commented Oct 11, 2023

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant