add trashable concern on user

This commit is contained in:
João Geonizeli
2022-07-31 21:17:22 -03:00
parent 4a2d2d1a67
commit eff96cd102
10 changed files with 221 additions and 168 deletions

View File

@@ -1,6 +1,30 @@
ActiveAdmin.register User do
permit_params :email, :name, roles: []
scope :all, default: true
scope :trashed
controller do
def show
@user = User.unscoped.find_by!(permitted_params[:user])
end
def edit
@user = User.unscoped.find_by!(permitted_params[:user])
end
def destroy
@user = User.unscoped.find(permitted_params[:id])
if @user.deleted_at
redirect_to admin_users_path, notice: t('active_admin.user.already_destroyed')
else
@user.destroy
redirect_to admin_users_path, notice: t('active_admin.user.succesfully_destroyed')
end
end
end
index do
selectable_column
id_column
@@ -8,6 +32,8 @@ ActiveAdmin.register User do
column :name
column :created_at
actions
actions
end
filter :email

View File

@@ -0,0 +1,21 @@
module Trashable
extend ActiveSupport::Concern
included do
default_scope { where(deleted_at: nil) }
end
module ClassMethods
def trashed
self.unscoped.where(self.arel_table[:deleted_at].not_eq(nil))
end
end
def destroy
update_column :deleted_at, Time.now
end
def recover
update_attribute :deleted_at, nil
end
end

View File

@@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# avatar_url :string
# deleted_at :datetime
# email :string default(""), not null
# encrypted_password :string default(""), not null
# name :string not null
@@ -20,6 +21,7 @@
# index_users_on_reset_password_token (reset_password_token) UNIQUE
#
class User < ApplicationRecord
include Trashable
extend Enumerize
devise :database_authenticatable,
@@ -38,7 +40,6 @@ class User < ApplicationRecord
before_validation :set_random_password, on: :create
roles.values.each do |role|
define_method "#{role}?" do
roles.include?(role)