Sha256: 906c2ab39cf546c1c91b8cd1c623f3591e208bca6afcc9792d259f263cf5db0b

Contents?: true

Size: 1.46 KB

Versions: 17

Compression:

Stored size: 1.46 KB

Contents

module RailsConnector

  # This class provides a basic implementation for accessing the search using the cms api.
  # It can be activated by making it the superclass of SearchRequest.
  # It should be customized by subclassing.
  # @api public
  class CmsApiSearchRequest

    # Takes +query_string+ and +options+ for accessing Cms Api Search.
    #
    # +options+ is a hash and may include:
    #
    # <tt>:limit</tt>:: The maximum number of hits
    # <tt>:offset</tt>:: The search offset
    # @api public
    def initialize(query_string, options = {})
      @query_string = query_string
      @options = options
    end

    # Accesses Cms Api Search using #query and fetches search hits.
    # @api public
    def fetch_hits
      search_enum = search_results

      result = SES::SearchResult.new(search_enum.size)
      search_enum.take(@options[:limit] || 10).each do |obj|
        hard_coded_score = 1
        result << SES::Hit.new(obj.id, hard_coded_score, {}, obj)
      end

      result
    end

    private

    def search_results
      now = Time.now
      search_enum = Obj.where(:_valid_from, :is_less_than, now.to_iso
          ).and_not(:_valid_until, :is_less_than, now.to_iso
          ).and_not(:_obj_class, :equals, 'Image'
          ).offset(@options[:offset] || 0
          ).batch_size(@options[:limit] || 10)

      @query_string.strip.split(/[\s]+/).each do |word|
        search_enum.and(:*, :contains_prefix, word)
      end

      search_enum
    end
  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.3.174.51542603 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.3.115.227021242 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.3.73.75172665 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.3.2.153416031 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.3.67.110847540 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.3.1.23895778 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.2.36.82613853 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.2.7.128454143 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.1.26.236500544 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.1 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.539.93842523 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.529.114081979 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.518.30999728 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.515.34928522 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.498.46559598 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.480.261594408 lib/rails_connector/cms_api_search_request.rb
infopark_cloud_connector-6.8.0.444.171626367 lib/rails_connector/cms_api_search_request.rb