From 8a4172ddad91d18ce1d919868ba1e99ab0fb0536 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 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/models/hyrax/collection_type.rb b/app/models/hyrax/collection_type.rb index 78119b7d71..6bb6a144e5 100644 --- a/app/models/hyrax/collection_type.rb +++ b/app/models/hyrax/collection_type.rb @@ -153,8 +153,15 @@ 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? + return false unless id + Hyrax::SolrQueryService.new.with_field_pairs(field_pairs: { Hyrax.config.collection_type_index_field.to_sym => to_global_id.to_s }).with_model(model: Hyrax.config.collection_class).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 +179,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