Class: TbCheckout::Cart
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- TbCheckout::Cart
- Includes:
- BelongsToUserSession
- Defined in:
- app/models/tb_checkout/cart.rb
Class Method Summary collapse
-
.check_abandoned! ⇒ Object
Look for carts that have outlived the configured TbCheckout.config.cart_lifespan value and mark them as abandoned.
Instance Method Summary collapse
- #add_to_cart(product, quantity: 1) ⇒ Object
-
#description ⇒ Object
Build a short description of the cart contents.
- #is_empty? ⇒ Boolean
- #total_price ⇒ Object
- #user_full_name ⇒ Object
Methods included from BelongsToUserSession
Class Method Details
.check_abandoned! ⇒ Object
Look for carts that have outlived the configured TbCheckout.config.cart_lifespan value and mark them as abandoned
15 16 17 18 |
# File 'app/models/tb_checkout/cart.rb', line 15 def self.check_abandoned! carts = in_progress.where('updated_at < ?', DateTime.now - TbCheckout.config.cart_lifespan) return carts.update_all(:is_abandoned => true) end |
Instance Method Details
#add_to_cart(product, quantity: 1) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'app/models/tb_checkout/cart.rb', line 37 def add_to_cart(product, quantity: 1) if !product.class.included_modules.map(&:to_s).include?("TbCheckout::Purchasable") raise ArgumentError, 'product must conform to TbCheckout::Purchasable' end return self.cart_items.create({ :item => product, :quantity => quantity }) end |
#description ⇒ Object
Build a short description of the cart contents
22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/tb_checkout/cart.rb', line 22 def description if cart_items.length == 0 desc = "This cart is currently empty" elsif cart_items.length > 1 desc = "#{cart_items.first.item_description} and #{cart_items.length-1} other #{'item'.pluralize(cart_items.length-1)}" else desc = cart_items.first.item_description end return desc end |
#is_empty? ⇒ Boolean
47 48 49 |
# File 'app/models/tb_checkout/cart.rb', line 47 def is_empty? return self.cart_items.count == 0 end |
#total_price ⇒ Object
33 34 35 |
# File 'app/models/tb_checkout/cart.rb', line 33 def total_price return self.cart_items.collect(&:total_price).sum() end |
#user_full_name ⇒ Object
51 52 53 |
# File 'app/models/tb_checkout/cart.rb', line 51 def user_full_name return self.spud_user.try(:full_name) || 'Anonymous' end |