diff --git a/app/models/user.rb b/app/models/user.rb index 25599da56..33fc07482 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -238,13 +238,11 @@ class User < ApplicationRecord end def status - if active == false - :deactivated - elsif confirmed? == false - :unconfirmed - else - :active - end + return :deleted if discarded_at.present? + return :deactivated unless active + return :unconfirmed if !confirmed? + + :active end protected diff --git a/db/migrate/20240305091927_add_discarded_at_column_to_users.rb b/db/migrate/20240305091927_add_discarded_at_column_to_users.rb new file mode 100644 index 000000000..469c4e145 --- /dev/null +++ b/db/migrate/20240305091927_add_discarded_at_column_to_users.rb @@ -0,0 +1,5 @@ +class AddDiscardedAtColumnToUsers < ActiveRecord::Migration[7.0] + def change + add_column :users, :discarded_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index da9c4b0aa..cca8b7f5c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -763,6 +763,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_03_19_122706) do t.datetime "confirmation_sent_at" t.string "unconfirmed_email" t.boolean "initial_confirmation_sent" + t.datetime "discarded_at" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["encrypted_otp_secret_key"], name: "index_users_on_encrypted_otp_secret_key", unique: true diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 774a7893b..f00aa1c7c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -517,5 +517,14 @@ RSpec.describe User, type: :model do expect(user.status).to eq(:active) end + + context "when the user is deleted" do + let(:user) { create(:user, discarded_at: Time.zone.yesterday) } + + it "returns the status of the user" do + user.destroy! + expect(user.status).to eq(:deleted) + end + end end end