skill-assessment-app/app/controllers/admin/user_controller.rb

62 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-17 17:49:09 -05:00
module Admin
class UserController < AdminController
def index
2017-03-06 13:35:45 -06:00
@users = policy_scope User.order("#{sort_column} #{sort_direction}")
.page(params[:page])
2016-08-17 17:49:09 -05:00
end
def new
2016-08-18 15:35:17 -05:00
@user = User.new
2016-09-20 14:22:20 -05:00
authorize @user
2016-08-17 17:49:09 -05:00
end
def create
2016-09-20 14:22:20 -05:00
authorize User
2016-08-18 15:35:17 -05:00
default_passwd = SecureRandom.urlsafe_base64(12)
@user = User.create({ password: default_passwd }.merge(user_params.to_h))
if @user.persisted?
2016-09-14 14:38:26 -05:00
UserMailer.welcome(@user, default_passwd).deliver_later
2016-08-24 12:15:12 -05:00
redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" }
2016-08-18 15:35:17 -05:00
else
flash[:error] = "Failed to save user."
render :new
end
2016-08-17 17:49:09 -05:00
end
def view
2016-08-18 15:35:17 -05:00
@user = User.find(params[:user_id])
2016-09-20 14:22:20 -05:00
authorize @user
2016-08-17 17:49:09 -05:00
end
def edit
2016-08-18 15:35:17 -05:00
@user = User.find(params[:user_id])
2016-09-20 14:22:20 -05:00
authorize @user
2016-08-17 17:49:09 -05:00
end
def update
2016-08-18 15:35:17 -05:00
@user = User.find(params[:user_id])
2016-09-20 14:22:20 -05:00
authorize @user
2016-08-18 15:35:17 -05:00
if @user.update_attributes(user_params)
redirect_to admin_user_path(@user.to_i),
2016-08-24 12:15:12 -05:00
flash: { success: "Sucessfully updated #{@user.name}" }
2016-08-18 15:35:17 -05:00
else
flash[:error] = "Failed to update user."
render :edit
end
2016-08-17 17:49:09 -05:00
end
2016-08-18 15:35:17 -05:00
private
def user_params
2016-09-21 15:50:02 -05:00
params.require(:user).permit(policy(User).permitted_attributes)
2016-08-18 15:35:17 -05:00
end
2017-03-06 13:35:45 -06:00
def sort_column
User.column_names.include?(params[:sort]) ? params[:sort] : 'name'
end
2016-08-17 17:49:09 -05:00
end
end