module Runnable
  extend ActiveSupport::Concern
  include Stateable

  included do
    extend Enumerize
    # extend ActiveModel::Naming

    enumerize :state,
              in: [:pending, :running, :completed, :aborted],
              default: :pending,
              scope: true

    field :result
    enumerize :result,
              in: [:success, :failure]

  end

  def status
    return result if state.completed?

    super
  end

  def can_run?
    state.pending?
  end

  def can_rerun?
    state.aborted? || status == :failure
  end

  def run
    update(state: :running) if can_run?
  end

  def rerun
    update(state: :pending, result: nil) if can_rerun?
    run
  end

  def can_stop?
    state.running?
  end

  def stop
    update(state: :aborted) if can_stop?
  end
end

module Runnable
  extend ActiveSupport::Concern
  include Stateable

  included do
    extend Enumerize
    # extend ActiveModel::Naming

    enumerize :state,
              in: [:pending, :running, :completed, :aborted],
              default: :pending,
              scope: true

    field :result
    enumerize :result,
              in: [:success, :failure]

  end

  def status
    return result if state.completed?

    super
  end

  def can_run?
    state.pending?
  end

  def can_rerun?
    state.completed? || state.aborted?
  end

  def run
    update(state: :running) if can_run?
  end

  def rerun
    update(state: :pending, result: nil) if can_rerun?
    run
  end

  def can_stop?
    state.running?
  end

  def stop
    update(state: :aborted) if can_stop?
  end
end

Revert "restric the rerun for failure or aborted only"
Revert "restric the rerun for failure or aborted only"

This reverts commit cd159fdf1e447191ccf11d80281c75700f8770c2.
