Skip to content

Commit

Permalink
🧹 Place custom queries back in Bulkrax
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkkwang committed Jan 31, 2024
1 parent ff02e13 commit 29f2264
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/services/hyrax/custom_queries/find_by_source_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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_source_identifier]
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 identifier String
def find_by_source_identifier(work_identifier:, source_identifier_value:)
sql_query = sql_by_source_identifier
query_service.run_query(sql_query, work_identifier, source_identifier_value).first
end

def sql_by_source_identifier
<<-SQL
SELECT * FROM orm_resources
WHERE metadata -> ? ->> 0 = ?;
SQL
end
end
end
end
30 changes: 30 additions & 0 deletions app/services/wings/custom_queries/find_by_source_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Wings
module CustomQueries
class FindBySourceIdentifier
# Custom query override specific to Wings
# Use:
# Hyrax.custom_queries.find_bulkrax_id(identifier: identifier, models: [ModelClass])

def self.queries
[:find_by_source_identifier]
end

attr_reader :query_service
delegate :resource_factory, to: :query_service

def initialize(query_service:)
@query_service = query_service
end

def find_by_source_identifier(identifier:, use_valkyrie: true)
af_object = ActiveFedora::Base.where("bulkrax_identifier_sim:#{identifier}").first

return af_object unless use_valkyrie

resource_factory.to_resource(object: af_object)
end
end
end
end

0 comments on commit 29f2264

Please sign in to comment.