Sha256: 5cfb9f11b416640e7d982febbb339915056ff4867df768165afc7c3889296b58

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class Bodega::OrdersController < ApplicationController
  helper 'bodega/cart'
  include Bodega::CartHelper
  include Bodega::PaymentMethod

  before_filter :find_order, only: [:show, :update]

  def add
    if product = params[:product]
      update_cart(product)
    end
    redirect_to root_path
  end

  def complete
    current_order.payment_id = payment_method.complete!
    current_order.save!
    redirect_to order_path(current_order)
  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)
    else
      render :new
    end
  end

  protected
  def find_order
    raise ActiveRecord::NotFound unless @order = Bodega::Order.where(id: params[:order_id] || params[:id]).first
  end

  def update_cart(product)
    product_id = "#{product[:type]}.#{product[:id]}"
    if product[:remove]
      current_products.delete product_id
    else
      if current = current_products[product_id]
        current_quantity = current[: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)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bodega-0.2.0 app/controllers/bodega/orders_controller.rb