diff --git a/app/models/collection.rb b/app/models/collection.rb index 7dfb86b3cd..e6a71805b0 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -2,4 +2,16 @@ # Provide plain collection model if not defined by the application. # Needed until Hyrax internals do not assume its existence. -class ::Collection < Hyrax.config.collection_class; end unless '::Collection'.safe_constantize +class ::Collection < Hyrax.config.collection_class; end unless ActiveSupport::Dependencies.then do |deps| + # In autoloading environments, when referencing +::Collection+ from the + # initializer, make sure that +safe_constantize+ wouldn’t just try loading + # this file again (which would produce a runtime error). Do this by manually + # searching for the file which should define +::Collection+ and checking if it + # is the one being currently loaded. + break true if Object.const_defined?(:Collection) + file_path = deps.search_for_file("collection") + expanded = File.expand_path(file_path) + expanded.delete_suffix!(".rb") + break false if deps.loading.include?(expanded) + '::Collection'.safe_constantize +end