tap/app/controllers/orders_controller.rb

63 lines
1.6 KiB
Ruby
Raw Normal View History

2014-11-06 18:56:00 +01:00
class OrdersController < ApplicationController
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
2015-02-09 17:06:24 +01:00
@order_items = @order.order_items
2014-12-04 17:02:08 +01:00
@products.each do |p|
2015-02-09 17:06:24 +01:00
@order_items.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
2015-02-09 17:06:24 +01:00
@order_items = @order.order_items
2014-12-04 19:28:21 +01:00
if @order.save
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
def destroy
order = Order.find(params[:id])
if order.created_at > 5.minutes.ago
order.destroy
flash[:success] = "Order has been removed."
else
flash[:error] = "This order has been placed too long ago, it can't be removed. Please contact a sysadmin."
end
redirect_to koelkast_root_path
end
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
def quickpay
user = User.find(params[:user_id])
order = user.orders.build
2015-02-10 08:33:51 +01:00
order.order_items << OrderItem.new(count: 1, product: user.dagschotel, order: order)
if order.save
flash[:success] = "Quick pay succeeded. #{view_context.link_to("Undo", [user, order], method: :delete)}."
else
flash[:error] = order.errors.full_messages.first
end
redirect_to root_path
end
2014-11-10 02:30:42 +01:00
private
def order_params
2015-02-10 09:00:29 +01:00
params.require(:order).permit(order_items_attributes: [:count, product_attributes: [:id, :price_cents, :stock]])
2014-11-10 02:30:42 +01:00
end
2014-11-06 18:56:00 +01:00
end