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

@@ -12,12 +12,14 @@ RSpec.describe BookmarkManager do
subject { described_class.new(user) }
describe ".create" do
it "creates the bookmark for the user" do
it "creates the bookmark for the user with the correct polymorphic type" do
subject.create(post_id: post.id, name: name)
bookmark = Bookmark.find_by(user: user)
expect(bookmark.post_id).to eq(post.id)
expect(bookmark.topic_id).to eq(post.topic_id)
expect(bookmark.bookmarkable_id).to eq(post.id)
expect(bookmark.bookmarkable_type).to eq("Post")
end
it "allows creating a bookmark for the topic and for the first post" do
@@ -27,6 +29,8 @@ RSpec.describe BookmarkManager do
expect(bookmark.post_id).to eq(post.id)
expect(bookmark.topic_id).to eq(post.topic_id)
expect(bookmark.for_topic).to eq(true)
expect(bookmark.bookmarkable_id).to eq(post.topic_id)
expect(bookmark.bookmarkable_type).to eq("Topic")
subject.create(post_id: post.id, name: name)
bookmark = Bookmark.find_by(user: user, post_id: post.id, for_topic: false)
@@ -34,6 +38,8 @@ RSpec.describe BookmarkManager do
expect(bookmark.post_id).to eq(post.id)
expect(bookmark.topic_id).to eq(post.topic_id)
expect(bookmark.for_topic).to eq(false)
expect(bookmark.bookmarkable_id).to eq(post.id)
expect(bookmark.bookmarkable_type).to eq("Post")
end
it "errors when creating a for_topic bookmark for a post that is not the first one" do