Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created July 14, 2020 22:08
Show Gist options
  • Select an option

  • Save elrayle/246a8bc4b118ee66660259d8a55fecee to your computer and use it in GitHub Desktop.

Select an option

Save elrayle/246a8bc4b118ee66660259d8a55fecee to your computer and use it in GitHub Desktop.

Revisions

  1. elrayle created this gist Jul 14, 2020.
    40 changes: 40 additions & 0 deletions find_publication_by_identifier.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    # Wings (aka ActiveFedora) specific implementation that finds a publication given its unique identifier.
    module CustomQueries
    module Wings
    class FindPublicationByIdentifier
    # Define the queries that can be fulfilled by this custom query.
    def self.queries
    [:find_publication_by_identifier]
    end

    attr_reader :query_service
    delegate :resource_factory, to: :query_service

    def initialize(query_service:)
    @query_service = query_service
    end

    ##
    # Find by identifier (e.g. slug_id)
    # @param identifier [String] agency identifier (e.g. slug_id, other string assigned by agency)
    # @return [Array<Valkyrie::Resource>]
    def find_publication_by_identifier(identifier:)
    af_find_publication_by_identifier(identifier: identifier)
    end

    private

    # Find by identifier (e.g. slug_id)
    # @param identifier [String] agency identifier (e.g. slug_id, other string assigned by agency)
    # @return [Array<Valkyrie::Resource>]
    # @raise [Hyrax::ObjectNotFoundError]
    def af_find_publication_by_identifier(identifier:)
    results = Publication.where(identifier: [identifier])
    raise Hyrax::ObjectNotFoundError, "Publication with identifier '#{identifier}' not found." unless results.count.positive?
    resource_factory.to_resource(object: results.first)
    rescue ActiveFedora::ObjectNotFoundError, Ldp::Gone
    raise Hyrax::ObjectNotFoundError, "Publication with identifier '#{identifier}' not found." unless results.count.positive?
    end
    end
    end
    end