Sha256: 0cd544742f7be6a394cbd6910c4144e8d8b807769124928e1484aee35c480fdf

Contents?: true

Size: 1.87 KB

Versions: 52

Compression:

Stored size: 1.87 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: start))

          while response.documents.present?
            response.documents.each { |x| yield x }
            start += response.documents.length
            response = index.search(search_params.merge(start: 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

52 entries across 52 versions & 1 rubygems

Version Path
blacklight-spotlight-3.5.0.4 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.5.0.3 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.5.0.2 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.5.0.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.5.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.4.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.4 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.3 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.2.2 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.2.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.2 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.4.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.3.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.2.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.1.0 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.0.3 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.0.2 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.0.1 app/models/concerns/spotlight/solr_document/finder.rb
blacklight-spotlight-3.0.0 app/models/concerns/spotlight/solr_document/finder.rb