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

Defined in:
lib/spree/core/controller_helpers/order.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


5
6
7
8
9
10
11
12
# File 'lib/spree/core/controller_helpers/order.rb', line 5

def self.included(base)
  base.class_eval do
    helper_method :simple_current_order
    helper_method :current_order
    helper_method :current_currency
    before_filter :set_current_order
  end
end

Instance Method Details

#associate_userObject


51
52
53
54
55
56
57
58
# File 'lib/spree/core/controller_helpers/order.rb', line 51

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

  session[:guest_token] = nil
end

#current_currencyObject


71
72
73
# File 'lib/spree/core/controller_helpers/order.rb', line 71

def current_currency
  Spree::Config[:currency]
end

#current_order(options = {}) ⇒ Object

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


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spree/core/controller_helpers/order.rb', line 20

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

  return @current_order if @current_order

  if session[:order_id]
    current_order = Spree::Order.includes(:adjustments).lock(options[:lock]).find_by(id: session[:order_id], currency: current_currency)
    @current_order = current_order unless current_order.try(:completed?)
  end

  if options[:create_order_if_necessary] and (@current_order.nil? or @current_order.completed?)
    @current_order = Spree::Order.new(currency: current_currency)
    @current_order.user ||= try_spree_current_user
    # See issue #3346 for reasons why this line is here
    @current_order.created_by ||= try_spree_current_user
    @current_order.save!

    # make sure the user has permission to access the order (if they are a guest)
    if try_spree_current_user.nil?
      session[:access_token] = @current_order.token
    end
  end

  if @current_order
    @current_order.last_ip_address = ip_address
    session[:order_id] = @current_order.id
    return @current_order
  end
end

#ip_addressObject


75
76
77
# File 'lib/spree/core/controller_helpers/order.rb', line 75

def ip_address
  request.remote_ip
end

#set_current_orderObject


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

def set_current_order
  if user = try_spree_current_user
    last_incomplete_order = user.last_incomplete_spree_order
    if session[:order_id].nil? && last_incomplete_order
      session[:order_id] = last_incomplete_order.id
    elsif current_order && last_incomplete_order && current_order != last_incomplete_order
      current_order.merge!(last_incomplete_order, user)
    end
  end
end

#simple_current_orderObject

Used in the link_to_cart helper.


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

def simple_current_order
  @simple_current_order ||= Spree::Order.find_by(id: session[:order_id], currency: current_currency)
end