app/controllers/bodega/orders_controller.rb in bodega-0.3.0 vs app/controllers/bodega/orders_controller.rb in bodega-0.4.0

- old
+ new

@@ -1,62 +1,53 @@ class Bodega::OrdersController < ApplicationController + before_filter :find_order, only: %w(show) + helper 'bodega/cart' - include Bodega::PaymentMethod - before_filter :find_order, only: [:show, :update] - def add - if product = params[:product] + if product = params[:order_product] update_cart(product) end - redirect_to root_path + redirect_to new_order_path end def complete - if current_order.finalize!(payment_method) - current_products.clear - redirect_to order_path(current_order) + if current_order.finalize!(params) + session.delete(:bodega_order_id) + redirect_to order_path(current_order), notice: t('bodega.order_processed') else - flash[:error] = "There was a problem processing this order. Your account has not been charged." - redirect_to new_order_path + redirect_to new_order_path, error: t('bodega.order_failed') end end def create - params[:products].each do |product| - update_cart(product) - end - if params[:checkout] - redirect_to payment_method.checkout_url(complete_url, root_url) + if current_order.update_attributes(params[:order]) + if params[:checkout] + redirect_to current_order.payment_method.checkout_url(complete_order_url, new_order_url) + else + redirect_to new_order_path + end else render :new end end def remove - current_products.delete params[:product_id] + current_order.remove_product params[:product_id] redirect_to :back end + def show + render :edit unless @order.complete? + end + protected def find_order raise ActiveRecord::RecordNotFound unless @order = Bodega::Order.where(identifier: params[:order_id] || params[:id]).first end 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_product = current_products[product_id] - current_quantity = current_product[:quantity].to_i - else - current_quantity = 0 - end - 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) + if current_order.update_product(product_hash) + session[:bodega_order_id] = current_order.identifier end end end