Sha256: 9482b351d9e6bf33084d7a8ba795559d04d4e045d6f39ff6060c2f390489acc9

Contents?: true

Size: 1.61 KB

Versions: 29

Compression:

Stored size: 1.61 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]]
        )
        ActiveFedora::SolrService.query(query, fl: 'id').map(&:rdf_uri)
      end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
active-fedora-10.3.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.3.0.rc2 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc7 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.3.0.rc1 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc6 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.2.1 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc5 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc4 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.2.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc3 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc2 lib/active_fedora/associations/contained_finder.rb
active-fedora-11.0.0.rc1 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.1.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.1.0.rc1 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.0.0 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.0.0.beta4 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.0.0.beta3 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.0.0.beta2 lib/active_fedora/associations/contained_finder.rb
active-fedora-10.0.0.beta1 lib/active_fedora/associations/contained_finder.rb
active-fedora-9.13.0 lib/active_fedora/associations/contained_finder.rb