Skip to content

Commit

Permalink
refactor(soft-delete-student-class): update the soft-delete config fo…
Browse files Browse the repository at this point in the history
…r dependent models
  • Loading branch information
phungmanhcuong committed Oct 14, 2024
1 parent 9863288 commit 16d69dc
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ playwright/.cache/
coverage/

dump.rdb
.idea
*.iml
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gem 'tzinfo-data', platforms: [:mswin, :mswin64]
gem 'rails', '~> 7.0.8.4'

# Soft delete
gem 'paranoia', '~> 2.4'
gem 'paranoia'

# Use PostgreSQL for the backend
gem 'pg'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ GEM
parallel (1.26.3)
parallel_tests (4.7.1)
parallel
paranoia (3.0.0)
activerecord (>= 6, < 8.1)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
Expand Down Expand Up @@ -631,6 +633,7 @@ DEPENDENCIES
mini_magick
nokogiri (>= 1.8.1)
parallel_tests
paranoia
pg
puma
rack-cors
Expand Down
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/course/achievement.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Course::Achievement < ApplicationRecord
include Course::SanitizeDescriptionConcern

acts_as_paranoid
acts_as_conditional
mount_uploader :badge, ImageUploader
has_many_attachments on: :description
Expand Down
1 change: 1 addition & 0 deletions app/models/course/experience_points_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
class Course::ExperiencePointsRecord < ApplicationRecord
include Generic::CollectionConcern
actable optional: true
acts_as_paranoid

before_save :send_notification, if: :reached_new_level?
before_create :set_awarded_attributes, if: :manually_awarded?
Expand Down
2 changes: 1 addition & 1 deletion app/models/course/group_user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class Course::GroupUser < ApplicationRecord
after_initialize :set_defaults, if: :new_record?

acts_as_paranoid
enum role: { normal: 0, manager: 1 }

validate :course_user_and_group_in_same_course
Expand Down
1 change: 1 addition & 0 deletions app/models/course/learning_rate_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Course::LearningRateRecord < ApplicationRecord
validates :effective_max, presence: true, numericality: true
validates :course_user, presence: true
validate :learning_rate_between_effective_min_and_max
acts_as_paranoid

belongs_to :course_user, inverse_of: :learning_rate_records

Expand Down
2 changes: 1 addition & 1 deletion app/models/course/personal_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class Course::PersonalTime < ApplicationRecord
belongs_to :course_user, inverse_of: :personal_times
belongs_to :lesson_plan_item, class_name: 'Course::LessonPlan::Item', inverse_of: :personal_times

acts_as_paranoid
validates :start_at, presence: true
validates :course_user, presence: true, uniqueness: { scope: :lesson_plan_item }
validates :lesson_plan_item, presence: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/course/user_achievement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class Course::UserAchievement < ApplicationRecord
after_initialize :set_defaults, if: :new_record?
after_create :send_notification

acts_as_paranoid
validate :validate_course_user_in_course, on: :create
validates :obtained_at, presence: true
validates :course_user_id, uniqueness: { scope: [:achievement_id], allow_nil: true,
Expand Down
1 change: 1 addition & 0 deletions app/models/course/user_email_unsubscription.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
class Course::UserEmailUnsubscription < ApplicationRecord
validates :course_user, presence: true
acts_as_paranoid

belongs_to :course_user, inverse_of: :email_unsubscriptions
belongs_to :course_setting_email, class_name: 'Course::Settings::Email',
Expand Down
11 changes: 11 additions & 0 deletions app/models/course_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ def self.user?(user)
all.exists?(user: user)
end

# Soft delete the course user.
def destroy
experience_points_records.each(&:destroy) if experience_points_records.exists?
learning_rate_records.each(&:destroy) if learning_rate_records.exists?
course_user_achievements.each(&:destroy) if course_user_achievements.exists?
email_unsubscriptions.each(&:destroy) if email_unsubscriptions.exists?
group_users.each(&:destroy) if group_users.exists?
personal_times.each(&:destroy) if personal_times.exists?
super
end

# Test whether this course_user is a manager (i.e. manager or owner)
#
# @return [Boolean] True if course_user is a staff
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20241014031756_add_deleted_at_to_course_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseUsers < ActiveRecord::Migration[7.0]
def change
add_column :course_users, :deleted_at, :datetime
add_index :course_users, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseExperiencePointsRecords < ActiveRecord::Migration[7.0]
def change
add_column :course_experience_points_records, :deleted_at, :datetime
add_index :course_experience_points_records, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseLearningRateRecords < ActiveRecord::Migration[7.0]
def change
add_column :course_learning_rate_records, :deleted_at, :datetime
add_index :course_learning_rate_records, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseUserAchievements < ActiveRecord::Migration[7.0]
def change
add_column :course_user_achievements, :deleted_at, :datetime
add_index :course_user_achievements, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseAchievements < ActiveRecord::Migration[7.0]
def change
add_column :course_achievements, :deleted_at, :datetime
add_index :course_achievements, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseUserEmailUnsubscriptions < ActiveRecord::Migration[7.0]
def change
add_column :course_user_email_unsubscriptions, :deleted_at, :datetime
add_index :course_user_email_unsubscriptions, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseGroupUsers < ActiveRecord::Migration[7.0]
def change
add_column :course_group_users, :deleted_at, :datetime
add_index :course_group_users, :deleted_at
end
end
6 changes: 6 additions & 0 deletions db/migrate/20241014031821_add_deleted_at_to_course_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseGroups < ActiveRecord::Migration[7.0]
def change
add_column :course_groups, :deleted_at, :datetime
add_index :course_groups, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCoursePersonalTimes < ActiveRecord::Migration[7.0]
def change
add_column :course_personal_times, :deleted_at, :datetime
add_index :course_personal_times, :deleted_at
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddDeletedAtToCourseReferenceTimelines < ActiveRecord::Migration[7.0]
def change
add_column :course_reference_timelines, :deleted_at, :datetime
add_index :course_reference_timelines, :deleted_at
end
end
24 changes: 22 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_09_17_170847) do
ActiveRecord::Schema[7.0].define(version: 2024_10_14_031828) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "uuid-ossp"
Expand Down Expand Up @@ -68,8 +68,10 @@
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.integer "satisfiability_type", default: 0
t.datetime "deleted_at"
t.index ["course_id"], name: "fk__course_achievements_course_id"
t.index ["creator_id"], name: "fk__course_achievements_creator_id"
t.index ["deleted_at"], name: "index_course_achievements_on_deleted_at"
t.index ["updater_id"], name: "fk__course_achievements_updater_id"
end

Expand Down Expand Up @@ -633,10 +635,12 @@
t.integer "draft_points_awarded"
t.datetime "awarded_at", precision: nil
t.integer "awarder_id"
t.datetime "deleted_at"
t.index ["actable_type", "actable_id"], name: "index_course_experience_points_records_on_actable", unique: true
t.index ["awarder_id"], name: "fk__course_experience_points_records_awarder_id"
t.index ["course_user_id"], name: "fk__course_experience_points_records_course_user_id"
t.index ["creator_id"], name: "fk__course_experience_points_records_creator_id"
t.index ["deleted_at"], name: "index_course_experience_points_records_on_deleted_at"
t.index ["updater_id"], name: "fk__course_experience_points_records_updater_id"
end

Expand Down Expand Up @@ -714,9 +718,11 @@
t.integer "updater_id", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.datetime "deleted_at"
t.index ["course_user_id", "group_id"], name: "index_course_group_users_on_course_user_id_and_course_group_id", unique: true
t.index ["course_user_id"], name: "fk__course_group_users_course_user_id"
t.index ["creator_id"], name: "fk__course_group_users_creator_id"
t.index ["deleted_at"], name: "index_course_group_users_on_deleted_at"
t.index ["group_id"], name: "fk__course_group_users_course_group_id"
t.index ["updater_id"], name: "fk__course_group_users_updater_id"
end
Expand All @@ -729,7 +735,9 @@
t.datetime "updated_at", precision: nil, null: false
t.text "description"
t.bigint "group_category_id", null: false
t.datetime "deleted_at"
t.index ["creator_id"], name: "fk__course_groups_creator_id"
t.index ["deleted_at"], name: "index_course_groups_on_deleted_at"
t.index ["group_category_id", "name"], name: "index_course_groups_on_group_category_id_and_name", unique: true
t.index ["group_category_id"], name: "fk__course_groups_group_category_id"
t.index ["updater_id"], name: "fk__course_groups_updater_id"
Expand All @@ -749,7 +757,9 @@
t.float "effective_max", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
t.index ["course_user_id"], name: "fk__course_learning_rate_records_course_user_id"
t.index ["deleted_at"], name: "index_course_learning_rate_records_on_deleted_at"
end

create_table "course_lesson_plan_event_materials", id: :serial, force: :cascade do |t|
Expand Down Expand Up @@ -909,7 +919,9 @@
t.datetime "bonus_end_at", precision: nil
t.datetime "end_at", precision: nil
t.boolean "fixed", default: false, null: false
t.datetime "deleted_at"
t.index ["course_user_id"], name: "index_course_personal_times_on_course_user_id"
t.index ["deleted_at"], name: "index_course_personal_times_on_deleted_at"
t.index ["lesson_plan_item_id"], name: "index_course_personal_times_on_lesson_plan_item_id"
end

Expand All @@ -929,7 +941,9 @@
t.datetime "updated_at", precision: nil, null: false
t.string "title"
t.integer "weight"
t.datetime "deleted_at"
t.index ["course_id"], name: "index_course_reference_timelines_on_course_id"
t.index ["deleted_at"], name: "index_course_reference_timelines_on_deleted_at"
end

create_table "course_reference_times", force: :cascade do |t|
Expand Down Expand Up @@ -1043,17 +1057,21 @@
t.datetime "obtained_at", precision: nil, null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.datetime "deleted_at"
t.index ["achievement_id"], name: "fk__course_user_achievements_achievement_id"
t.index ["course_user_id", "achievement_id"], name: "index_user_achievements_on_course_user_id_and_achievement_id", unique: true
t.index ["course_user_id"], name: "fk__course_user_achievements_course_user_id"
t.index ["deleted_at"], name: "index_course_user_achievements_on_deleted_at"
end

create_table "course_user_email_unsubscriptions", force: :cascade do |t|
t.bigint "course_user_id", null: false
t.bigint "course_settings_email_id", null: false
t.datetime "deleted_at"
t.index ["course_settings_email_id"], name: "index_email_unsubscriptions_on_course_settings_email_id"
t.index ["course_user_id", "course_settings_email_id"], name: "index_course_user_email_unsubscriptions_composite", unique: true
t.index ["course_user_id"], name: "index_email_unsubscriptions_on_course_user_id"
t.index ["deleted_at"], name: "index_course_user_email_unsubscriptions_on_deleted_at"
end

create_table "course_user_invitations", id: :serial, force: :cascade do |t|
Expand Down Expand Up @@ -1093,9 +1111,11 @@
t.integer "updater_id", null: false
t.bigint "reference_timeline_id"
t.integer "timeline_algorithm", default: 0, null: false
t.datetime "deleted_at"
t.index ["course_id", "user_id"], name: "index_course_users_on_course_id_and_user_id", unique: true
t.index ["course_id"], name: "fk__course_users_course_id"
t.index ["creator_id"], name: "fk__course_users_creator_id"
t.index ["deleted_at"], name: "index_course_users_on_deleted_at"
t.index ["reference_timeline_id"], name: "index_course_users_on_reference_timeline_id"
t.index ["updater_id"], name: "fk__course_users_updater_id"
t.index ["user_id"], name: "fk__course_users_user_id"
Expand Down Expand Up @@ -1392,7 +1412,7 @@
t.string "type", limit: 255, null: false
t.string "name", limit: 255, null: false
t.integer "parent_id"
t.serial "weight"
t.serial "weight", null: false
t.boolean "enabled", default: true, null: false
t.index "lower((name)::text)", name: "index_polyglot_languages_on_name", unique: true
t.index ["parent_id"], name: "fk__polyglot_languages_parent_id"
Expand Down

0 comments on commit 16d69dc

Please sign in to comment.