Class: ShoppingCart::OrderItemsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shopping_cart/order_items_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/shopping_cart/order_items_controller.rb', line 10

def create
  model = params['model'].constantize
  product = model.find(params['id'])
  return unless product.respond_to?(:price) && OrderItem.new(quantity: params[:quantity]).valid?
  @current_order ||= Order.create(user: @user)
  order_item = @current_order.order_items.find_by(productable: product)
  if order_item
    order_item.update(quantity: params[:quantity])
  else
    @current_order.order_items.create(productable: product,
                                      price: product.price, quantity: params[:quantity])
  end
  flash[:notice] = t('shopping_cart.item_added')
  redirect_to root_path
end

#destroyObject



31
32
33
34
# File 'app/controllers/shopping_cart/order_items_controller.rb', line 31

def destroy
  @order_item.destroy
  redirect_to root_path
end

#destroy_itemsObject



36
37
38
39
# File 'app/controllers/shopping_cart/order_items_controller.rb', line 36

def destroy_items
  @current_order.order_items.destroy_all
  redirect_to root_path
end

#discountObject



41
42
43
44
45
46
47
48
# File 'app/controllers/shopping_cart/order_items_controller.rb', line 41

def discount
  discount = Discount.find_by_code(params[:code])
  if discount && discount.order_id.nil?
    discount.update(order: @current_order)
    flash[:notice] = t('shopping_cart.valid_coupon', amount: discount.amount)
  end
  redirect_to root_path
end

#updateObject



26
27
28
29
# File 'app/controllers/shopping_cart/order_items_controller.rb', line 26

def update
  flash[:notice] = t('shopping_cart.cart_updated') if @order_item.update(quantity: params[:quantity])
  redirect_to root_path
end