Sha256: 432f79f9036dea28ae4de82d2e87c8e870699a2248e19fa250cdba0075e85b67

Contents?: true

Size: 1.68 KB

Versions: 11

Compression:

Stored size: 1.68 KB

Contents

module ActiveFedora::Associations
  ##
  # Finds the objects which associate with a given record and are contained
  # within the given container. Uses #repository to find the objects.
  class ContainedFinder
    attr_reader :container, :repository, :proxy_class
    delegate :contained_ids, to: :container
    # @param [#contained_ids] container a container that records are stored
    #   under.
    # @param [#translate_uri_to_id, #find] repository a repository to build
    #   objects from.
    # @param [ActiveFedora::Base] proxy_class class that represents an
    #   ore:Proxy
    def initialize(container:, repository:, proxy_class:)
      @container = container
      @repository = repository
      @proxy_class = proxy_class
    end

    # @param [ActiveFedora::Base] record a record which you want to find the
    #   reference node for.
    # @return [Array<ActiveFedora::Base>] This returns whatever type
    #   repository.find returns.
    def find(record)
      record.reload
      repository.find(matching_ids(record))
    end

    private

      def matching_ids(record)
        IDComposite.new(proxy_ids(record) & contained_ids, repository.translate_uri_to_id)
      end

      def proxy_ids(record)
        relation_subjects(record)
      end

      # This could be done with Prefer InboundReferences, but that is
      # a slow fedora call
      def relation_subjects(record)
        query = ActiveFedora::SolrQueryBuilder.construct_query_for_rel(
          [[:has_model, proxy_class.to_class_uri], [:proxyFor, record.id]]
        )
        results = ActiveFedora::SolrService.query(query, fl: 'id')
        results.map { |res| ::RDF::URI(ActiveFedora::Base.id_to_uri(res['id'])) }
      end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active-fedora-9.7.3 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.7.2 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.9.1 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.9.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.8.2 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.8.1 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.8.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.7.1 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.7.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.6.2 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.6.1 lib/active_fedora/associations/contained_finder.rb