2014-11-06 18:56:00 +01:00
|
|
|
class OrdersController < ApplicationController
|
2015-08-31 15:35:59 +02:00
|
|
|
load_resource :user
|
2015-10-27 16:31:10 +01:00
|
|
|
load_and_authorize_resource :order, through: :user, shallow: true
|
2015-01-06 20:18:01 +01:00
|
|
|
|
2014-11-25 15:27:12 +01:00
|
|
|
def new
|
2015-09-20 21:21:18 +02:00
|
|
|
@products = Product.all.for_sale.order(:name)
|
|
|
|
@order.products << @products
|
2014-11-06 18:56:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2014-12-04 19:28:21 +01:00
|
|
|
if @order.save
|
2015-08-31 14:33:15 +02:00
|
|
|
flash[:success] = "#{@order.to_sentence} ordered. Enjoy it!"
|
2014-12-04 19:28:21 +01:00
|
|
|
redirect_to root_path
|
|
|
|
else
|
2015-09-20 21:21:18 +02:00
|
|
|
@products = Product.all.for_sale.order(:name)
|
2014-12-04 19:28:21 +01:00
|
|
|
render 'new'
|
2014-11-25 15:27:12 +01:00
|
|
|
end
|
2014-11-23 21:12:31 +01:00
|
|
|
end
|
|
|
|
|
2015-02-04 19:03:50 +01:00
|
|
|
def destroy
|
2015-09-14 17:52:25 +02:00
|
|
|
@order.destroy
|
|
|
|
flash[:success] = "Order has been removed."
|
2015-02-12 14:39:58 +01:00
|
|
|
redirect_to root_path
|
2015-02-04 19:03:50 +01:00
|
|
|
end
|
|
|
|
|
2015-01-06 20:33:11 +01:00
|
|
|
def overview
|
2015-09-14 17:52:25 +02:00
|
|
|
@users = User.members.publik.order(:name)
|
2014-12-06 12:03:08 +01:00
|
|
|
end
|
|
|
|
|
2014-11-10 02:30:42 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def order_params
|
2015-03-10 11:37:48 +01:00
|
|
|
params.require(:order).permit(order_items_attributes: [:count, :price, product_attributes: [:id]])
|
2014-11-10 02:30:42 +01:00
|
|
|
end
|
2014-11-06 18:56:00 +01:00
|
|
|
end
|