Skip to content

Commit

Permalink
Collection type provides performant method to check if any collection…
Browse files Browse the repository at this point in the history
…s exist (#6929)

* Collection type provides performant method to check if any collections exist

* Ensure there are no collections of this type

---------

Co-authored-by: Daniel Pierce <dlpierce@iu.edu>
  • Loading branch information
cjcolvar and dlpierce authored Oct 24, 2024
1 parent 4f0ba25 commit e11d5d0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
8 changes: 1 addition & 7 deletions app/forms/hyrax/forms/admin/collection_type_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CollectionTypeForm
delegate :title, :description, :brandable, :discoverable, :nestable, :sharable, :share_applies_to_new_works,
:require_membership, :allow_multiple_membership, :assigns_workflow,
:assigns_visibility, :id, :collection_type_participants, :persisted?,
:admin_set?, :user_collection?, :badge_color, to: :collection_type
:admin_set?, :user_collection?, :badge_color, :collections?, to: :collection_type

##
# @return [Boolean]
Expand All @@ -23,12 +23,6 @@ def all_settings_disabled?
def share_options_disabled?
all_settings_disabled? || !sharable
end

##
# @return [Boolean]
def collections?
collection_type.collections.any?
end
end
end
end
Expand Down
19 changes: 10 additions & 9 deletions app/models/hyrax/collection_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def collections(use_valkyrie: Hyrax.config.use_valkyrie?, model: Hyrax.config.co
ActiveFedora::Base.where(Hyrax.config.collection_type_index_field.to_sym => to_global_id.to_s)
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?
# @return [Boolean] True if there are any collections of this collection type in the repository
def 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

# @return [Boolean] True if this is the Admin Set type
def admin_set?
machine_id == ADMIN_SET_MACHINE_ID
Expand Down Expand Up @@ -153,15 +161,8 @@ 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 any_collections?
return true unless collections?
errors[:base] << I18n.t('hyrax.admin.collection_types.errors.not_empty')
throw :abort
end
Expand All @@ -179,7 +180,7 @@ def ensure_no_settings_changes_for_user_collection_type
end

def ensure_no_settings_changes_if_collections_exist
return true unless any_collections?
return true unless 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
2 changes: 1 addition & 1 deletion app/views/hyrax/admin/collection_types/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<button class="btn btn-danger btn-sm delete-collection-type"
data-collection-type="<%= collection_type.to_json %>"
data-collection-type-index="<%= hyrax.dashboard_collections_path({ 'f[collection_type_gid_ssim][]' => collection_type.to_global_id.to_s }) %>"
data-has-collections="<%= collection_type.collections.any? %>">
data-has-collections="<%= collection_type.collections? %>">
<%= t('helpers.action.delete') %>
</button>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions spec/features/collection_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
end

describe 'edit collection type' do
context 'when there are no collections of this type' do
context 'when there are no collections of this type', :clean_repo do
before do
exhibit_collection_type
sign_in admin_user
Expand Down Expand Up @@ -270,7 +270,7 @@
end

describe 'delete collection type' do
context 'when there are no collections of this type' do
context 'when there are no collections of this type', :clean_repo do
let!(:empty_collection_type) { create(:collection_type, title: 'Empty Type', creator_user: admin_user) }
let!(:delete_modal_text) { 'Deleting this collection type will permanently remove the type and its settings from the repository. Are you sure you want to delete this collection type?' }
let!(:deleted_flash_text) { "The collection type #{empty_collection_type.title} has been deleted." }
Expand Down

0 comments on commit e11d5d0

Please sign in to comment.