1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  def self.search(params)
    if params[:query].present?
      tire.search(load: true) do
        query { string params[:query], default_operator: "AND" }
      end
    else
      filter params
    end
  end

  def self.filter(params)
    discussions = self.includes(:author).paginate(page: params[:page], per_page: 10)
    discussions = discussions.where(category: params[:category].singularize) if params[:category]
    if params[:filter]
      filter = params[:filter].to_sym
      if [:hot, :new, :top].include? filter
        discussions.open.send "order_by_#{filter}"
      else
        discussions.order_by_new.where(status: filter)
      end
    else
      discussions.open.order_by_hot
    end
  end