DEV: Minor refactor for bookmark polymorphic changes (#20733)

Followup to 184ce647ea,
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.
This commit is contained in:
Martin Brennan 2023-03-20 19:19:28 +10:00 committed by GitHub
parent 25d06faa8b
commit 2f6987a241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions

View File

@ -11,7 +11,14 @@ class Bookmark < ActiveRecord::Base
end end
def self.registered_bookmarkable_from_type(type) 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 end
def self.valid_bookmarkable_types def self.valid_bookmarkable_types
@ -46,8 +53,7 @@ class Bookmark < ActiveRecord::Base
validates :name, length: { maximum: 100 } validates :name, length: { maximum: 100 }
def registered_bookmarkable def registered_bookmarkable
type = Bookmark.polymorphic_class_for(self.bookmarkable_type).name Bookmark.registered_bookmarkable_from_type(self.bookmarkable_type)
Bookmark.registered_bookmarkable_from_type(type)
end end
def polymorphic_columns_present def polymorphic_columns_present