diff --git a/app/admin/questions.rb b/app/admin/questions.rb index 48d11a3..ea6a451 100644 --- a/app/admin/questions.rb +++ b/app/admin/questions.rb @@ -1,6 +1,30 @@ ActiveAdmin.register Question do permit_params :authorship, :authorship_year, :check_type, :difficulty, :status, :subject_id + scope :all, default: true + scope :trashed + + controller do + def show + @question = Question.unscoped.find_by!(permitted_params[:question]) + end + + def edit + @question = Question.unscoped.find_by!(permitted_params[:question]) + end + + def destroy + @question = Question.unscoped.find(permitted_params[:id]) + + if @question.deleted_at + redirect_to admin_questions_path, notice: t('active_admin.question.already_destroyed') + else + @question.destroy + redirect_to admin_questions_path, notice: t('active_admin.question.succesfully_destroyed') + end + end + end + index do selectable_column id_column diff --git a/app/models/question.rb b/app/models/question.rb index 59e37aa..5cce21c 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -32,6 +32,7 @@ # fk_rails_... (user_id => users.id) # class Question < ApplicationRecord + include Trashable extend Enumerize belongs_to :user diff --git a/config/locales/activeadmin/pt-BR.yml b/config/locales/activeadmin/pt-BR.yml index edc7599..3ab4e2f 100644 --- a/config/locales/activeadmin/pt-BR.yml +++ b/config/locales/activeadmin/pt-BR.yml @@ -147,3 +147,6 @@ pt-BR: user: succesfully_destroyed: 'Usuário removido com sucesso.' already_destroyed: "Usuário já está removido." + question: + succesfully_destroyed: 'Questão removido com sucesso.' + already_destroyed: "Questão já está removido." diff --git a/db/migrate/20220805233401_add_deleted_at_to_question.rb b/db/migrate/20220805233401_add_deleted_at_to_question.rb new file mode 100644 index 0000000..381d80d --- /dev/null +++ b/db/migrate/20220805233401_add_deleted_at_to_question.rb @@ -0,0 +1,5 @@ +class AddDeletedAtToQuestion < ActiveRecord::Migration[7.0] + def change + add_column :questions, :deleted_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index f445e42..fd29e44 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_31_232807) do +ActiveRecord::Schema[7.0].define(version: 2022_08_05_233401) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -93,6 +93,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_31_232807) do t.text "support" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "deleted_at" t.index ["subject_id"], name: "index_questions_on_subject_id" t.index ["user_id"], name: "index_questions_on_user_id" end diff --git a/scripts/setup b/scripts/setup index 7d07830..43e078b 100755 --- a/scripts/setup +++ b/scripts/setup @@ -45,7 +45,7 @@ fi ###################### yellow_text 'Preparing the database...' -docker-compose run --rm web rails db:create +docker-compose run --rm rails rails db:create green_text 'Everything ready to run!'