2014-11-06 18:56:00 +01:00
|
|
|
class OrdersController < ApplicationController
|
2014-12-15 18:56:13 +01:00
|
|
|
include OrdersHelper
|
2014-11-10 02:30:42 +01:00
|
|
|
|
2015-01-06 20:18:01 +01:00
|
|
|
load_and_authorize_resource
|
|
|
|
|
2014-11-25 15:27:12 +01:00
|
|
|
def new
|
2014-11-26 11:58:51 +01:00
|
|
|
@user = User.find(params[:user_id])
|
2014-11-25 15:27:12 +01:00
|
|
|
@order = @user.orders.build
|
2014-11-24 21:45:32 +01:00
|
|
|
@products = Product.all
|
2014-12-04 17:02:08 +01:00
|
|
|
@order_products = @order.order_products
|
|
|
|
|
|
|
|
@products.each do |p|
|
2014-12-04 19:28:21 +01:00
|
|
|
@order_products.build(product: p)
|
2014-12-04 17:02:08 +01:00
|
|
|
end
|
2014-11-06 18:56:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-11-26 11:58:51 +01:00
|
|
|
@user = User.find(params[:user_id])
|
2014-12-04 19:28:21 +01:00
|
|
|
@order = @user.orders.build(order_params)
|
2014-12-04 17:02:08 +01:00
|
|
|
@products = Product.all
|
2014-12-05 16:15:07 +01:00
|
|
|
@order_products = @order.order_products
|
2015-01-15 00:39:34 +01:00
|
|
|
|
2014-12-04 19:28:21 +01:00
|
|
|
if @order.save
|
2015-01-15 00:39:34 +01:00
|
|
|
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
2014-12-04 19:28:21 +01:00
|
|
|
redirect_to root_path
|
|
|
|
else
|
|
|
|
render 'new'
|
2014-11-25 15:27:12 +01:00
|
|
|
end
|
2014-11-23 21:12:31 +01:00
|
|
|
end
|
|
|
|
|
2015-01-06 20:33:11 +01:00
|
|
|
def overview
|
2015-01-06 20:18:01 +01:00
|
|
|
@users_by_name = User.members.order(:name)
|
|
|
|
@users_by_order = User.members.order(:orders_count).reverse_order
|
2014-12-06 12:03:08 +01:00
|
|
|
end
|
|
|
|
|
2014-12-10 13:21:06 +01:00
|
|
|
def quickpay
|
2015-01-15 00:39:34 +01:00
|
|
|
user = User.find(params[:user_id])
|
|
|
|
order = user.orders.build
|
|
|
|
order.products << user.dagschotel
|
2014-12-10 13:21:06 +01:00
|
|
|
if order.save
|
|
|
|
flash[:success] = "Quick pay succeeded"
|
|
|
|
else
|
2015-01-15 00:39:34 +01:00
|
|
|
flash[:error] = order.errors.full_messages.first
|
2014-12-10 13:21:06 +01:00
|
|
|
end
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
|
2014-11-10 02:30:42 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def order_params
|
2015-02-04 16:10:47 +01:00
|
|
|
params.require(:order).permit(order_products_attributes: [:count, product_attributes: [:id, :price, :stock]])
|
2014-11-10 02:30:42 +01:00
|
|
|
end
|
2014-11-06 18:56:00 +01:00
|
|
|
end
|