Class: CartPage

Inherits:
Page
  • Object
show all
Defined in:
lib/models/cart_page.rb

Instance Method Summary collapse

Instance Method Details

#cartObject



21
22
23
24
25
26
27
28
# File 'lib/models/cart_page.rb', line 21

def cart
  if logged_in?
    @cart ||= site.carts.where(transaction: nil, user: current_user.id).order('created desc').first
  else
    # TODO: need to track by session
    nil
  end
end

#hold_product(product, quantity) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/models/cart_page.rb', line 6

def hold_product(product, quantity)
  # TODO: find_or_create
  # use any existing holds for this product
  hold = site.product_holds.where(product: product.id, cart: cart.id).first
  hold ||= site.product_holds.new(product: product, cart: cart)

  if product.quantity >= quantity
    if product.increment! :quantity, -quantity, :quantity.gt => 0
      hold.quantity += quantity
      return hold.save
    end
  end
  false
end

#productsObject



2
3
4
# File 'lib/models/cart_page.rb', line 2

def products
  cart.product_holds.collect(&:product)
end