From e5df0644b83bafdfca3d6f2c672638406707d444 Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Mon, 22 Jan 2024 14:58:30 -0300 Subject: [PATCH] allow to create user in the users controller --- app/controllers/users_controller.rb | 10 ++++++++++ app/models/user.rb | 10 +++++----- db/schema.rb | 1 - spec/factories/users.rb | 1 + 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9bb6df3..06f2f1f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -17,6 +17,16 @@ def show # This will use app/views/users/show.json.jbuilder end + def create + @user = User.new(user_params) + + if @user.save + render :show, status: :created, location: @user + else + render json: @user.errors, status: :unprocessable_entity + end + end + def update ApplicationRecord.transaction do @user.salaries.destroy_all diff --git a/app/models/user.rb b/app/models/user.rb index c3e4fe6..b2164cf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,11 +35,11 @@ class User < ApplicationRecord extend FriendlyId friendly_id :name, use: :slugged - validates :email, presence: true, uniqueness: true, - format: { - with: /\A[\w+\-.]+@#{ENV.fetch('VALID_USER_DOMAIN', nil)}\z/i, - message: "must be a #{ENV.fetch('VALID_USER_DOMAIN', nil)} account" - } + validates :email, presence: true, uniqueness: true + validates :email, format: { + with: /\A[\w+\-.]+@#{ENV.fetch('VALID_USER_DOMAIN', nil)}\z/i, + message: "must be a #{ENV.fetch('VALID_USER_DOMAIN', nil)} account" + }, if: :internal? validates :google_id, presence: true, uniqueness: true, if: :internal? validates :google_id, absence: true, unless: :internal? diff --git a/db/schema.rb b/db/schema.rb index ebdca3e..9c3ad06 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,6 @@ ActiveRecord::Schema[7.0].define(version: 2024_01_22_154129) do # These are extensions that must be enabled in order to support this database - enable_extension "pg_stat_statements" enable_extension "plpgsql" create_table "assignments", force: :cascade do |t| diff --git a/spec/factories/users.rb b/spec/factories/users.rb index de89a4b..f35a58c 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -41,6 +41,7 @@ contract_type { FFaker::Lorem.word } seniority { %w[Intern Junior Midlevel Senior].sample } active { [true, false].sample } + internal { true } profession