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

add optimized #fid_collections_by_type query for Wings #4432

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions lib/wings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def self.inspect
require 'wings/orm_converter'
require 'wings/attribute_transformer'
require 'wings/services/custom_queries/find_access_control'
require 'wings/services/custom_queries/find_collections_by_type'
require 'wings/services/custom_queries/find_file_metadata'
require 'wings/services/custom_queries/find_many_by_alternate_ids'
require 'wings/valkyrizable'
Expand Down
27 changes: 27 additions & 0 deletions lib/wings/services/custom_queries/find_collections_by_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true
module Wings
module CustomQueries
##
# @see Hyrax::CustomQueries::FindCollectionsByType
class FindCollectionsByType
def self.queries
[:find_collections_by_type]
end

def initialize(query_service:)
@query_service = query_service
end

attr_reader :query_service
delegate :resource_factory, to: :query_service

##
# @param global_id [GlobalID] global id for a Hyrax::CollectionType
#
# @return [Enumerable<PcdmCollection>]
def find_collections_by_type(global_id:)
::Collection.where(collection_type_gid_ssim: global_id.to_s).map(&:valkyrie_resource)
end
end
end
end
1 change: 1 addition & 0 deletions lib/wings/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def self.model_mapper
Hyrax::CustomQueries::Navigators::ChildWorksNavigator,
Hyrax::CustomQueries::Navigators::FindFiles,
Wings::CustomQueries::FindAccessControl, # override Hyrax::CustomQueries::FindAccessControl
Wings::CustomQueries::FindCollectionsByType,
Wings::CustomQueries::FindFileMetadata, # override Hyrax::CustomQueries::FindFileMetadata
Wings::CustomQueries::FindManyByAlternateIds] # override Hyrax::CustomQueries::FindManyByAlternateIds
custom_queries.each do |query_handler|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true
require 'wings_helper'
require 'wings/services/custom_queries/find_collections_by_type'

RSpec.describe Wings::CustomQueries::FindCollectionsByType, :clean_repo do
subject(:query_handler) { described_class.new(query_service: query_service) }
let(:collection_type) { FactoryBot.create(:collection_type) }
let(:type_gid) { collection_type.to_global_id }
let(:query_service) { Hyrax.query_service }

describe '#find_collections_by_type' do
context 'when there are no collections' do
it 'is empty' do
expect(query_handler.find_collections_by_type(global_id: type_gid))
.to be_empty
end
end

context 'when collections exist' do
let(:collection_with_default_type) { FactoryBot.create(:collection) }
let(:non_collection_model) { FactoryBot.create(:work) }

let(:collections_with_target_type) do
FactoryBot.create_list(:collection_lw, 3, collection_type_gid: type_gid)
end

before do # force create all the models
non_collection_model
collection_with_default_type
collections_with_target_type
end
cjcolvar marked this conversation as resolved.
Show resolved Hide resolved

it 'returns only the collections of requested type' do
expect(query_handler.find_collections_by_type(global_id: type_gid).map(&:id))
.to contain_exactly(*collections_with_target_type.map(&:id))
end

it 'returns Hyrax::PcdmCollection instances' do
expect(query_handler.find_collections_by_type(global_id: type_gid))
.to contain_exactly be_a(Hyrax::PcdmCollection),
be_a(Hyrax::PcdmCollection),
be_a(Hyrax::PcdmCollection)
end
end
end
end