Sha256: dbc238864d8e040f8bd772ea7cc6b6aaba4475358a27ae5e3b2931d916703411

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

module Spotlight
  # Creates solr documents for the documents in a resource
  class SolrDocumentBuilder
    def initialize(resource)
      @resource = resource
    end

    attr_reader :resource
    delegate :exhibit, :document_model, to: :resource

    ##
    # @return an enumerator of all the indexable documents for this resource
    def documents_to_index
      data = to_solr
      return [] if data.blank?
      data &&= [data] if data.is_a? Hash

      return to_enum(:documents_to_index) { data.size } unless block_given?

      data.lazy.reject(&:blank?).each do |doc|
        yield doc.reverse_merge(exhibit_solr_doc(doc[unique_key]).to_solr)
      end
    end

    protected

    ##
    # @abstract
    # Convert this resource into zero-to-many new solr documents. The data here
    # should be merged into the resource-specific {#to_solr} data.
    #
    # @return [Hash] a single solr document hash
    # @return [Enumerator<Hash>] multiple solr document hashes. This can be a
    #   simple array, or an lazy enumerator
    def to_solr
      spotlight_resource_metadata_for_solr
    end

    private

    # Null object for SolrDocument
    module NilSolrDocument
      def self.to_solr
        {}
      end
    end

    ##
    # Get any exhibit-specific metadata stored in e.g. sidecars, tags, etc
    # This needs the generated solr document
    # @returns [#to_solr] something that responds to `to_solr'
    def exhibit_solr_doc(id)
      return NilSolrDocument unless document_model || id.present?
      document_model.build_for_exhibit(id, exhibit)
    end

    def unique_key
      if document_model
        document_model.unique_key.to_sym
      else
        :id
      end
    end

    def spotlight_resource_metadata_for_solr
      {
        Spotlight::Engine.config.resource_global_id_field => (resource.to_global_id.to_s if resource.persisted?),
        document_model.resource_type_field => resource.class.to_s.tableize
      }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
blacklight-spotlight-0.26.1 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.26.0 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.25.0 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.24.0 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.23.0 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.22.0 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.21.0 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.20.3 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.20.2 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.20.1 app/services/spotlight/solr_document_builder.rb
blacklight-spotlight-0.20.0 app/services/spotlight/solr_document_builder.rb