Sha256: e3404dcc4de5829673e8334a1c3b80341d32dc0b7b95cb82cfd6df69646849b0

Contents?: true

Size: 1.85 KB

Versions: 12

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Spotlight
  module SolrDocument
    ##
    # Finder methods for SolrDocuments
    module Finder
      extend ActiveSupport::Concern

      ##
      # Class level finder methods for documents
      module ClassMethods
        def find(id)
          solr_response = index.find(id)
          solr_response.documents.first
        end

        def index
          @index ||= blacklight_config.repository_class.new(blacklight_config)
        end

        def find_each(&)
          return to_enum(:find_each) unless block_given?

          start = 0
          search_params = { q: '*:*', fl: 'id', facet: false }
          response = index.search(search_params.merge(start:))

          while response.documents.present?
            response.documents.each(&)
            start += response.documents.length
            response = index.search(search_params.merge(start:))
          end
        end

        protected

        def blacklight_config
          @conf ||= Spotlight::Engine.blacklight_config
        end
      end

      # Returns true if +comparison_object+ is the same exact object, or +comparison_object+
      # is of the same type and +self+ has an ID and it is equal to +comparison_object.id+.
      #
      # Note that new records are different from any other record by definition, unless the
      # other record is the receiver itself. Besides, if you fetch existing records with
      # +select+ and leave the ID out, you're on your own, this predicate will return false.
      #
      # Note also that destroying a record preserves its ID in the model instance, so deleted
      # models are still comparable.
      def ==(other)
        super ||
          (other.instance_of?(self.class) &&
            id &&
            other.id == id)
      end

      def blacklight_solr
        self.class.index.connection
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
blacklight-spotlight-4.7.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.6.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.6.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.5.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.4.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.6 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.5 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.4 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.3 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.2 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-4.3.0 app/models/concerns/spotlight/solr_document/finder.rb