Class: Shoppy::CartController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shoppy/cart_controller.rb

Direct Known Subclasses

CartController

Instance Method Summary collapse

Instance Method Details

#deleteObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/shoppy/cart_controller.rb', line 14

def delete
  customer = Customer.find_by(id: params[:customer_id])
  cart_item  = CartItem.find_by(id: params[:cart_item_id])
  if customer && cart_item && cart_item.customer == customer
    cart_item.destroy
    Log.newEvent("User Cart", "An Item has been deleted from '#{customer.name}' cart", current_admin.name)
    flash[:notice] = "Cart Item has been deleted."
    redirect_to "/accounts/#{customer.id}/cart"
  else
    page_not_found
  end
end

#emptyObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/shoppy/cart_controller.rb', line 27

def empty
  customer = Customer.find_by(id: params[:customer_id])
  if customer
    if customer.cart_items.count > 0
      if CartHelper::empty_cart_for_customer(customer) == true
        Log.newEvent("User Cart", "The cart for '#{customer.name}' was emptied", current_admin.name)
        flash[:notice] = "Cart has been emptied"
      else
        flash[:warning] = "Something wrong happend. Try again later"
      end
    else
      flash[:warning] = "Cart is already empty for this customer and can't be re-emptied. Kill the customer instead? 🔫"
    end
    redirect_to "/accounts/#{customer.id}/cart"
  else
    page_not_found
  end
end

#indexObject



5
6
7
8
9
10
11
12
# File 'app/controllers/shoppy/cart_controller.rb', line 5

def index
  @customer = Customer.find_by(id: params[:customer_id])
  if @customer
    @cart_items = @customer.cart_items
  else
    redirect_to "404.html"
  end
end