skill-assessment-app/app/controllers/admin/user_controller.rb
2017-03-06 13:35:45 -06:00

62 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Admin
class UserController < AdminController
def index
@users = policy_scope User.order("#{sort_column} #{sort_direction}")
.page(params[:page])
end
def new
@user = User.new
authorize @user
end
def create
authorize User
default_passwd = SecureRandom.urlsafe_base64(12)
@user = User.create({ password: default_passwd }.merge(user_params.to_h))
if @user.persisted?
UserMailer.welcome(@user, default_passwd).deliver_later
redirect_to admin_users_path, flash: { success: "Sucessfully created user #{@user.name}" }
else
flash[:error] = "Failed to save user."
render :new
end
end
def view
@user = User.find(params[:user_id])
authorize @user
end
def edit
@user = User.find(params[:user_id])
authorize @user
end
def update
@user = User.find(params[:user_id])
authorize @user
if @user.update_attributes(user_params)
redirect_to admin_user_path(@user.to_i),
flash: { success: "Sucessfully updated #{@user.name}" }
else
flash[:error] = "Failed to update user."
render :edit
end
end
private
def user_params
params.require(:user).permit(policy(User).permitted_attributes)
end
def sort_column
User.column_names.include?(params[:sort]) ? params[:sort] : 'name'
end
end
end