<commit_before>module Spree
  class Order < ActiveRecord::Base
    module Checkout
      def self.included(klass)
        klass.class_eval do
          class_attribute :next_event_transitions
          class_attribute :previous_states
          class_attribute :checkout_flow
          class_attribute :checkout_steps
          class_attribute :removed_transitions

          def self.checkout_flow(&block)
            if block_given?
              @checkout_flow = block
              define_state_machine!
            else
              @checkout_flow
            end
          end

          def self.define_state_machine!
            self.checkout_steps = {}
            self.next_event_transitions = []
            self.previous_states = [:cart]
            self.removed_transitions = []

            # Build the checkout flow using the checkout_flow defined either
            # within the Order class, or a decorator for that class.
            #
            # This method may be called multiple times depending on if the
            # checkout_flow is re-defined in a decorator or not.
            instance_eval(&checkout_flow)

            klass = self

            # To avoid a ton of warnings when the state machine is re-defined
            StateMachine::Machine.ignore_method_conflicts = true
            # To avoid multiple occurrences of the same transition being defined
            # On first definition, state_machines will not be defined
            state_machines.clear if respond_to?(:state_machines)
            state_machine :state, :initial => :cart do
              klass.next_event_transitions.each { |t| transition(t.merge(:on => :next)) }

              # Persist the state on the order
              after_transition do |order|
                order.state = order.state
                order.save
              end

              event :cancel do
                transition :to => :canceled, :if => :allow_cancel?
              end

              event :return do
                transition :to => :returned, :from => :awaiting_return, :unless => :awaiting_returns?
              end

              event :resume do
                transition :to => :resumed, :from => :canceled, :if => :allow_resume?
              end

              event :authorize_return do
                transition :to => :awaiting_return
              end

              if states[:payment]
                before_transition :to => :complete do |order|
                  order.process_payments! if order.payment_required?
                end
              end

              before_transition :from => :cart, :do => :ensure_line_items_present

              before_transition :from => :address, :do => :create_tax_charge!

              before_transition :to => :delivery, :do => :create_proposed_shipments
              before_transition :to => :delivery, :do => :ensure_available_shipping_rates
              before_transition :to => :payment, :do => :set_shipments_cost
              before_transition :to => :payment, :do => :create_tax_charge!

              after_transition :to => :complete, :do => :finalize!
              after_transition :to => :resumed,  :do => :after_resume
              after_transition :to => :canceled, :do => :after_cancel

              after_transition :from => any - :cart, :to => any - [:confirm, :complete] do |order|
                order.updater.update_totals
                order.updater.persist_totals
              end
            end
          end<commit_msg>Only define event transitions if the state machine contains those states<commit_after>module Spree
  class Order < ActiveRecord::Base
    module Checkout
      def self.included(klass)
        klass.class_eval do
          class_attribute :next_event_transitions
          class_attribute :previous_states
          class_attribute :checkout_flow
          class_attribute :checkout_steps
          class_attribute :removed_transitions

          def self.checkout_flow(&block)
            if block_given?
              @checkout_flow = block
              define_state_machine!
            else
              @checkout_flow
            end
          end

          def self.define_state_machine!
            self.checkout_steps = {}
            self.next_event_transitions = []
            self.previous_states = [:cart]
            self.removed_transitions = []

            # Build the checkout flow using the checkout_flow defined either
            # within the Order class, or a decorator for that class.
            #
            # This method may be called multiple times depending on if the
            # checkout_flow is re-defined in a decorator or not.
            instance_eval(&checkout_flow)

            klass = self

            # To avoid a ton of warnings when the state machine is re-defined
            StateMachine::Machine.ignore_method_conflicts = true
            # To avoid multiple occurrences of the same transition being defined
            # On first definition, state_machines will not be defined
            state_machines.clear if respond_to?(:state_machines)
            state_machine :state, :initial => :cart do
              klass.next_event_transitions.each { |t| transition(t.merge(:on => :next)) }

              # Persist the state on the order
              after_transition do |order|
                order.state = order.state
                order.save
              end

              event :cancel do
                transition :to => :canceled, :if => :allow_cancel?
              end

              event :return do
                transition :to => :returned, :from => :awaiting_return, :unless => :awaiting_returns?
              end

              event :resume do
                transition :to => :resumed, :from => :canceled, :if => :allow_resume?
              end

              event :authorize_return do
                transition :to => :awaiting_return
              end

              if states[:payment]
                before_transition :to => :complete do |order|
                  order.process_payments! if order.payment_required?
                end
              end

              before_transition :from => :cart, :do => :ensure_line_items_present

              if states[:address]
                before_transition :from => :address, :do => :create_tax_charge!
              end

              if states[:delivery]
                before_transition :to => :delivery, :do => :create_proposed_shipments
                before_transition :to => :delivery, :do => :ensure_available_shipping_rates
              end

              if states[:payment]
                before_transition :to => :payment, :do => :set_shipments_cost
                before_transition :to => :payment, :do => :create_tax_charge!
              end

              after_transition :to => :complete, :do => :finalize!
              after_transition :to => :resumed,  :do => :after_resume
              after_transition :to => :canceled, :do => :after_cancel

              after_transition :from => any - :cart, :to => any - [:confirm, :complete] do |order|
                order.updater.update_totals
                order.updater.persist_totals
              end
            end
          end
{'fraction_non_alphanumeric': 0.07412008281573498, 'fraction_numerical': 0.0, 'mean_word_length': 1.708785046728972, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '7472762', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1777, 'n_words': 512}