Class: Effective::CartsController

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

Instance Method Summary collapse

Instance Method Details

#add_to_cartObject



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

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

  EffectiveOrders.authorize!(self, :update, current_cart)

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

    current_cart.add(@purchasable, quantity: [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

  redirect_back_or_to_cart
end

#destroyObject



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

def destroy
  @cart = current_cart

  EffectiveOrders.authorize!(self, :destroy, @cart)

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

  redirect_back_or_to_cart
end

#remove_from_cartObject



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

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

  EffectiveOrders.authorize!(self, :update, current_cart)

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

  redirect_back_or_to_cart
end

#showObject



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

def show
  @cart = current_cart
  @pending_orders = Effective::Order.pending.where(user: current_user) if current_user.present?

  @page_title ||= 'My Cart'
  EffectiveOrders.authorize!(self, :show, @cart)
end