Sha256: 3dd549e82d98b79d21e708a6f6032f89b7d575b9e491cbe0bd85a4e4be48829e

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
module Hyrax
  module CustomQueries
    # @example
    #   collection_type = Hyrax::CollectionType.find(1)
    #
    #   Hyrax.custom_queries.find_collections_by_type(global_id: Hyrax::GlobalID(collection_type))
    #
    # @see https://github.com/samvera/valkyrie/wiki/Queries#custom-queries
    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

      ##
      # @note this is an unoptimized default implementation of this custom
      #   query. it's Hyrax's policy to provide such implementations of custom
      #   queries in use for cross-compatibility of Valkyrie query services.
      #   it's advisable to provide an optimized query for the specific adapter.
      #
      # @param global_id [GlobalID] global id for a Hyrax::CollectionType
      #
      # @return [Enumerable<PcdmCollection>]
      def find_collections_by_type(global_id:)
        query_service
          .find_all_of_model(model: Hyrax.config.collection_model.safe_constantize)
          .select { |collection| collection.collection_type_gid == global_id }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hyrax-5.0.1 app/services/hyrax/custom_queries/find_collections_by_type.rb
hyrax-5.0.0 app/services/hyrax/custom_queries/find_collections_by_type.rb
hyrax-5.0.0.rc3 app/services/hyrax/custom_queries/find_collections_by_type.rb