Class: Tire::Search::MultiSort

Inherits:
Sort
  • Object
show all
Defined in:
lib/tire-multisort/tire/search/multi_sort.rb

Overview

EXAMPLE of using:

multi_sort sort, order do

by_default :premium

by :date
by :rating,    invert(order)

by :name,      :title
by :rating,    :licensee_rating
by :relevance, :_score
by :category,  :category_name
by :distance,  :_geo_distance, location: "test", unit: 'mi'
by :premium,   :premium, :desc

by_collection  :category,  [:category, :name]
by_collection  :premium,   [:premium, :relevance, :name]

end

Instance Method Summary collapse

Constructor Details

#initialize(sort_param, order, &block) ⇒ MultiSort

Returns a new instance of MultiSort.



30
31
32
33
34
35
36
37
38
39
# File 'lib/tire-multisort/tire/search/multi_sort.rb', line 30

def initialize(sort_param, order, &block)
  @collections = {}
  @fields = {}

  super(&block)

  @sort = sort_by(sort_param)
  @order = order
  fields_with_order.each { |opts| origin_by *opts }
end

Instance Method Details

#by(sort, *options) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/tire-multisort/tire/search/multi_sort.rb', line 53

def by(sort, *options)
  if options.blank? || [:desc, :asc].include?(options.first)
    @fields[sort] = options.unshift(sort)
  else
    @fields[sort] = options
  end
end

#by_collection(sort, collection) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/tire-multisort/tire/search/multi_sort.rb', line 45

def by_collection(sort, collection)
  if collection.is_a? Array
    @collections[sort] = collection
  else
    raise ArgumentError, "expect Array (got #{collection.class}) for collection param"
  end
end

#by_default(sort_param) ⇒ Object



41
42
43
# File 'lib/tire-multisort/tire/search/multi_sort.rb', line 41

def by_default(sort_param)
  @default = sort_by(sort_param)
end

#invert(order) ⇒ Object



61
62
63
# File 'lib/tire-multisort/tire/search/multi_sort.rb', line 61

def invert(order)
  order.to_s == 'asc' ? :desc : :asc
end

#origin_byObject



28
# File 'lib/tire-multisort/tire/search/multi_sort.rb', line 28

alias :origin_by :by