Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize callbacks by using solr instead of find_collections_by_type #6925

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading