Skip to content

Commit

Permalink
Merge pull request #6925 from samvera/ct_opt
Browse files Browse the repository at this point in the history
Optimize callbacks by using solr instead of find_collections_by_type
  • Loading branch information
dlpierce authored Oct 16, 2024
2 parents 0ffde70 + 8a4172d commit 13b6c9e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/models/hyrax/collection_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 13b6c9e

Please sign in to comment.