Class: CartItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cart_item.rb

Overview

Schema Information

Table name: cart_items

id           :integer(4)      not null, primary key
user_id      :integer(4)
cart_id      :integer(4)
variant_id   :integer(4)      not null
quantity     :integer(4)      default(1)
active       :boolean(1)      default(TRUE)
item_type_id :integer(4)      not null
created_at   :datetime
updated_at   :datetime

Constant Summary

QUANTITIES =
[1,2,3,4]

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) inactivate!

Call this method to soft delete an item in the cart

Parameters:

  • (none)

Returns:

  • (Boolean)


52
53
54
# File 'app/models/cart_item.rb', line 52

def inactivate!
  self.update_attributes(:active => false)
end

- (Object) name



36
37
38
# File 'app/models/cart_item.rb', line 36

def name
  variant.product_name
end

- (Float) price

Call this if you need to know the unit price of an item

Parameters:

  • (none)

Returns:

  • (Float)

    price of the variant in the cart



32
33
34
# File 'app/models/cart_item.rb', line 32

def price
  self.variant.price
end

- (Object) shipping_rate



64
65
66
# File 'app/models/cart_item.rb', line 64

def shipping_rate
  variant.product.shipping_rate
end

- (Boolean) shopping_cart_item?

Call this method to determine if an item is in the shopping cart and active

Parameters:

  • (none)

Returns:

  • (Boolean)


60
61
62
# File 'app/models/cart_item.rb', line 60

def shopping_cart_item?
  item_type_id == ItemType::SHOPPING_CART_ID && active?
end

- (Float) total

Call this method if you need the price of an item before taxes

Parameters:

  • (none)

Returns:

  • (Float)

    price of the variant in the cart times quantity



44
45
46
# File 'app/models/cart_item.rb', line 44

def total
  self.price * self.quantity
end