From efefbe9e3dd5c272cef82ae25ae92487e68dcaf7 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 17 Mar 2023 15:56:55 +0100 Subject: [PATCH] FIX: correctly infer polymorphic class from bookmarkable type --- app/models/bookmark.rb | 3 +- .../core_ext/bookmarks_controller_spec.rb | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 plugins/chat/spec/requests/core_ext/bookmarks_controller_spec.rb diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index 845ba5c76a..be664d346e 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 0000000000..b82e38171c --- /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