From 2f6987a241dc6c6dae0cce18cf99f7216df59acb Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 20 Mar 2023 19:19:28 +1000 Subject: [PATCH] DEV: Minor refactor for bookmark polymorphic changes (#20733) Followup to 184ce647ea7e9159b43b3552f334e6508e6aef51, this just implements Bianca's suggestion on the original PR and catches the NameError, which was not necessary before as we were not actually resolving any class from bookmarkable_type. --- app/models/bookmark.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index be664d346e9..6621265212f 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -11,7 +11,14 @@ class Bookmark < ActiveRecord::Base end def self.registered_bookmarkable_from_type(type) - Bookmark.registered_bookmarkables.find { |bm| bm.model.name == type } + begin + resolved_type = Bookmark.polymorphic_class_for(type).name + Bookmark.registered_bookmarkables.find { |bm| bm.model.name == resolved_type } + + # If the class cannot be found from the provided type using polymorphic_class_for, + # then the type is not valid and thus there will not be any registered bookmarkable. + rescue NameError + end end def self.valid_bookmarkable_types @@ -46,8 +53,7 @@ class Bookmark < ActiveRecord::Base validates :name, length: { maximum: 100 } def registered_bookmarkable - type = Bookmark.polymorphic_class_for(self.bookmarkable_type).name - Bookmark.registered_bookmarkable_from_type(type) + Bookmark.registered_bookmarkable_from_type(self.bookmarkable_type) end def polymorphic_columns_present