<commit_before>class OrderWorkflow
  attr_reader :order

  def initialize(order)
    @order = order
  end

  def complete
    advance_order(advance_order_options)
  end

  def complete!
    advance_order!(advance_order_options)
  end

  def next(options = {})
    result = advance_order_one_step

    after_transition_hook(options)

    result
  end

  private

  def advance_order_options
    shipping_method_id = order.shipping_method.id if order.shipping_method.present?
    { shipping_method_id: shipping_method_id }
  end

  def advance_order(options)
    until order.state == "complete"
      break unless order.next

      after_transition_hook(options)
    end
  end

  def advance_order!(options)
    until order.completed?
      order.next!
      after_transition_hook(options)
    end
  end

  def advance_order_one_step
    tries ||= 3
    order.next
  rescue ActiveRecord::StaleObjectError
    retry unless (tries -= 1).zero?
    false
  end

  def after_transition_hook(options)
    if order.state == "delivery"
      order.select_shipping_method(options[:shipping_method_id]) if options[:shipping_method_id]
    end

    persist_all_payments if order.state == "payment"
  end

  # When a payment fails, the order state machine rollbacks all transactions
  #   Here we ensure we always persist all payments
  def persist_all_payments
    order.payments.each do |payment|
      original_payment_state = payment.state
      if original_payment_state != payment.reload.state
        payment.update(state: original_payment_state)
      end
    end
  end

end<commit_msg>Avoid reloading the payment every time, so that in-memory data is not wiped out<commit_after>class OrderWorkflow
  attr_reader :order

  def initialize(order)
    @order = order
  end

  def complete
    advance_order(advance_order_options)
  end

  def complete!
    advance_order!(advance_order_options)
  end

  def next(options = {})
    result = advance_order_one_step

    after_transition_hook(options)

    result
  end

  private

  def advance_order_options
    shipping_method_id = order.shipping_method.id if order.shipping_method.present?
    { shipping_method_id: shipping_method_id }
  end

  def advance_order(options)
    until order.state == "complete"
      break unless order.next

      after_transition_hook(options)
    end
  end

  def advance_order!(options)
    until order.completed?
      order.next!
      after_transition_hook(options)
    end
  end

  def advance_order_one_step
    tries ||= 3
    order.next
  rescue ActiveRecord::StaleObjectError
    retry unless (tries -= 1).zero?
    false
  end

  def after_transition_hook(options)
    if order.state == "delivery"
      order.select_shipping_method(options[:shipping_method_id]) if options[:shipping_method_id]
    end

    persist_all_payments if order.state == "payment"
  end

  # When a payment fails, the order state machine rollbacks all transactions
  #   Here we ensure we always persist all payments
  def persist_all_payments
    order.payments.each do |payment|
      original_payment_state = payment.state
      if original_payment_state != Spree::Payment.find(payment.id).state
        payment.reload.update(state: original_payment_state)
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.064, 'fraction_numerical': 0.0012307692307692308, 'mean_word_length': 3.4351978171896316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6176044', 'n_tokens_mistral': 1008, 'n_tokens_neox': 981, 'n_words': 242}