Class: Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/models/cart.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



2
3
4
# File 'app/models/cart.rb', line 2

def currency
  @currency
end

Instance Method Details

#add(product, quantity = 1) ⇒ Object

Add n number of this product to our cart (addition)



14
15
16
# File 'app/models/cart.rb', line 14

def add(product, quantity=1)
  self.alter_cart_item_quantity(:add, product, quantity.to_i)
end

#alter_cart_item_quantity(change_type, product, quantity) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/cart.rb', line 28

def alter_cart_item_quantity(change_type, product, quantity)
  item = self.items.detect{|item| item.product_id == product.id}
  if item
    if quantity >= 1
      case change_type
        when :add then item.increment_quantity(quantity)
        when :set then item.quantity = quantity
      end
    else
      item.remove_from_cart!
    end
  else
    item = self.items.build(:product_id => product.id, :quantity => quantity)
  end
  item.save!
end

#empty!Object



61
62
63
# File 'app/models/cart.rb', line 61

def empty!
  self.items.each{ |cart_item| cart_item.remove_from_cart! }
end

#empty?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/cart.rb', line 57

def empty?
  self.items.empty?
end

#item_countObject



65
66
67
68
69
# File 'app/models/cart.rb', line 65

def item_count
  count = 0
  self.items.each{|item| count += item.quantity}
  count
end

#itemsObject

Because ‘has_many :cart_items, :as => :items’ does not appear to work



7
# File 'app/models/cart.rb', line 7

def items ; self.cart_items ; end

#paypal_encrypted(return_url, notify_url) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/cart.rb', line 73

def paypal_encrypted(return_url, notify_url)
  values = {
    :invoice    => self.id,
    :notify_url => notify_url,
    :business   => ECO['paypal']['email'],
    :cert_id    => ECO['paypal']['cert_id'],
    :cmd        => '_cart',
    :upload     => 1,
    :weight     => self.shipping_weight
  }

  self.items.each_with_index do |item, index|
    values.merge!({
      "amount_#{index+1}"      => item.unit_price,
      "item_name_#{index+1}"   => item.name,
      "item_number_#{index+1}" => item.id,
      "quantity_#{index+1}"    => item.quantity
    })
  end
  encrypt_for_paypal(values)
end

#purchased?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/models/cart.rb', line 9

def purchased?
  self.purchased_at.present?
end

#remove(product) ⇒ Object

Remove a product no matter what the quantity



19
20
21
# File 'app/models/cart.rb', line 19

def remove(product)
  self.alter_cart_item_quantity(:set, product, 0)
end

#set_item_quantity(product, quantity) ⇒ Object

Set the total number of this product in our cart to quantity. Setting to 0 will remove the item



24
25
26
# File 'app/models/cart.rb', line 24

def set_item_quantity(product, quantity)
  self.alter_cart_item_quantity(:set, product, quantity)
end

#shipping_weightObject



53
54
55
# File 'app/models/cart.rb', line 53

def shipping_weight
  self.items.inject(0) { |sum, cart_item| sum + cart_item.shipping_weight }
end

#totalObject



49
50
51
# File 'app/models/cart.rb', line 49

def total
  self.items.inject(0) { |sum, cart_item| sum + cart_item.price }
end

#totalfObject



45
46
47
# File 'app/models/cart.rb', line 45

def totalf
  Cart.format_price(self.total())
end