DEV: Add polymorphic bookmarkable columns (#15454)

We are planning on attaching bookmarks to more and
more other models, so it makes sense to make a polymorphic
relationship to handle this. This commit adds the new
columns and backfills them in the bookmark table, and
makes sure that any new bookmark changes fill in the columns
via DB triggers.

This way we can gradually change the frontend and backend
to use these new columns, and eventually delete the
old post_id and for_topic columns in `bookmarks`.
This commit is contained in:
Martin Brennan
2022-01-06 08:56:05 +10:00
committed by GitHub
parent e6ab8f5b71
commit e21c640a3c
6 changed files with 80 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ class Bookmark < ActiveRecord::Base
belongs_to :user
belongs_to :post
has_one :topic, through: :post
belongs_to :bookmarkable, polymorphic: true
delegate :topic_id, to: :post
@@ -171,9 +172,12 @@ end
# auto_delete_preference :integer default(0), not null
# pinned :boolean default(FALSE)
# for_topic :boolean default(FALSE), not null
# bookmarkable_id :integer
# bookmarkable_type :string
#
# Indexes
#
# idx_bookmarks_user_polymorphic_unique (user_id,bookmarkable_type,bookmarkable_id) UNIQUE
# index_bookmarks_on_post_id (post_id)
# index_bookmarks_on_reminder_at (reminder_at)
# index_bookmarks_on_reminder_set_at (reminder_set_at)

View File

@@ -46,6 +46,9 @@ class Post < ActiveRecord::Base
has_many :uploads, through: :post_uploads
has_one :post_stat
# When we are ready we can add as: :bookmarkable here to use the
# polymorphic association.
has_many :bookmarks
has_one :incoming_email

View File

@@ -203,7 +203,15 @@ class Topic < ActiveRecord::Base
belongs_to :category
has_many :category_users, through: :category
has_many :posts
# When we are ready we can add as: :bookmarkable here to use the
# polymorphic association.
#
# At that time we may also want to make another association for example
# :topic_bookmarks that get all of the bookmarks for that topic's bookmarkable id
# and type, because this one gets all of the post bookmarks.
has_many :bookmarks, through: :posts
has_many :ordered_posts, -> { order(post_number: :asc) }, class_name: "Post"
has_many :topic_allowed_users
has_many :topic_allowed_groups