Class: Sunspot::Query::SortComposite

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/query/sort_composite.rb

Overview

The SortComposite class encapsulates an ordered collection of Sort objects. It’s necessary to keep this as a separate class as Solr takes the sort as a single parameter, so adding sorts as regular components would not merge correctly in the #to_params method.

Instance Method Summary collapse

Constructor Details

#initializeSortComposite

:nodoc:



10
11
12
# File 'lib/sunspot/query/sort_composite.rb', line 10

def initialize
  @sorts = []
end

Instance Method Details

#<<(sort) ⇒ Object

Add a sort to the composite



17
18
19
# File 'lib/sunspot/query/sort_composite.rb', line 17

def <<(sort)
  @sorts << sort
end

#include?(sort) ⇒ Boolean

Check sort presence

Returns:

  • (Boolean)


24
25
26
# File 'lib/sunspot/query/sort_composite.rb', line 24

def include?(sort)
  @sorts.any? { |s| s.to_param.include?(sort) }
end

#to_params(prefix = "") ⇒ Object

Combine the sorts into a single param by joining them



31
32
33
34
35
36
37
38
# File 'lib/sunspot/query/sort_composite.rb', line 31

def to_params(prefix = "")
  unless @sorts.empty?
    key = "#{prefix}sort".to_sym
    { key => @sorts.map { |sort| sort.to_param } * ', ' }
  else
    {}
  end
end