refactor: Username and slug unique indexes, before actions for set link and user

confirm_password_validation around action, unnecessary helpers removed, rubocop-rails suggestions applied

Add username index and unique slug index migrations
This commit is contained in:
Juan Rodriguez
2021-06-15 15:24:34 -05:00
parent 9204abf2e3
commit 73b674b613
22 changed files with 95 additions and 93 deletions
+11 -4
View File
@@ -1,22 +1,29 @@
# frozen_string_literal: true
class UsersController < ApplicationController
def create
if user_params[:password] != params[:user][:confirm_password]
return render json: { password: ['Password not match with Confirm Password'] }, status: :bad_request
end
around_action :confirm_password_validation, only: %i[create]
def create
@user = User.create(user_params)
if @user.errors.any?
render json: @user.errors, status: :unprocessable_entity
else
session[:user_id] = @user.id
session[:username] = @user.username
render json: nil, status: :ok
end
end
private
def confirm_password_validation
if user_params[:password] == params[:user][:confirm_password]
yield
else
render json: { password: ['Password not match with Confirm Password'] }, status: :bad_request
end
end
def user_params
params.require(:user).permit(:username, :password)
end