mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 05:51:03 +08:00
Unscope post + topic relation for bookmark
* we want to be able to get these records for the bookmark even if they are trashed, for serialization for the bookmark list
This commit is contained in:
parent
e1eb5fb9b3
commit
03c012bd97
|
@ -13,6 +13,16 @@ class Bookmark < ActiveRecord::Base
|
||||||
validate :unique_per_post_for_user
|
validate :unique_per_post_for_user
|
||||||
validate :ensure_sane_reminder_at_time
|
validate :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_user
|
def unique_per_post_for_user
|
||||||
existing_bookmark = Bookmark.find_by(user_id: user_id, post_id: post_id)
|
existing_bookmark = Bookmark.find_by(user_id: user_id, post_id: post_id)
|
||||||
return if existing_bookmark.blank? || existing_bookmark.id == id
|
return if existing_bookmark.blank? || existing_bookmark.id == id
|
||||||
|
|
|
@ -26,11 +26,11 @@ class UserBookmarkSerializer < ApplicationSerializer
|
||||||
:username
|
:username
|
||||||
|
|
||||||
def closed
|
def closed
|
||||||
object.topic_closed
|
object.topic.closed
|
||||||
end
|
end
|
||||||
|
|
||||||
def archived
|
def archived
|
||||||
object.topic_archived
|
object.topic.archived
|
||||||
end
|
end
|
||||||
|
|
||||||
def linked_post_number
|
def linked_post_number
|
||||||
|
|
35
spec/serializers/user_bookmark_serializer_spec.rb
Normal file
35
spec/serializers/user_bookmark_serializer_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user