<commit_before>require 'spree/core/validators/email'
require 'spree/order/checkout'

module Spree
  class Order < ActiveRecord::Base
    # TODO:
    # Need to use fully qualified name here because during sandbox migration
    # there is a class called Checkout which conflicts if you use this:
    #
    #   include Checkout
    #
    # rather than the qualified name. This will most likely be fixed with the
    # 1.3 release.
    include Spree::Order::Checkout
    checkout_flow do
      go_to_state :address
      go_to_state :delivery
      go_to_state :payment, :if => lambda { |order|
        # Fix for #2191
        if order.shipping_method
          order.create_shipment!
          order.update_totals
        end
        order.payment_required?
      }
      go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
      go_to_state :complete, :if => lambda { |order| (order.payment_required? && order.has_unprocessed_payments?) || !order.payment_required? }
      remove_transition :from => :delivery, :to => :confirm
    end

    token_resource

    attr_accessible :line_items, :bill_address_attributes, :ship_address_attributes, :payments_attributes,
                    :ship_address, :bill_address, :payments_attributes, :line_items_attributes, :number,
                    :shipping_method_id, :email, :use_billing, :special_instructions, :currency, :coupon_code

    attr_reader :coupon_code

    if Spree.user_class
      belongs_to :user, :class_name => Spree.user_class.to_s
    else
      belongs_to :user
    end

    belongs_to :bill_address, :foreign_key => :bill_address_id, :class_name => "Spree::Address"
    alias_attribute :billing_address, :bill_address

    belongs_to :ship_address, :foreign_key => :ship_address_id, :class_name => "Spree::Address"
    alias_attribute :shipping_address, :ship_address

    belongs_to :shipping_method

    has_many :state_changes, :as => :stateful
    has_many :line_items, :dependent => :destroy, :order => "created_at ASC"
    has_many :inventory_units
    has_many :payments, :dependent => :destroy

    has_many :shipments, :dependent => :destroy do
      def states
        pluck(:state).uniq
      end
    end

    has_many :return_authorizations, :dependent => :destroy
    has_many :adjustments, :as => :adjustable, :dependent => :destroy, :order => "created_at ASC"

    accepts_nested_attributes_for :line_items
    accepts_nested_attributes_for :bill_address
    accepts_nested_attributes_for :ship_address
    accepts_nested_attributes_for :payments
    accepts_nested_attributes_for :shipments

    # Needs to happen before save_permalink is called
    before_validation :set_currency
    before_validation :generate_order_number, :on => :create
    before_validation :clone_billing_address, :if => :use_billing?
    attr_accessor :use_billing

    before_create :link_by_email
    after_create :create_tax_charge!

    validates :email, :presence => true, :if => :require_email
    validates :email, :email => true, :if => :require_email, :allow_blank => true
    validate :has_available_shipment
    validate :has_available_payment, :if => :payment_required?

    make_permalink :field => :number

    class_attribute :update_hooks
    self.update_hooks = Set.new

    def self.by_number(number)
      where(:number => number)
    end

    def self.between(start_date, end_date)
      where(:created_at => start_date..end_date)
    end

    def self.by_customer(customer)
      joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
    end
<commit_msg>Revert "Don't require payment methods if payment isn't required"<commit_after>require 'spree/core/validators/email'
require 'spree/order/checkout'

module Spree
  class Order < ActiveRecord::Base
    # TODO:
    # Need to use fully qualified name here because during sandbox migration
    # there is a class called Checkout which conflicts if you use this:
    #
    #   include Checkout
    #
    # rather than the qualified name. This will most likely be fixed with the
    # 1.3 release.
    include Spree::Order::Checkout
    checkout_flow do
      go_to_state :address
      go_to_state :delivery
      go_to_state :payment, :if => lambda { |order|
        # Fix for #2191
        if order.shipping_method
          order.create_shipment!
          order.update_totals
        end
        order.payment_required?
      }
      go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
      go_to_state :complete, :if => lambda { |order| (order.payment_required? && order.has_unprocessed_payments?) || !order.payment_required? }
      remove_transition :from => :delivery, :to => :confirm
    end

    token_resource

    attr_accessible :line_items, :bill_address_attributes, :ship_address_attributes, :payments_attributes,
                    :ship_address, :bill_address, :payments_attributes, :line_items_attributes, :number,
                    :shipping_method_id, :email, :use_billing, :special_instructions, :currency, :coupon_code

    attr_reader :coupon_code

    if Spree.user_class
      belongs_to :user, :class_name => Spree.user_class.to_s
    else
      belongs_to :user
    end

    belongs_to :bill_address, :foreign_key => :bill_address_id, :class_name => "Spree::Address"
    alias_attribute :billing_address, :bill_address

    belongs_to :ship_address, :foreign_key => :ship_address_id, :class_name => "Spree::Address"
    alias_attribute :shipping_address, :ship_address

    belongs_to :shipping_method

    has_many :state_changes, :as => :stateful
    has_many :line_items, :dependent => :destroy, :order => "created_at ASC"
    has_many :inventory_units
    has_many :payments, :dependent => :destroy

    has_many :shipments, :dependent => :destroy do
      def states
        pluck(:state).uniq
      end
    end

    has_many :return_authorizations, :dependent => :destroy
    has_many :adjustments, :as => :adjustable, :dependent => :destroy, :order => "created_at ASC"

    accepts_nested_attributes_for :line_items
    accepts_nested_attributes_for :bill_address
    accepts_nested_attributes_for :ship_address
    accepts_nested_attributes_for :payments
    accepts_nested_attributes_for :shipments

    # Needs to happen before save_permalink is called
    before_validation :set_currency
    before_validation :generate_order_number, :on => :create
    before_validation :clone_billing_address, :if => :use_billing?
    attr_accessor :use_billing

    before_create :link_by_email
    after_create :create_tax_charge!

    validates :email, :presence => true, :if => :require_email
    validates :email, :email => true, :if => :require_email, :allow_blank => true
    validate :has_available_shipment
    validate :has_available_payment

    make_permalink :field => :number

    class_attribute :update_hooks
    self.update_hooks = Set.new

    def self.by_number(number)
      where(:number => number)
    end

    def self.between(start_date, end_date)
      where(:created_at => start_date..end_date)
    end

    def self.by_customer(customer)
      joins(:user).where("#{Spree.user_class.table_name}.email" => customer)
    end

{'fraction_non_alphanumeric': 0.09095982142857142, 'fraction_numerical': 0.0016741071428571428, 'mean_word_length': 3.6551948051948053, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '4415178', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2191, 'n_words': 520}