diff --git a/Dockerfile b/Dockerfile index 4270e7c..eb7b056 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,6 +37,7 @@ COPY Gemfile* package.json yarn.lock $APP_PATH # shared-mime-info ref: https://vtex.slack.com/archives/CE16Q9XRT/p1616716948056900 RUN apk update && \ apk add --no-cache \ + watchman \ git \ build-base \ postgresql-dev \ diff --git a/app/admin/users.rb b/app/admin/users.rb index b063edb..963fbaa 100644 --- a/app/admin/users.rb +++ b/app/admin/users.rb @@ -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 diff --git a/app/models/concerns/trashable.rb b/app/models/concerns/trashable.rb new file mode 100644 index 0000000..6b1c186 --- /dev/null +++ b/app/models/concerns/trashable.rb @@ -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 \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 0148259..6893bc1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/config/locales/activeadmin/pt-BR.yml b/config/locales/activeadmin/pt-BR.yml index e7bde81..edc7599 100644 --- a/config/locales/activeadmin/pt-BR.yml +++ b/config/locales/activeadmin/pt-BR.yml @@ -78,6 +78,9 @@ pt-BR: action_label: "%{title} Selecionado" labels: destroy: "Excluir" + scopes: + all: Ativos + trashed: Excluídos comments: created_at: "Criado em" resource_type: "Tipo de Objeto" @@ -140,4 +143,7 @@ pt-BR: table: "Tabela" block: "Lista" grid: "Grid" - blog: "Blog" \ No newline at end of file + blog: "Blog" + user: + succesfully_destroyed: 'Usuário removido com sucesso.' + already_destroyed: "Usuário já está removido." diff --git a/db/migrate/20220731232807_add_deleted_at_to_user.rb b/db/migrate/20220731232807_add_deleted_at_to_user.rb new file mode 100644 index 0000000..a9fb3f2 --- /dev/null +++ b/db/migrate/20220731232807_add_deleted_at_to_user.rb @@ -0,0 +1,5 @@ +class AddDeletedAtToUser < ActiveRecord::Migration[7.0] + def change + add_column :users, :deleted_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 00eeb43..f445e42 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.0].define(version: 2022_07_25_225343) do +ActiveRecord::Schema[7.0].define(version: 2022_07_31_232807) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -147,6 +147,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_25_225343) do t.datetime "updated_at", null: false t.string "avatar_url" t.string "roles", default: [], array: true + t.datetime "deleted_at" t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/erd.svg b/erd.svg index 5bba33c..885aecb 100644 --- a/erd.svg +++ b/erd.svg @@ -4,30 +4,30 @@ - - + + ProgressTest - -Progress Test - Models + +Progress Test - Models m_ActiveAdmin::Comment - -ActiveAdmin::Comment - -author_id -integer (8) -author_type -string -body -text ∗ -namespace -string ∗ -resource_id -integer (8) -resource_type -string + +ActiveAdmin::Comment + +author_id +integer (8) +author_type +string +body +text ∗ +namespace +string ∗ +resource_id +integer (8) +resource_type +string @@ -43,228 +43,218 @@ m_ActiveStorage::Blob - -ActiveStorage::Blob - -byte_size -integer (8) ∗ -checksum -string ∗ -content_type -string -filename -string ∗ -key -string ∗ -metadata -text -service_name -string ∗ + +ActiveStorage::Blob + +byte_size +integer (8) ∗ +checksum +string ∗ +content_type +string +filename +string ∗ +key +string ∗ +metadata +text +service_name +string ∗ m_ActiveStorage::Blob->m_ActiveStorage::Attachment - + m_ActiveStorage::Blob->m_ActiveStorage::Blob - + m_ActiveStorage::VariantRecord - -ActiveStorage::VariantRecord - -variation_digest -string ∗ + +ActiveStorage::VariantRecord + +variation_digest +string ∗ m_ActiveStorage::VariantRecord->m_ActiveStorage::Attachment - + m_ActiveStorage::VariantRecord->m_ActiveStorage::Blob - - + + m_Axis - -Axis - -name -string ∗ U + +Axis + +name +string ∗ U - + m_Subject - -Subject - -name -string ∗ U + +Subject + +name +string ∗ U - + m_Axis->m_Subject - - + + m_Category - -Category - -name -string ∗ U + +Category + +name +string ∗ U - + m_Category->m_Subject - - + + m_CkEditorUpload - -CkEditorUpload + +CkEditorUpload m_CkEditorUpload->m_ActiveStorage::Attachment - + m_Question - -Question - -alternatives -jsonb ∗ -authorship -string -authorship_year -string -bloom_taxonomy -string -body -text -check_type -string -difficulty -string -explanation -text -instruction -text -intention -text -references -text -status -string ∗ -support -text + +Question + +alternatives +jsonb ∗ +authorship +string +authorship_year +string +bloom_taxonomy +string +body +text +check_type +string +difficulty +string +explanation +text +instruction +text +intention +text +references +text +status +string ∗ +support +text m_ReviewMessage - -ReviewMessage - -content -string ∗ -feedback_type -string + +ReviewMessage + +content +string ∗ +feedback_type +string - + m_Question->m_ReviewMessage - - + + m_ReviewRequest - -ReviewRequest - -answered -string + +ReviewRequest + +answered +string m_Question->m_ReviewRequest - - - - - -m_Role - -Role - -name -string ∗ U - - - -m_User - -User - -email -string ∗ U -encrypted_password -string ∗ -name -string ∗ -remember_created_at -datetime (6,0) -reset_password_sent_at -datetime (6,0) -reset_password_token -string - - - -m_Role->m_User - - - + + - + m_Subject->m_Question - - + + + + + +m_User + +User + +avatar_url +string +deleted_at +datetime (6,0) +email +string ∗ U +encrypted_password +string ∗ +name +string ∗ +remember_created_at +datetime (6,0) +reset_password_sent_at +datetime (6,0) +reset_password_token +string +roles +string - + m_User->m_Question - - + + - + m_User->m_ReviewMessage - - + + m_User->m_ReviewRequest - - - + + + diff --git a/spec/factories/users.rb b/spec/factories/users.rb index bbddcec..bc3ba0b 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3daa4a6..278c1e2 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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