Module: Spree::Core::ControllerHelpers::Order

Extended by:
ActiveSupport::Concern
Defined in:
lib/spree/core/controller_helpers/order.rb

Instance Method Summary collapse

Instance Method Details

#associate_userObject



61
62
63
64
65
66
# File 'lib/spree/core/controller_helpers/order.rb', line 61

def associate_user
  @order ||= current_order
  if try_spree_current_user && @order
    @order.associate_user!(try_spree_current_user) if @order.user.blank? || @order.email.blank?
  end
end

#current_order(options = {}) ⇒ Object

The current incomplete order from the token for use in cart and during checkout



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/spree/core/controller_helpers/order.rb', line 33

def current_order(options = {})
  options[:create_order_if_necessary] ||= false
  options[:includes] ||= false

  if @current_order
    @current_order.last_ip_address = ip_address
    return @current_order
  end

  @current_order = find_order_by_token_or_user(options, false)

  if options[:create_order_if_necessary] && (@current_order.nil? || @current_order.completed?)
    @current_order = current_store.orders.create!(current_order_params.except(:token))
    @current_order.associate_user! try_spree_current_user if try_spree_current_user
    @current_order.last_ip_address = ip_address

    create_token_cookie(@current_order.token)
  end

  # There is some edge case where the order doesn't have a token.
  # but can't reproduce it. So let's generate one on the fly in that case.
  @current_order.regenerate_token if @current_order && @current_order.token.blank?

  create_token_cookie(@current_order&.token || current_order_params[:token] || params[:token]) if create_cookie_from_token?

  @current_order
end

#ip_addressObject



93
94
95
# File 'lib/spree/core/controller_helpers/order.rb', line 93

def ip_address
  request.remote_ip
end

#order_tokenObject



14
15
16
# File 'lib/spree/core/controller_helpers/order.rb', line 14

def order_token
  @order_token ||= cookies.signed[:token] || params[:order_token]
end

#set_current_orderObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spree/core/controller_helpers/order.rb', line 68

def set_current_order
  return unless try_spree_current_user && current_order

  orders_scope = user_orders_scope

  orders_to_merge = orders_scope.limit(10)
  order_ids_to_delete = orders_scope.ids - orders_to_merge.ids

  orders_scope_exists = orders_scope.exists?

  if orders_scope.exists?
    ActiveRecord::Base.connected_to(role: :writing) do
      orders_to_merge.find_each do |order|
        current_order.merge!(order, try_spree_current_user)
      end

      Spree::Order.where(id: order_ids_to_delete).find_each do |order|
        Rails.logger.error("Failed to destroy order #{order.id} while merging") unless order.destroy
      end
    end
  end

  orders_scope_exists
end

#simple_current_orderObject

Used in the link_to_cart helper.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spree/core/controller_helpers/order.rb', line 19

def simple_current_order
  return @simple_current_order if @simple_current_order

  @simple_current_order = find_order_by_token_or_user

  if @simple_current_order
    @simple_current_order.last_ip_address = ip_address
    return @simple_current_order
  else
    @simple_current_order = current_store.orders.new
  end
end