From 5ef98c173358d08a7577ce09be9e1fec57c4aecc Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 27 Jul 2022 21:17:55 +0200 Subject: [PATCH] Add soft deletes to reportable models --- app/models/answer.rb | 4 +++- app/models/appendable.rb | 2 ++ app/models/comment.rb | 2 ++ app/models/question.rb | 2 ++ app/models/user.rb | 2 ++ db/migrate/20220727174919_add_soft_deletes.rb | 9 +++++++++ db/schema.rb | 12 +++++++++++- 7 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20220727174919_add_soft_deletes.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index ddd5fe98..e7cca592 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -1,6 +1,8 @@ class Answer < ApplicationRecord extend Answer::TimelineMethods + acts_as_paranoid + belongs_to :user belongs_to :question has_many :comments, dependent: :destroy @@ -21,7 +23,7 @@ class Answer < ApplicationRecord before_destroy do # mark a report as deleted if it exists - rep = Report.where(target_id: self.id, type: 'Reports::Answer') + rep = Report.where(target_id: id, type: "Reports::Answer") rep.each do |r| unless r.nil? r.deleted = true diff --git a/app/models/appendable.rb b/app/models/appendable.rb index f736acfa..f7cc99cb 100644 --- a/app/models/appendable.rb +++ b/app/models/appendable.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class Appendable < ApplicationRecord + acts_as_paranoid + belongs_to :parent, polymorphic: true belongs_to :user end diff --git a/app/models/comment.rb b/app/models/comment.rb index 127524ef..39abb8ca 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,4 +1,6 @@ class Comment < ApplicationRecord + acts_as_paranoid + belongs_to :user belongs_to :answer validates :user_id, presence: true diff --git a/app/models/question.rb b/app/models/question.rb index f9b8e4c3..b116f4ad 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -1,6 +1,8 @@ class Question < ApplicationRecord include Question::AnswerMethods + acts_as_paranoid + belongs_to :user, optional: true has_many :answers, dependent: :destroy has_many :inboxes, dependent: :destroy diff --git a/app/models/user.rb b/app/models/user.rb index a12c8248..e0a70292 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,6 +13,8 @@ class User < ApplicationRecord include User::TimelineMethods include ActiveModel::OneTimePassword + acts_as_paranoid + # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :async, :registerable, diff --git a/db/migrate/20220727174919_add_soft_deletes.rb b/db/migrate/20220727174919_add_soft_deletes.rb new file mode 100644 index 00000000..1bab6382 --- /dev/null +++ b/db/migrate/20220727174919_add_soft_deletes.rb @@ -0,0 +1,9 @@ +class AddSoftDeletes < ActiveRecord::Migration[6.1] + def change + %i[answers appendables comments questions users].each do |table| + say "Migrating #{table}" + add_column table, :deleted_at, :datetime + add_index table, :deleted_at + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 140139e5..bf44a41f 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.define(version: 2022_07_20_190421) do +ActiveRecord::Schema.define(version: 2022_07_27_174919) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -46,6 +46,8 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do t.datetime "created_at" t.datetime "updated_at" t.integer "smile_count", default: 0, null: false + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_answers_on_deleted_at" t.index ["question_id"], name: "index_answers_on_question_id" t.index ["user_id", "created_at"], name: "index_answers_on_user_id_and_created_at" end @@ -58,6 +60,8 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do t.text "content" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_appendables_on_deleted_at" t.index ["parent_id", "parent_type"], name: "index_appendables_on_parent_id_and_parent_type" t.index ["user_id", "created_at"], name: "index_appendables_on_user_id_and_created_at" end @@ -69,7 +73,9 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do t.datetime "created_at" t.datetime "updated_at" t.integer "smile_count", default: 0, null: false + t.datetime "deleted_at" t.index ["answer_id"], name: "index_comments_on_answer_id" + t.index ["deleted_at"], name: "index_comments_on_deleted_at" t.index ["user_id", "created_at"], name: "index_comments_on_user_id_and_created_at" end @@ -144,6 +150,8 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do t.datetime "updated_at" t.integer "answer_count", default: 0, null: false t.boolean "direct", default: false, null: false + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_questions_on_deleted_at" t.index ["user_id", "created_at"], name: "index_questions_on_user_id_and_created_at" end @@ -292,7 +300,9 @@ ActiveRecord::Schema.define(version: 2022_07_20_190421) do t.datetime "export_created_at" t.string "otp_secret_key" t.integer "otp_module", default: 0, null: false + t.datetime "deleted_at" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true + t.index ["deleted_at"], name: "index_users_on_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 t.index ["screen_name"], name: "index_users_on_screen_name", unique: true