Class: Effective::CartsController

Inherits:
ApplicationController
  • Object
show all
Includes:
EffectiveCartsHelper
Defined in:
app/controllers/effective/carts_controller.rb

Instance Method Summary collapse

Methods included from EffectiveCartsHelper

#current_cart, #link_to_add_to_cart, #link_to_checkout, #link_to_current_cart, #link_to_empty_cart, #link_to_remove_from_cart, #render_cart, #render_purchasables

Instance Method Details

#add_to_cartObject



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

def add_to_cart
  @purchasable = (add_to_cart_params[:purchasable_type].constantize.find(add_to_cart_params[:purchasable_id].to_i) rescue nil)

  EffectiveOrders.authorized?(self, :update, current_cart)

  begin
    raise "please select a valid #{add_to_cart_params[:purchasable_type] || 'item' }" unless @purchasable

    current_cart.add_to_cart(@purchasable, [add_to_cart_params[:quantity].to_i, 1].max)
    flash[:success] = 'Successfully added item to cart'
  rescue EffectiveOrders::SoldOutException
    flash[:warning] = 'This item is sold out'
  rescue => e
    flash[:danger] = 'Unable to add item to cart: ' + e.message
  end

  request.referrer ? (redirect_to :back) : (redirect_to effective_orders.cart_path)
end

#destroyObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/effective/carts_controller.rb', line 12

def destroy
  @cart = current_cart

  EffectiveOrders.authorized?(self, :destroy, @cart)

  if @cart.destroy
    flash[:success] = 'Successfully emptied cart'
  else
    flash[:danger] = 'Unable to destroy cart:' + e.message
  end

  request.referrer ? (redirect_to :back) : (redirect_to effective_orders.cart_path)
end

#remove_from_cartObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/effective/carts_controller.rb', line 45

def remove_from_cart
  @cart_item = current_cart.cart_items.find(remove_from_cart_params[:id])

  EffectiveOrders.authorized?(self, :update, current_cart)

  if @cart_item.destroy
    flash[:success] = 'Successfully removed item from cart'
  else
    flash[:danger] = 'Unable to remove item from cart: ' + e.message
  end

  request.referrer ? (redirect_to :back) : (redirect_to effective_orders.cart_path)
end

#showObject



6
7
8
9
10
# File 'app/controllers/effective/carts_controller.rb', line 6

def show
  @cart = current_cart
  @page_title ||= 'Shopping Cart'
  EffectiveOrders.authorized?(self, :show, @cart)
end