Make reportable models discardable

This commit is contained in:
Karina Kwiatek 2022-07-28 21:00:12 +02:00
parent 397ea9eaa4
commit fab3a9323f
7 changed files with 28 additions and 2 deletions

View file

@ -1,4 +1,5 @@
class Answer < ApplicationRecord
include Discard::Model
extend Answer::TimelineMethods
belongs_to :user
@ -21,7 +22,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

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Appendable < ApplicationRecord
include Discard::Model
belongs_to :parent, polymorphic: true
belongs_to :user
end

View file

@ -1,4 +1,6 @@
class Comment < ApplicationRecord
include Discard::Model
belongs_to :user
belongs_to :answer
validates :user_id, presence: true

View file

@ -1,5 +1,6 @@
class Question < ApplicationRecord
include Question::AnswerMethods
include Discard::Model
belongs_to :user, optional: true
has_many :answers, dependent: :destroy

View file

@ -12,6 +12,7 @@ class User < ApplicationRecord
include User::RelationshipMethods
include User::TimelineMethods
include ActiveModel::OneTimePassword
include Discard::Model
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable

View file

@ -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, :discarded_at, :datetime
add_index table, :discarded_at
end
end
end

View file

@ -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 "discarded_at"
t.index ["discarded_at"], name: "index_answers_on_discarded_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 "discarded_at"
t.index ["discarded_at"], name: "index_appendables_on_discarded_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 "discarded_at"
t.index ["answer_id"], name: "index_comments_on_answer_id"
t.index ["discarded_at"], name: "index_comments_on_discarded_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 "discarded_at"
t.index ["discarded_at"], name: "index_questions_on_discarded_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 "discarded_at"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["discarded_at"], name: "index_users_on_discarded_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