A platform for community discussion. Free, open, simple.
Unscope post + topic relation for bookmark
Martin Brennan03c012bd9727
diff --git a/app/models/bookmark.rb b/app/models/bookmark.rbindex d25a30b8650..d28e40d2635 100644--- a/app/models/bookmark.rb+++ b/app/models/bookmark.rb@@ -13,6 +13,16 @@ class Bookmark < ActiveRecord::Basevalidate :unique_per_post_for_uservalidate :ensure_sane_reminder_at_time+ # we don't care whether the post or topic is deleted,+ # they hold important information about the bookmark+ def post+ Post.unscoped { super }+ end++ def topic+ Topic.unscoped { super }+ end+def unique_per_post_for_userexisting_bookmark = Bookmark.find_by(user_id: user_id, post_id: post_id)return if existing_bookmark.blank? || existing_bookmark.id == iddiff --git a/app/serializers/user_bookmark_serializer.rb b/app/serializers/user_bookmark_serializer.rbindex adef053d308..39358c2262a 100644--- a/app/serializers/user_bookmark_serializer.rb+++ b/app/serializers/user_bookmark_serializer.rb@@ -26,11 +26,11 @@ class UserBookmarkSerializer < ApplicationSerializer:usernamedef closed- object.topic_closed+ object.topic.closedenddef archived- object.topic_archived+ object.topic.archivedenddef linked_post_numberdiff --git a/spec/serializers/user_bookmark_serializer_spec.rb b/spec/serializers/user_bookmark_serializer_spec.rbnew file mode 100644index 00000000000..0bca3a0228c--- /dev/null+++ b/spec/serializers/user_bookmark_serializer_spec.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++require 'rails_helper'++RSpec.describe UserBookmarkSerializer do+ let(:bookmark) do+ Fabricate(:bookmark)+ Bookmark.all.includes(post: :user).includes(:topic).last+ end++ subject { described_class.new(bookmark) }++ context "when the topic is deleted" do+ before do+ bookmark.topic.trash!+ bookmark.reload+ end+ it "still returns the topic title because the relationship is unscoped" do+ expect(subject.title).not_to eq(nil)+ end+ end++ context "when the post is deleted" do+ before do+ bookmark.post.trash!+ bookmark.reload+ end+ it "still returns the post number because the relationship is unscoped" do+ expect(subject.linked_post_number).not_to eq(nil)+ end+ it "still returns the post username" do+ expect(subject.username).not_to eq(nil)+ end+ end+end