Skip to content

Commit

Permalink
Optimize callbacks by using solr instead of find_collections_by_type
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcolvar committed Oct 15, 2024
1 parent 0ffde70 commit e4dc646
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:).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 e4dc646

Please sign in to comment.