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
# File 'lib/spree/core/controller_helpers/order.rb', line 5

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

Instance Method Details

#associate_userObject


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spree/core/controller_helpers/order.rb', line 39

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

  # This will trigger any "first order" promotions to be triggered
  # Assuming of course that this session variable was set correctly in
  # the authentication provider's registrations controller
  if session[:spree_user_signup] && @order
    fire_event('spree.user.signup', user: try_spree_current_user, order: @order)
    session[:spree_user_signup] = nil
  end

  session[:guest_token] = nil
end

#current_currencyObject


67
68
69
# File 'lib/spree/core/controller_helpers/order.rb', line 67

def current_currency
  Spree::Config[:currency]
end

#current_order(create_order_if_necessary = false) ⇒ Object

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


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/spree/core/controller_helpers/order.rb', line 14

def current_order(create_order_if_necessary = false)
  return @current_order if @current_order
  if session[:order_id]
    current_order = Spree::Order.includes(:adjustments).find_by(id: session[:order_id], currency: current_currency)
    @current_order = current_order unless current_order.try(:completed?)
  end
  if 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


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

def ip_address
  request.env['HTTP_X_REAL_IP'] || request.env['REMOTE_ADDR']
end

#set_current_orderObject


56
57
58
59
60
61
62
63
64
65
# File 'lib/spree/core/controller_helpers/order.rb', line 56

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(true) && last_incomplete_order && current_order != last_incomplete_order
      current_order.merge!(last_incomplete_order)
    end
  end
end