From 662a86c7f7badb371bc1197eb8f9f8ddf8c156ef Mon Sep 17 00:00:00 2001 From: Chris Colvard Date: Tue, 15 Oct 2024 10:55:03 -0400 Subject: [PATCH] Optimize callbacks by using solr instead of find_collections_by_type --- app/models/hyrax/collection_type.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/hyrax/collection_type.rb b/app/models/hyrax/collection_type.rb index 78119b7d71..8bb75c19de 100644 --- a/app/models/hyrax/collection_type.rb +++ b/app/models/hyrax/collection_type.rb @@ -153,8 +153,14 @@ def assign_machine_id self.machine_id ||= title.parameterize.underscore.to_sym if title.present? end + # Query solr to see if any collections of this type exist + # This should be much more performant for certain adapters than calling collections.any? + def any_collections? + Hyrax::SolrQueryService.new.with_field_pairs(field_pairs: { Hyrax.config.collection_type_index_field.to_sym => to_global_id.to_s }).with_model(model:).count > 0 + end + def ensure_no_collections - return true unless collections.any? + return true unless any_collections? errors[:base] << I18n.t('hyrax.admin.collection_types.errors.not_empty') throw :abort end @@ -172,7 +178,7 @@ def ensure_no_settings_changes_for_user_collection_type end def ensure_no_settings_changes_if_collections_exist - return true unless collections.any? + return true unless any_collections? return true unless collection_type_settings_changed? errors[:base] << I18n.t('hyrax.admin.collection_types.errors.no_settings_change_if_not_empty') throw :abort