discourse/plugins
Joffrey JAFFEUX 184ce647ea
FIX: correctly infer polymorphic class from bookmarkable type (#20719)
Prior to this change `registered_bookmarkable` would return `nil` as  `type` in `Bookmark.registered_bookmarkable_from_type(type)` would be `ChatMessage` and we registered a `Chat::Message` class.

This commit will now properly rely on each model `polymorphic_class_for(name)` to help us infer the proper type from a a `bookmarkable_type`.

Tests have also been added to ensure that creating/destroying chat message bookmarks is working correctly.

---

Longer explanation

Currently when you save a bookmark in the database, it's associated to another object through a polymorphic relationship, which will is represented by two columns: `bookmarkable_id` and `bookmarkable_type`. The `bookmarkable_id` contains the id of the relationship (a post ID for example) and the `bookmarkable_type` contains the type of the object as a string by default, (`"Post"` for example).

Chat plugin just started namespacing objects, as a result a model named `ChatMessage` is now named `Chat::Message`, to avoid complex and risky migrations we rely on methods provided by rails to alter the `bookmarkable_type` when we save it: we want to still save it as `"ChatMessage"` and not `"Chat::Message"`. And, to retrieve the correct model when we load the bookmark from the database: we want `"ChatMessage"` to load the `Chat::Message` model and not the `ChatMessage`model which doesn't exist anymore.

On top of this the bookmark codepath is allowing plugins to register types and will check against these types, so we alter this code path to be able to do a similar ChatMessage <-> Chat::Message dance and allow to check the type is valid. In the specific case of this commit, we were retrieving a `"ChatMessage"` bookmarkable_type from the DB and looking for it in the registered bookmarkable types which contain `Chat::Message` and not `ChatMessage`.
2023-03-17 17:20:24 +01:00
..
chat FIX: correctly infer polymorphic class from bookmarkable type (#20719) 2023-03-17 17:20:24 +01:00
discourse-details Update translations (#20671) 2023-03-14 15:04:54 +01:00
discourse-local-dates DEV: Support @debounce decorator in native class syntax (#20521) 2023-03-03 11:48:58 +00:00
discourse-narrative-bot Update translations (#20671) 2023-03-14 15:04:54 +01:00
discourse-presence Update translations (#20370) 2023-02-20 11:01:01 +01:00
lazy-yt FIX: lazy_yt_enabled doesn't affect the engine (#19997) 2023-01-25 12:40:57 -03:00
poll Update translations (#20671) 2023-03-14 15:04:54 +01:00
styleguide Update translations (#20671) 2023-03-14 15:04:54 +01:00