about summary refs log tree commit diff
path: root/app/models/trends/tag_filter.rb
blob: 3b142efc450ba08278be25ffe5d7a9f315f739ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

class Trends::TagFilter
  KEYS = %i(
    trending
    status
  ).freeze

  attr_reader :params

  def initialize(params)
    @params = params
  end

  def results
    scope = begin
      if params[:status] == 'pending_review'
        Tag.unscoped
      else
        trending_scope
      end
    end

    params.each do |key, value|
      next if key.to_s == 'page'

      scope.merge!(scope_for(key, value.to_s.strip)) if value.present?
    end

    scope
  end

  private

  def scope_for(key, value)
    case key.to_s
    when 'status'
      status_scope(value)
    else
      raise "Unknown filter: #{key}"
    end
  end

  def trending_scope
    Trends.tags.query.to_arel
  end

  def status_scope(value)
    case value.to_s
    when 'approved'
      Tag.trendable
    when 'rejected'
      Tag.not_trendable
    when 'pending_review'
      Tag.pending_review
    else
      raise "Unknown status: #{value}"
    end
  end
end