2014-11-06 17:56:00 +00:00
|
|
|
class OrdersController < ApplicationController
|
2015-08-31 13:35:59 +00:00
|
|
|
load_resource :user
|
2016-02-02 14:20:04 +00:00
|
|
|
load_and_authorize_resource :order, through: :user, shallow: true, only: [:overview, :destroy]
|
|
|
|
load_and_authorize_resource :order, through: :user, only: [:new, :create]
|
2015-01-06 19:18:01 +00:00
|
|
|
|
2014-11-25 14:27:12 +00:00
|
|
|
def new
|
2015-09-20 19:21:18 +00:00
|
|
|
@products = Product.all.for_sale.order(:name)
|
|
|
|
@order.products << @products
|
2014-11-06 17:56:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2016-02-02 14:20:04 +00:00
|
|
|
@order.user = @user
|
2014-12-04 18:28:21 +00:00
|
|
|
if @order.save
|
2015-11-30 17:20:54 +00:00
|
|
|
flash[:success] = @order.flash_success
|
2014-12-04 18:28:21 +00:00
|
|
|
redirect_to root_path
|
|
|
|
else
|
2015-09-20 19:21:18 +00:00
|
|
|
@products = Product.all.for_sale.order(:name)
|
2014-12-04 18:28:21 +00:00
|
|
|
render 'new'
|
2014-11-25 14:27:12 +00:00
|
|
|
end
|
2014-11-23 20:12:31 +00:00
|
|
|
end
|
|
|
|
|
2015-02-04 18:03:50 +00:00
|
|
|
def destroy
|
2015-09-14 15:52:25 +00:00
|
|
|
@order.destroy
|
|
|
|
flash[:success] = "Order has been removed."
|
2015-02-12 13:39:58 +00:00
|
|
|
redirect_to root_path
|
2015-02-04 18:03:50 +00:00
|
|
|
end
|
|
|
|
|
2015-01-06 19:33:11 +00:00
|
|
|
def overview
|
2015-09-14 15:52:25 +00:00
|
|
|
@users = User.members.publik.order(:name)
|
2015-10-28 15:57:11 +00:00
|
|
|
@last = Order.order(:created_at).reverse_order.includes(:user).limit(10).map(&:user)
|
2014-12-06 11:03:08 +00:00
|
|
|
end
|
|
|
|
|
2014-11-10 01:30:42 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def order_params
|
2015-10-28 21:07:48 +00:00
|
|
|
params.require(:order).permit(order_items_attributes: [:count, :price, :product_id])
|
2014-11-10 01:30:42 +00:00
|
|
|
end
|
2014-11-06 17:56:00 +00:00
|
|
|
end
|