Class: Effective::CartsController

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

Instance Method Summary collapse

Instance Method Details

#add_to_cartObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/effective/carts_controller.rb', line 33

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

  EffectiveResources.authorize!(self, :add_to_cart, 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 Exception => e
    flash[:danger] = 'Unable to add item to cart: ' + e.message
  end

  redirect_back_or_to_cart
end

#destroyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/effective/carts_controller.rb', line 19

def destroy
  @cart = current_cart

  EffectiveResources.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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/effective/carts_controller.rb', line 52

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

  EffectiveResources.authorize!(self, :remove_from_cart, 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



11
12
13
14
15
16
17
# File 'app/controllers/effective/carts_controller.rb', line 11

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

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