<commit_before>module Comable
  class Customer < ActiveRecord::Base
    include CartOwner

    has_many :orders, class_name: Comable::Order.name, foreign_key: table_name.singularize.foreign_key
    has_many :addresses, class_name: Comable::Address.name, foreign_key: table_name.singularize.foreign_key, dependent: :destroy
    belongs_to :bill_address, class_name: Comable::Address.name, dependent: :destroy
    belongs_to :ship_address, class_name: Comable::Address.name, dependent: :destroy

    accepts_nested_attributes_for :addresses
    accepts_nested_attributes_for :bill_address
    accepts_nested_attributes_for :ship_address

    devise(*Comable::Config.devise_strategies[:customer])

    before_save :inherit_cart_items, if: :current_sign_in_at_changed?

    def with_cookies(cookies)
      @cookies = cookies
      self
    end

    # Add conditions for the orders association.
    # Override method of the orders association to support Rails 3.x.
    def orders
      super.complete
    end

    def other_addresses
      addresses - [bill_address] - [ship_address]
    end

    def update_bill_address_by(bill_address)
      update_attributes(bill_address: addresses.find_or_clone(bill_address))
    end

    def update_ship_address_by(ship_address)
      update_attributes(ship_address: addresses.find_or_clone(ship_address))
    end

    def signed_in?
      !new_record?
    end

    def not_signed_in?
      !signed_in?
    end

    def reset_cart
      return unless incomplete_order

      # TODO: テストケースの作成
      incomplete_order.destroy if incomplete_order.incomplete?

      @cart_items = nil
      @incomplete_order = nil
    end

    def cart_items
      @cart_items ||= incomplete_order.order_deliveries.first.order_details
    end

    def incomplete_order
      @incomplete_order ||= find_or_initialize_incomplete_order
    end

    def preorder(order_params = {})
      Rails.logger.debug '[DEPRECATED] Comable::Customer#preorder is deprecated. Please use #incomplete_order and #valid? methods.'
      incomplete_order.attributes = order_params
      fail Comable::InvalidOrder if incomplete_order.invalid?
      incomplete_order
    end

    def order(order_params = {})
      incomplete_order.attributes = order_params
      incomplete_order.complete!
      incomplete_order.tap { |completed_flag| reset_cart if completed_flag }
    end

    private

    def current_guest_token
      @cookies.signed[:guest_token] if @cookies
    end

    def find_or_initialize_incomplete_order
      find_incomplete_order || initialize_incomplete_order
    end

    def initialize_incomplete_order
      order = Comable::Order.create(incomplete_order_attributes)
      @cookies.permanent.signed[:guest_token] = order.guest_token if @cookies
      # enable preload
      find_incomplete_order
    end

    def incomplete_order_attributes
      {
        self.class.table_name.singularize.foreign_key => id,
        email: email,
        # TODO: Remove
        family_name: family_name,
        first_name: first_name,
        order_deliveries_attributes: [{ family_name: family_name, first_name: first_name }]
      }
    end

    def find_incomplete_order
      guest_token ||= current_guest_token unless signed_in?
      Comable::Order
        .incomplete
        .preload(order_deliveries: :order_details)
        .where(guest_token: guest_token)
        .by_customer(self)
        .first
    end

    def inherit_cart_items
      return unless current_guest_token
      guest_order = Comable::Order.incomplete.includes(order_deliveries: :order_details).where(guest_token: current_guest_token).first
      return unless guest_order
      guest_order.order_deliveries.map(&:order_details).flatten.each do |order_detail|
        move_cart_item(order_detail)
      end
      # TODO: Remove?
      cart_items.reload
    end
  end
end
<commit_msg>core: Remove the preorder method on customer<commit_after>module Comable
  class Customer < ActiveRecord::Base
    include CartOwner

    has_many :orders, class_name: Comable::Order.name, foreign_key: table_name.singularize.foreign_key
    has_many :addresses, class_name: Comable::Address.name, foreign_key: table_name.singularize.foreign_key, dependent: :destroy
    belongs_to :bill_address, class_name: Comable::Address.name, dependent: :destroy
    belongs_to :ship_address, class_name: Comable::Address.name, dependent: :destroy

    accepts_nested_attributes_for :addresses
    accepts_nested_attributes_for :bill_address
    accepts_nested_attributes_for :ship_address

    devise(*Comable::Config.devise_strategies[:customer])

    before_save :inherit_cart_items, if: :current_sign_in_at_changed?

    def with_cookies(cookies)
      @cookies = cookies
      self
    end

    # Add conditions for the orders association.
    # Override method of the orders association to support Rails 3.x.
    def orders
      super.complete
    end

    def other_addresses
      addresses - [bill_address] - [ship_address]
    end

    def update_bill_address_by(bill_address)
      update_attributes(bill_address: addresses.find_or_clone(bill_address))
    end

    def update_ship_address_by(ship_address)
      update_attributes(ship_address: addresses.find_or_clone(ship_address))
    end

    def signed_in?
      !new_record?
    end

    def not_signed_in?
      !signed_in?
    end

    def reset_cart
      return unless incomplete_order

      # TODO: テストケースの作成
      incomplete_order.destroy if incomplete_order.incomplete?

      @cart_items = nil
      @incomplete_order = nil
    end

    def cart_items
      @cart_items ||= incomplete_order.order_deliveries.first.order_details
    end

    def incomplete_order
      @incomplete_order ||= find_or_initialize_incomplete_order
    end

    def order(order_params = {})
      incomplete_order.attributes = order_params
      incomplete_order.complete!
      incomplete_order.tap { |completed_flag| reset_cart if completed_flag }
    end

    private

    def current_guest_token
      @cookies.signed[:guest_token] if @cookies
    end

    def find_or_initialize_incomplete_order
      find_incomplete_order || initialize_incomplete_order
    end

    def initialize_incomplete_order
      order = Comable::Order.create(incomplete_order_attributes)
      @cookies.permanent.signed[:guest_token] = order.guest_token if @cookies
      # enable preload
      find_incomplete_order
    end

    def incomplete_order_attributes
      {
        self.class.table_name.singularize.foreign_key => id,
        email: email,
        # TODO: Remove
        family_name: family_name,
        first_name: first_name,
        order_deliveries_attributes: [{ family_name: family_name, first_name: first_name }]
      }
    end

    def find_incomplete_order
      guest_token ||= current_guest_token unless signed_in?
      Comable::Order
        .incomplete
        .preload(order_deliveries: :order_details)
        .where(guest_token: guest_token)
        .by_customer(self)
        .first
    end

    def inherit_cart_items
      return unless current_guest_token
      guest_order = Comable::Order.incomplete.includes(order_deliveries: :order_details).where(guest_token: current_guest_token).first
      return unless guest_order
      guest_order.order_deliveries.map(&:order_details).flatten.each do |order_detail|
        move_cart_item(order_detail)
      end
      # TODO: Remove?
      cart_items.reload
    end
  end
end

{'fraction_non_alphanumeric': 0.0666487503359312, 'fraction_numerical': 0.0002687449610319806, 'mean_word_length': 3.4093601895734595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '911199', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2237, 'n_words': 447}