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

51 lines
1.1 KiB
Ruby
Raw Normal View History

2016-08-17 17:49:09 -05:00
module Admin
class UserController < AdminController
def index
2016-08-18 15:35:17 -05:00
@users = User.order(:name)
2016-08-17 17:49:09 -05:00
end
def new
2016-08-18 15:35:17 -05:00
@user = User.new
2016-08-17 17:49:09 -05:00
end
def create
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-08-24 16:26:07 -05:00
UserMailer.welcome(@user, default_passwd).deliver_now
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-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-08-17 17:49:09 -05:00
end
def update
2016-08-18 15:35:17 -05:00
@user = User.find(params[:user_id])
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
params.require(:user).permit(:name, :email, :role, :password)
end
2016-08-17 17:49:09 -05:00
end
end