2014-11-06 16:25:27 +01:00
|
|
|
class UsersController < ApplicationController
|
2015-02-12 14:39:58 +01:00
|
|
|
load_and_authorize_resource
|
2015-09-01 17:40:18 +02:00
|
|
|
before_action :init, only: [:show, :edit, :update]
|
2015-02-12 14:39:58 +01:00
|
|
|
|
2014-11-06 16:25:27 +01:00
|
|
|
def show
|
2015-03-12 13:25:11 +01:00
|
|
|
@categories = @user.products
|
|
|
|
.select("products.category", "sum(order_items.count) as count")
|
|
|
|
.group(:category)
|
2014-11-06 16:25:27 +01:00
|
|
|
end
|
2014-11-06 18:30:53 +01:00
|
|
|
|
2015-03-20 02:21:56 +01:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @user.update_attributes(user_params)
|
2015-03-25 17:49:37 +01:00
|
|
|
flash[:success] = "Successfully updated!"
|
|
|
|
redirect_to @user
|
2015-03-20 02:21:56 +01:00
|
|
|
else
|
2015-09-01 17:40:18 +02:00
|
|
|
@user.reload
|
2015-03-20 02:21:56 +01:00
|
|
|
render 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-23 21:12:31 +01:00
|
|
|
def index
|
2015-01-06 20:18:01 +01:00
|
|
|
@users = User.members
|
2014-11-23 21:12:31 +01:00
|
|
|
end
|
2014-12-09 22:32:54 +01:00
|
|
|
|
|
|
|
def destroy
|
2015-08-31 14:33:15 +02:00
|
|
|
user = User.find(params[:id])
|
|
|
|
user.destroy
|
2014-12-09 22:32:54 +01:00
|
|
|
flash[:success] = "Succesfully removed user"
|
2015-03-12 13:25:11 +01:00
|
|
|
redirect_to users_path
|
2014-12-09 22:32:54 +01:00
|
|
|
end
|
|
|
|
|
2015-03-19 14:59:37 +01:00
|
|
|
def edit_dagschotel
|
|
|
|
@user = User.find(params[:user_id])
|
2015-08-31 14:33:15 +02:00
|
|
|
authorize! :update_dagschotel, @user
|
2015-03-19 14:59:37 +01:00
|
|
|
@dagschotel = @user.dagschotel
|
2015-03-19 16:22:55 +01:00
|
|
|
|
2015-08-25 15:53:27 +02:00
|
|
|
@products = Product.for_sale
|
2015-03-19 16:22:55 +01:00
|
|
|
@categories = Product.categories
|
2015-03-19 14:59:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_dagschotel
|
2015-08-31 14:33:15 +02:00
|
|
|
user = User.find(params[:user_id])
|
|
|
|
authorize! :update_dagschotel, user
|
2015-03-19 16:22:55 +01:00
|
|
|
|
2015-08-31 14:33:15 +02:00
|
|
|
user.dagschotel = Product.find(params[:product_id])
|
|
|
|
user.save
|
2015-03-12 13:25:11 +01:00
|
|
|
|
2015-08-31 14:33:15 +02:00
|
|
|
flash[:success] = "Succesfully updated dagschotel"
|
|
|
|
redirect_to user
|
2014-12-10 14:18:56 +01:00
|
|
|
end
|
2015-03-19 14:59:37 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-03-20 02:21:56 +01:00
|
|
|
def user_params
|
2015-08-31 14:33:15 +02:00
|
|
|
params.require(:user).permit(:avatar, :private)
|
2015-03-20 02:21:56 +01:00
|
|
|
end
|
2015-09-01 17:40:18 +02:00
|
|
|
|
|
|
|
def init
|
|
|
|
@user = User.find_by_id(params[:id]) || current_user
|
|
|
|
@orders = @user.orders
|
|
|
|
.order(:created_at)
|
|
|
|
.reverse_order
|
|
|
|
.paginate(page: params[:page])
|
|
|
|
@products = @user.products
|
|
|
|
.select("products.*", "sum(order_items.count) as count")
|
|
|
|
.group(:product_id)
|
|
|
|
.order("count")
|
|
|
|
.reverse_order
|
|
|
|
end
|
2014-11-06 16:25:27 +01:00
|
|
|
end
|