Class: Atreides::Order

Inherits:
Base
  • Object
show all
Includes:
AASM, Extendable
Defined in:
app/models/atreides/order.rb

Constant Summary collapse

BILLING_COLS =

Constants

%w(zip country street city province).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dom_id, #to_param

Instance Attribute Details

#card_expires_onObject

Returns the value of attribute card_expires_on.



17
18
19
# File 'app/models/atreides/order.rb', line 17

def card_expires_on
  @card_expires_on
end

#card_numberObject

Returns the value of attribute card_number.



17
18
19
# File 'app/models/atreides/order.rb', line 17

def card_number
  @card_number
end

#card_typeObject

Returns the value of attribute card_type.



17
18
19
# File 'app/models/atreides/order.rb', line 17

def card_type
  @card_type
end

#card_verificationObject

Returns the value of attribute card_verification.



17
18
19
# File 'app/models/atreides/order.rb', line 17

def card_verification
  @card_verification
end

Class Method Details

.build_order_for_user(user) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/atreides/order.rb', line 77

def build_order_for_user(user)
  # Get address info if user has previous orders
  attrs = {}
  if old_order = user.orders.first
    old_order.attributes.each{|k,v| attrs[k] = v if BILLING_COLS.include?(k) }
  end

  # Build new object
  new(attrs.update({
    :user       => user,
    :first_name => user.first_name,
    :last_name  => user.last_name,
    :line_items => user.cart_items,
    :amount     => Money.new(user.cart_items.sum(:price_cents)),
    :ip_address => user.
  }))
end

Instance Method Details

#addressObject



108
109
110
# File 'app/models/atreides/order.rb', line 108

def address
  %w(street city province zip country).map{|f| self.send(f) }.compact.join("\n")
end

#full_nameObject

Instance Methods



99
100
101
# File 'app/models/atreides/order.rb', line 99

def full_name
  [first_name, last_name].join(' ')
end

#numberObject



103
104
105
106
# File 'app/models/atreides/order.rb', line 103

def number
  # TODO: Format this as something more user friendly
  [id, created_at].map(&:to_i).join('-')
end

#total_amountsObject



112
113
114
115
116
117
118
# File 'app/models/atreides/order.rb', line 112

def total_amounts
  # Set price
  self.amount = line_items.to_a.sum{ |i| i.total_price }

  # Set final amount to be paid
  self.final_amount = amount - discount
end