Sha256: ab176ba269307e1066cc1b45b5d1d5d0e965a0fe5406cdc5a63d803c05dfbf31

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module RightnowOms
  class OrdersController < ApplicationController
    before_filter :load_cart, only: :create

    def index
      orders = Order.search(params[:q])
      orders.sorts = "created_at desc"

      respond_to do |format|
        format.json { render_for_api :default, json: orders.result, root: :orders, status: :ok }
      end
    end

    def show
      @order = Order.find(params[:id])
    end

    def create
      @order = Order.new_with_items(prepare_order)

      respond_to do |format|
        if @order.save
          @cart.destroy if @cart

          format.html { redirect_to @order }
          format.json { render_for_api :default, json: @order, root: :order, status: :ok }
        else
          format.html { redirect_to ::RightnowOms.config.new_order_url }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
    end

    private
    def prepare_order
      return params[:order] if params[:order].has_key?(:order_items) || @cart.nil?

      items = @cart.cart_items.inject([]) do |c, i|
        item = { name: i.name, price: i.price, quantity: i.quantity }
        unless i.children.empty?
          children = i.children.inject([]) { |m, v| m << { name: v.name, price: v.price, quantity: v.quantity }}
          item[:children] = children
        end

        c << item
      end
      params[:order][:order_items] = items

      params[:order]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rightnow_oms-0.1.6 app/controllers/rightnow_oms/orders_controller.rb