mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Change Bookmarkable registration to DiscoursePluginRegistry (#20556)
Similar spirit to e195e6f614
,
this moves the Bookmarkable registration to DiscoursePluginRegistry
so plugins which are not enabled do not register additional
bookmarkable classes.
This commit is contained in:
parent
1c881c1037
commit
360d0dde65
@ -1,36 +1,21 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Bookmark < ActiveRecord::Base
|
class Bookmark < ActiveRecord::Base
|
||||||
cattr_accessor :registered_bookmarkables
|
DEFAULT_BOOKMARKABLES = [
|
||||||
self.registered_bookmarkables = []
|
RegisteredBookmarkable.new(PostBookmarkable),
|
||||||
|
RegisteredBookmarkable.new(TopicBookmarkable),
|
||||||
|
]
|
||||||
|
|
||||||
|
def self.registered_bookmarkables
|
||||||
|
Set.new(DEFAULT_BOOKMARKABLES | DiscoursePluginRegistry.bookmarkables)
|
||||||
|
end
|
||||||
|
|
||||||
def self.registered_bookmarkable_from_type(type)
|
def self.registered_bookmarkable_from_type(type)
|
||||||
Bookmark.registered_bookmarkables.find { |bm| bm.model.name == type }
|
Bookmark.registered_bookmarkables.find { |bm| bm.model.name == type }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.register_bookmarkable(bookmarkable_klass)
|
|
||||||
return if Bookmark.registered_bookmarkable_from_type(bookmarkable_klass.model.name).present?
|
|
||||||
Bookmark.registered_bookmarkables << RegisteredBookmarkable.new(bookmarkable_klass)
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# This is called when the app loads, similar to AdminDashboardData.reset_problem_checks,
|
|
||||||
# so the default Post and Topic bookmarkables are registered on
|
|
||||||
# boot.
|
|
||||||
#
|
|
||||||
# This method also can be used in testing to reset bookmarkables between
|
|
||||||
# tests. It will also fire multiple times in development mode because
|
|
||||||
# classes are not cached.
|
|
||||||
def self.reset_bookmarkables
|
|
||||||
self.registered_bookmarkables = []
|
|
||||||
|
|
||||||
Bookmark.register_bookmarkable(PostBookmarkable)
|
|
||||||
Bookmark.register_bookmarkable(TopicBookmarkable)
|
|
||||||
end
|
|
||||||
reset_bookmarkables
|
|
||||||
|
|
||||||
def self.valid_bookmarkable_types
|
def self.valid_bookmarkable_types
|
||||||
Bookmark.registered_bookmarkables.map(&:model).map(&:to_s)
|
Bookmark.registered_bookmarkables.map { |bm| bm.model.to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
##
|
##
|
||||||
# Anything that we want to be able to bookmark must be registered as a
|
# Anything that we want to be able to bookmark must be registered as a
|
||||||
# bookmarkable type using Bookmark.register_bookmarkable(bookmarkable_klass),
|
# bookmarkable type using Plugin::Instance#register_bookmarkable(bookmarkable_klass),
|
||||||
# where the bookmarkable_klass is a class that implements this BaseBookmarkable
|
# where the bookmarkable_klass is a class that implements this BaseBookmarkable
|
||||||
# interface. Some examples are TopicBookmarkable and PostBookmarkable.
|
# interface. Some examples are TopicBookmarkable and PostBookmarkable.
|
||||||
#
|
#
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Should only be created via the Bookmark.register_bookmarkable
|
# Should only be created via the Plugin::Instance#register_bookmarkable
|
||||||
# method; this is used to let the BookmarkQuery class query and
|
# method; this is used to let the BookmarkQuery class query and
|
||||||
# search additional bookmarks for the user bookmark list, and
|
# search additional bookmarks for the user bookmark list, and
|
||||||
# also to enumerate on the registered RegisteredBookmarkable types.
|
# also to enumerate on the registered RegisteredBookmarkable types.
|
||||||
@ -11,7 +11,7 @@
|
|||||||
# when trying to save the Bookmark record. All things that are bookmarkable
|
# when trying to save the Bookmark record. All things that are bookmarkable
|
||||||
# must be registered in this way.
|
# must be registered in this way.
|
||||||
#
|
#
|
||||||
# See Bookmark#reset_bookmarkables for some examples on how registering
|
# See Plugin::Instance#register_bookmarkable for some examples on how registering
|
||||||
# bookmarkables works.
|
# bookmarkables works.
|
||||||
#
|
#
|
||||||
# See BaseBookmarkable for documentation on what return types should be
|
# See BaseBookmarkable for documentation on what return types should be
|
||||||
|
@ -109,6 +109,7 @@ class DiscoursePluginRegistry
|
|||||||
define_filtered_register :search_groups_set_query_callbacks
|
define_filtered_register :search_groups_set_query_callbacks
|
||||||
|
|
||||||
define_filtered_register :about_stat_groups
|
define_filtered_register :about_stat_groups
|
||||||
|
define_filtered_register :bookmarkables
|
||||||
|
|
||||||
def self.register_auth_provider(auth_provider)
|
def self.register_auth_provider(auth_provider)
|
||||||
self.auth_providers << auth_provider
|
self.auth_providers << auth_provider
|
||||||
|
@ -1221,6 +1221,16 @@ class Plugin::Instance
|
|||||||
DiscoursePluginRegistry.register_user_destroyer_on_content_deletion_callback(callback, self)
|
DiscoursePluginRegistry.register_user_destroyer_on_content_deletion_callback(callback, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# Register a class that implements [BaseBookmarkable], which represents another
|
||||||
|
# [ActiveRecord::Model] that may be bookmarked via the [Bookmark] model's
|
||||||
|
# polymorphic association. The class handles create and destroy hooks, querying,
|
||||||
|
# and reminders among other things.
|
||||||
|
def register_bookmarkable(klass)
|
||||||
|
return if Bookmark.registered_bookmarkable_from_type(klass.model.name).present?
|
||||||
|
DiscoursePluginRegistry.register_bookmarkable(RegisteredBookmarkable.new(klass), self)
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def self.js_path
|
def self.js_path
|
||||||
|
@ -280,7 +280,6 @@ after_initialize do
|
|||||||
Category.prepend Chat::CategoryExtension
|
Category.prepend Chat::CategoryExtension
|
||||||
User.prepend Chat::UserExtension
|
User.prepend Chat::UserExtension
|
||||||
Jobs::UserEmail.prepend Chat::UserEmailExtension
|
Jobs::UserEmail.prepend Chat::UserEmailExtension
|
||||||
Bookmark.register_bookmarkable(ChatMessageBookmarkable)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if Oneboxer.respond_to?(:register_local_handler)
|
if Oneboxer.respond_to?(:register_local_handler)
|
||||||
@ -777,6 +776,8 @@ after_initialize do
|
|||||||
register_user_destroyer_on_content_deletion_callback(
|
register_user_destroyer_on_content_deletion_callback(
|
||||||
Proc.new { |user| Jobs.enqueue(:delete_user_messages, user_id: user.id) },
|
Proc.new { |user| Jobs.enqueue(:delete_user_messages, user_id: user.id) },
|
||||||
)
|
)
|
||||||
|
|
||||||
|
register_bookmarkable(ChatMessageBookmarkable)
|
||||||
end
|
end
|
||||||
|
|
||||||
if Rails.env == "test"
|
if Rails.env == "test"
|
||||||
|
@ -11,10 +11,12 @@ describe ChatMessageBookmarkable do
|
|||||||
fab!(:channel) { Fabricate(:category_channel) }
|
fab!(:channel) { Fabricate(:category_channel) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
Bookmark.register_bookmarkable(ChatMessageBookmarkable)
|
register_test_bookmarkable(ChatMessageBookmarkable)
|
||||||
UserChatChannelMembership.create(chat_channel: channel, user: user, following: true)
|
UserChatChannelMembership.create(chat_channel: channel, user: user, following: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
let!(:message1) { Fabricate(:chat_message, chat_channel: channel) }
|
let!(:message1) { Fabricate(:chat_message, chat_channel: channel) }
|
||||||
let!(:message2) { Fabricate(:chat_message, chat_channel: channel) }
|
let!(:message2) { Fabricate(:chat_message, chat_channel: channel) }
|
||||||
let!(:bookmark1) do
|
let!(:bookmark1) do
|
||||||
|
@ -499,8 +499,6 @@ describe ChatMessage do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "bookmarks" do
|
describe "bookmarks" do
|
||||||
before { Bookmark.register_bookmarkable(ChatMessageBookmarkable) }
|
|
||||||
|
|
||||||
it "destroys bookmarks" do
|
it "destroys bookmarks" do
|
||||||
message_1 = Fabricate(:chat_message)
|
message_1 = Fabricate(:chat_message)
|
||||||
bookmark_1 = Fabricate(:bookmark, bookmarkable: message_1)
|
bookmark_1 = Fabricate(:bookmark, bookmarkable: message_1)
|
||||||
|
@ -18,9 +18,6 @@ module ChatSystemHelpers
|
|||||||
end
|
end
|
||||||
|
|
||||||
Group.refresh_automatic_groups!
|
Group.refresh_automatic_groups!
|
||||||
|
|
||||||
# this is reset after each test
|
|
||||||
Bookmark.register_bookmarkable(ChatMessageBookmarkable)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def chat_thread_chain_bootstrap(channel:, users:, messages_count: 4)
|
def chat_thread_chain_bootstrap(channel:, users:, messages_count: 4)
|
||||||
|
@ -12,7 +12,6 @@ RSpec.describe BookmarkQuery do
|
|||||||
|
|
||||||
describe "#list_all" do
|
describe "#list_all" do
|
||||||
before do
|
before do
|
||||||
Bookmark.reset_bookmarkables
|
|
||||||
register_test_bookmarkable
|
register_test_bookmarkable
|
||||||
|
|
||||||
Fabricate(:topic_user, user: user, topic: post_bookmark.bookmarkable.topic)
|
Fabricate(:topic_user, user: user, topic: post_bookmark.bookmarkable.topic)
|
||||||
@ -20,14 +19,14 @@ RSpec.describe BookmarkQuery do
|
|||||||
user_bookmark
|
user_bookmark
|
||||||
end
|
end
|
||||||
|
|
||||||
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
||||||
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
||||||
let(:user_bookmark) do
|
let(:user_bookmark) do
|
||||||
Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user, username: "bookmarkqueen"))
|
Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user, username: "bookmarkqueen"))
|
||||||
end
|
end
|
||||||
|
|
||||||
after { Bookmark.reset_bookmarkables }
|
|
||||||
|
|
||||||
it "returns all the bookmarks for a user" do
|
it "returns all the bookmarks for a user" do
|
||||||
expect(bookmark_query.list_all.count).to eq(3)
|
expect(bookmark_query.list_all.count).to eq(3)
|
||||||
end
|
end
|
||||||
@ -77,9 +76,7 @@ RSpec.describe BookmarkQuery do
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
before { Bookmark.reset_bookmarkables }
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
after { Bookmark.reset_bookmarkables }
|
|
||||||
|
|
||||||
let(:bookmark3) do
|
let(:bookmark3) do
|
||||||
Fabricate(:bookmark, user: user, name: "Check up later", bookmarkable: Fabricate(:post))
|
Fabricate(:bookmark, user: user, name: "Check up later", bookmarkable: Fabricate(:post))
|
||||||
|
@ -4,6 +4,8 @@ RSpec.describe Bookmark do
|
|||||||
fab!(:post) { Fabricate(:post) }
|
fab!(:post) { Fabricate(:post) }
|
||||||
|
|
||||||
describe "Validations" do
|
describe "Validations" do
|
||||||
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
it "does not allow a user to create a bookmark with only one polymorphic column" do
|
it "does not allow a user to create a bookmark with only one polymorphic column" do
|
||||||
user = Fabricate(:user)
|
user = Fabricate(:user)
|
||||||
bm = Bookmark.create(bookmarkable_id: post.id, user: user)
|
bm = Bookmark.create(bookmarkable_id: post.id, user: user)
|
||||||
|
@ -13,6 +13,8 @@ RSpec.describe UserBookmarkList do
|
|||||||
user_bookmark
|
user_bookmark
|
||||||
end
|
end
|
||||||
|
|
||||||
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
||||||
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
||||||
let(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user)) }
|
let(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user)) }
|
||||||
|
@ -145,9 +145,6 @@ module TestSetup
|
|||||||
# Don't queue badge grant in test mode
|
# Don't queue badge grant in test mode
|
||||||
BadgeGranter.disable_queue
|
BadgeGranter.disable_queue
|
||||||
|
|
||||||
# Make sure the default Post and Topic bookmarkables are registered
|
|
||||||
Bookmark.reset_bookmarkables
|
|
||||||
|
|
||||||
OmniAuth.config.test_mode = false
|
OmniAuth.config.test_mode = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -5947,7 +5947,7 @@ RSpec.describe UsersController do
|
|||||||
bookmark3 && bookmark4
|
bookmark3 && bookmark4
|
||||||
end
|
end
|
||||||
|
|
||||||
after { Bookmark.registered_bookmarkables = [] }
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
let(:bookmark1) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:post)) }
|
let(:bookmark1) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:post)) }
|
||||||
let(:bookmark2) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:topic)) }
|
let(:bookmark2) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:topic)) }
|
||||||
|
@ -11,6 +11,8 @@ RSpec.describe UserBookmarkListSerializer do
|
|||||||
user_bookmark
|
user_bookmark
|
||||||
end
|
end
|
||||||
|
|
||||||
|
after { DiscoursePluginRegistry.reset! }
|
||||||
|
|
||||||
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
|
||||||
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
|
||||||
let(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user)) }
|
let(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user)) }
|
||||||
|
@ -72,6 +72,9 @@ class UserTestBookmarkable < BaseBookmarkable
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_test_bookmarkable
|
def register_test_bookmarkable(klass = UserTestBookmarkable)
|
||||||
Bookmark.register_bookmarkable(UserTestBookmarkable)
|
DiscoursePluginRegistry.register_bookmarkable(
|
||||||
|
RegisteredBookmarkable.new(klass),
|
||||||
|
stub(enabled?: true),
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user