app/controllers/bodega/orders_controller.rb in bodega-0.2.0 vs app/controllers/bodega/orders_controller.rb in bodega-0.3.0
- old
+ new
@@ -1,8 +1,7 @@
class Bodega::OrdersController < ApplicationController
helper 'bodega/cart'
- include Bodega::CartHelper
include Bodega::PaymentMethod
before_filter :find_order, only: [:show, :update]
def add
@@ -11,13 +10,17 @@
end
redirect_to root_path
end
def complete
- current_order.payment_id = payment_method.complete!
- current_order.save!
- redirect_to order_path(current_order)
+ if current_order.finalize!(payment_method)
+ current_products.clear
+ redirect_to order_path(current_order)
+ else
+ flash[:error] = "There was a problem processing this order. Your account has not been charged."
+ redirect_to new_order_path
+ end
end
def create
params[:products].each do |product|
update_cart(product)
@@ -27,25 +30,33 @@
else
render :new
end
end
+ def remove
+ current_products.delete params[:product_id]
+ redirect_to :back
+ end
+
protected
def find_order
- raise ActiveRecord::NotFound unless @order = Bodega::Order.where(id: params[:order_id] || params[:id]).first
+ raise ActiveRecord::RecordNotFound unless @order = Bodega::Order.where(identifier: params[:order_id] || params[:id]).first
end
- def update_cart(product)
- product_id = "#{product[:type]}.#{product[:id]}"
- if product[:remove]
+ def update_cart(product_hash)
+ product_id = "#{product_hash[:type]}.#{product_hash[:id]}"
+ if product_hash[:remove]
current_products.delete product_id
else
- if current = current_products[product_id]
- current_quantity = current[:quantity].to_i
+ if current_product = current_products[product_id]
+ current_quantity = current_product[:quantity].to_i
else
current_quantity = 0
end
- new_quantity = product[:quantity] ? product[:quantity] : current_quantity + 1
- current_products[product_id] = product.merge(quantity: new_quantity)
+ new_quantity = product_hash[:quantity] ? product_hash[:quantity].to_i : current_quantity + 1
+ if product = product_hash[:type].constantize.where(id: product_hash[:id], keep_stock: true).first
+ new_quantity = [product.number_in_stock, new_quantity].min
+ end
+ current_products[product_id] = product_hash.merge(quantity: new_quantity)
end
end
end