Skip to content

A platform for community discussion. Free, open, simple.

FIX: correctly infer polymorphic class from bookmarkable type (#20719)

Joffrey JAFFEUX184ce647ea7e
diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb
index 845ba5c76a0..be664d346e9 100644
--- a/app/models/bookmark.rb
+++ b/app/models/bookmark.rb
@@ -46,7 +46,8 @@ class Bookmark < ActiveRecord::Base
validates :name, length: { maximum: 100 }
def registered_bookmarkable
- Bookmark.registered_bookmarkable_from_type(self.bookmarkable_type)
+ type = Bookmark.polymorphic_class_for(self.bookmarkable_type).name
+ Bookmark.registered_bookmarkable_from_type(type)
end
def polymorphic_columns_present
diff --git a/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb b/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb
new file mode 100644
index 00000000000..b82e38171ce
--- /dev/null
+++ b/plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+RSpec.describe BookmarksController do
+ let(:current_user) { Fabricate(:user) }
+ let(:bookmark_message) { Fabricate(:chat_message) }
+ let(:bookmark_user) { current_user }
+
+ before { sign_in(current_user) }
+
+ context "when bookmarking a chat message" do
+ describe "#create" do
+ it "creates the bookmark" do
+ post "/bookmarks.json",
+ params: {
+ bookmarkable_id: bookmark_message.id,
+ bookmarkable_type: "Chat::Message",
+ reminder_at: (Time.zone.now + 1.day).iso8601,
+ }
+
+ expect(response.status).to eq(200)
+ expect(Bookmark.find_by(bookmarkable: bookmark_message).user_id).to eq(current_user.id)
+ end
+ end
+
+ describe "#destroy" do
+ let!(:bookmark) { Fabricate(:bookmark, bookmarkable: bookmark_message, user: bookmark_user) }
+
+ it "destroys the bookmark" do
+ delete "/bookmarks/#{bookmark.id}.json"
+
+ expect(response.status).to eq(200)
+ expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
+ end
+ end
+ end
+end