about summary refs log tree commit diff
path: root/app/models/admin/status_filter.rb
blob: ce5bb5f4610bd19eeebf75c31a8d2231c181cef0 (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
# frozen_string_literal: true

class Admin::StatusFilter
  KEYS = %i(
    media
    id
    report_id
  ).freeze

  attr_reader :params

  def initialize(account, params)
    @account = account
    @params  = params
  end

  def results
    scope = @account.statuses.where(visibility: [:public, :unlisted])

    params.each do |key, value|
      next if %w(page report_id).include?(key.to_s)

      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 'media'
      Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
    when 'id'
      Status.where(id: value)
    else
      raise "Unknown filter: #{key}"
    end
  end
end