From 7b620114729ff70b209a0b2745cc35cc6540e46b Mon Sep 17 00:00:00 2001 From: Lucas Leandro Date: Fri, 8 Aug 2025 13:29:58 -0300 Subject: [PATCH 1/5] Fix to users soft_delete --- app/models/user.rb | 13 +++++++++++-- config/locales/pt-BR/devise.yml | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 124e143..bcfe131 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,7 +6,7 @@ class User < ApplicationRecord # Include default devise modules. Others available are: # :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, - :recoverable, :rememberable, :validatable, :api, + :recoverable, :rememberable, :api, :omniauthable, :confirmable, :lockable, omniauth_providers: %i[github strava google_oauth2] @@ -16,5 +16,14 @@ class User < ApplicationRecord has_many :procedures, dependent: :destroy has_many :health_insurances, dependent: :destroy - validates :email, uniqueness: { case_sensitive: false } + validates :email, presence: true, format: { with: Devise.email_regexp }, + uniqueness: { case_sensitive: false, conditions: -> { where(deleted_at: nil) } } + + validates :password, presence: true, length: { minimum: 6 }, confirmation: true, if: :password_required? + + private + + def password_required? + !persisted? || !password.nil? || !password_confirmation.nil? + end end diff --git a/config/locales/pt-BR/devise.yml b/config/locales/pt-BR/devise.yml index 5a14562..608eade 100644 --- a/config/locales/pt-BR/devise.yml +++ b/config/locales/pt-BR/devise.yml @@ -29,6 +29,20 @@ pt-BR: user: one: Usuário other: Usuários + errors: + messages: + taken: "já está sendo usado" + blank: "não pode ficar em branco" + too_short: "é muito curta (mínimo de %{count} caracteres)" + invalid: "não é válido" + confirmation: "não confere" + errors: + messages: + taken: "já está sendo usado" + blank: "não pode ficar em branco" + too_short: "é muito curta (mínimo de %{count} caracteres)" + invalid: "não é válido" + confirmation: "não confere" devise: confirmations: confirmed: A sua conta foi confirmada com sucesso. From b548a99af8824beb5946ad75c6e21f3e8a1b4dfa Mon Sep 17 00:00:00 2001 From: Lucas Leandro Date: Thu, 14 Aug 2025 19:53:54 -0300 Subject: [PATCH 2/5] removed validation unique email to schema --- ...14223547_remove_unique_index_from_users_email.rb | 13 +++++++++++++ db/schema.rb | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20250814223547_remove_unique_index_from_users_email.rb diff --git a/db/migrate/20250814223547_remove_unique_index_from_users_email.rb b/db/migrate/20250814223547_remove_unique_index_from_users_email.rb new file mode 100644 index 0000000..c8c317a --- /dev/null +++ b/db/migrate/20250814223547_remove_unique_index_from_users_email.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class RemoveUniqueIndexFromUsersEmail < ActiveRecord::Migration[7.1] + def up + remove_index :users, name: "index_users_on_email" + add_index :users, :email, name: "index_users_on_email" + end + + def down + remove_index :users, name: "index_users_on_email" + add_index :users, :email, name: "index_users_on_email", unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 7dde119..fb108e3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2025_05_28_125806) do +ActiveRecord::Schema[7.1].define(version: 2025_08_14_223547) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "plpgsql" @@ -171,7 +171,7 @@ t.string "unlock_token" t.datetime "locked_at" t.index ["deleted_at"], name: "index_users_on_deleted_at" - t.index ["email"], name: "index_users_on_email", unique: true + t.index ["email"], name: "index_users_on_email" t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end From e5cec7fa4953ecb7640dcfafa2add5b7d31bf2ba Mon Sep 17 00:00:00 2001 From: Lucas Leandro Date: Thu, 14 Aug 2025 20:02:06 -0300 Subject: [PATCH 3/5] added validatable --- app/models/user.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index bcfe131..124e143 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,7 +6,7 @@ class User < ApplicationRecord # Include default devise modules. Others available are: # :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, - :recoverable, :rememberable, :api, + :recoverable, :rememberable, :validatable, :api, :omniauthable, :confirmable, :lockable, omniauth_providers: %i[github strava google_oauth2] @@ -16,14 +16,5 @@ class User < ApplicationRecord has_many :procedures, dependent: :destroy has_many :health_insurances, dependent: :destroy - validates :email, presence: true, format: { with: Devise.email_regexp }, - uniqueness: { case_sensitive: false, conditions: -> { where(deleted_at: nil) } } - - validates :password, presence: true, length: { minimum: 6 }, confirmation: true, if: :password_required? - - private - - def password_required? - !persisted? || !password.nil? || !password_confirmation.nil? - end + validates :email, uniqueness: { case_sensitive: false } end From 593564461d7a1219f0d5d954f74cc97e39469fcb Mon Sep 17 00:00:00 2001 From: Lucas Leandro Date: Mon, 8 Sep 2025 16:08:17 -0300 Subject: [PATCH 4/5] destroy override method --- app/models/user.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 124e143..b5566d8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,5 +16,7 @@ class User < ApplicationRecord has_many :procedures, dependent: :destroy has_many :health_insurances, dependent: :destroy - validates :email, uniqueness: { case_sensitive: false } + def destroy + destroy_fully! + end end From b2d080916d84faa7b443e58d507399257bf69c5a Mon Sep 17 00:00:00 2001 From: Lucas Leandro Date: Mon, 8 Sep 2025 16:19:21 -0300 Subject: [PATCH 5/5] removed test to soft-delete --- spec/models/user_spec.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1dff491..7f39f61 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -5,12 +5,6 @@ RSpec.describe User do subject { build(:user) } - describe "soft delete behavior" do - let(:user) { create(:user) } - - it_behaves_like "acts as paranoid", :user - end - describe "associations" do it { is_expected.to have_many(:event_procedures).dependent(:destroy) } it { is_expected.to have_many(:medical_shifts).dependent(:destroy) }