Sha256: 810ed6f6f5336598da2d947e8ed336bfca003b9fdf6ee2a2e64c185a7d74b35c

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Hyrax
  module CustomQueries
    ##
    # @see https://github.com/samvera/valkyrie/wiki/Queries#custom-queries
    class FindBySourceIdentifier
      def self.queries
        [:find_by_model_and_property_value]
      end

      def initialize(query_service:)
        @query_service = query_service
      end

      attr_reader :query_service
      delegate :resource_factory, to: :query_service
      delegate :orm_class, to: :resource_factory

      ##
      # @param model [Class, #internal_resource]
      # @param property [#to_s] the name of the property we're attempting to
      #        query.
      # @param value [#to_s] the propety's value that we're trying to match.
      #
      # @return [NilClass] when no record was found
      # @return [Valkyrie::Resource] when a record was found
      #
      # @note This is not a real estate transaction nor a Zillow lookup.
      def find_by_model_and_property_value(model:, property:, value:)
        sql_query = sql_for_find_by_model_and_property_value
        # NOTE: Do we need to ask the model for it's internal_resource?
        # TODO: no => undefined method `internal_resource' for Image:Class
        query_service.run_query(sql_query, model, property, value).first
      end

      private

      def sql_for_find_by_model_and_property_value
        # NOTE: This is querying the first element of the property, but we might
        # want to check all of the elements.
        <<-SQL
          SELECT * FROM orm_resources
          WHERE internal_resource = ? AND metadata -> ? ->> 0 = ?
          LIMIT 1;
        SQL
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
bulkrax-9.0.2 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-9.0.1 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-9.0.0 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.2.3 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.2.2 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.3.0 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.2.1 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.2.0 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.1.0 app/services/hyrax/custom_queries/find_by_source_identifier.rb
bulkrax-8.0.0 app/services/hyrax/custom_queries/find_by_source_identifier.rb