Class: TbCommerce::Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BelongsToUserSession
Defined in:
app/models/tb_commerce/cart.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BelongsToUserSession

#belongs_to?

Class Method Details

.check_abandoned!Object

Look for carts that have outlived the configured TbCommerce.config.cart_lifespan value and mark them as abandoned



17
18
19
20
# File 'app/models/tb_commerce/cart.rb', line 17

def self.check_abandoned!
  carts = in_progress.where('updated_at < ?', DateTime.now - TbCommerce.config.cart_lifespan)
  return carts.update_all(:is_abandoned => true)
end

Instance Method Details

#cart_is_empty?Boolean

Returns:

  • (Boolean)


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

def cart_is_empty?
  return self.cart_items.count == 0
end

#descriptionObject

Build a short description of the cart contents



24
25
26
27
28
29
30
31
32
33
# File 'app/models/tb_commerce/cart.rb', line 24

def description
  if cart_items.length == 0
    desc = "This cart is currently empty"
  elsif cart_items.length > 1
    desc = "#{cart_items.first.description} and #{cart_items.length-1} other #{'item'.pluralize(cart_items.length-1)}"
  else
    desc = cart_items.first.description
  end
  return desc
end

#sales_taxObject



35
36
37
38
39
40
41
# File 'app/models/tb_commerce/cart.rb', line 35

def sales_tax
  if TbCommerce.config.sales_tax > 0
    return TbCommerce.config.sales_tax * subtotal
  else
    return 0
  end
end

#shipping_costObject



43
44
45
46
47
48
49
50
# File 'app/models/tb_commerce/cart.rb', line 43

def shipping_cost
  if TbCommerce.config.flat_rate_shipping > 0
    quantity = self.cart_items.collect(&:quantity).sum()
    return quantity * TbCommerce.config.flat_rate_shipping
  else
    return 0
  end
end

#subtotalObject



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

def subtotal
  return self.cart_items.collect(&:total_price).sum()
end

#total_priceObject



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

def total_price
  return subtotal + sales_tax + shipping_cost
end

#user_full_nameObject



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

def user_full_name
  return self.spud_user.try(:full_name) || 'Guest'
end