class Sunspot::Search::QueryFacet

Attributes

name[R]

Public Instance Methods

rows(options = {}) click to toggle source
# File lib/sunspot/search/query_facet.rb, line 13
def rows(options = {})
  @rows ||=
    begin
      data = @search.facet_response['facet_queries']
      rows = []
      minimum_count =
        case
        when @options[:minimum_count] then @options[:minimum_count]
        when @options[:zeros] then 0
        else 1
        end
      @requested_facets.each do |requested_facet|
        count = data[requested_facet.boolean_phrase] || 0
        if count >= minimum_count
          rows << FacetRow.new(requested_facet.label, count, self)
        end
      end
      sort_rows!(rows)
    end
end

Private Instance Methods

limit() click to toggle source
# File lib/sunspot/search/query_facet.rb, line 61
def limit
  return @limit if defined?(@limit)
  @limit = (@options[:limit].to_i if @options[:limit].to_i > 0)
end
sort_rows!(rows) click to toggle source
# File lib/sunspot/search/query_facet.rb, line 40
def sort_rows!(rows)
  case @options[:sort] || (:count if limit)
  when :count
    rows.sort! { |lrow, rrow| rrow.count <=> lrow.count }
  when :index
    rows.sort! do |lrow, rrow|
      if lrow.respond_to?(:<=>)
        lrow.value <=> rrow.value
      elsif lrow.respond_to?(:first) && rrow.respond_to?(:first) && lrow.first.respond_to?(:<=>)
        lrow.first.value <=> rrow.first.value
      else
        lrow.value.to_s <=> rrow.value.to_s
      end
    end
  end
  if limit
    rows.replace(rows.first(limit))
  end
  rows
end