mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-01-19 00:46:03 +01:00
Add report filtering capabilities
This commit is contained in:
parent
2a9404e285
commit
311fc0812b
2 changed files with 53 additions and 3 deletions
|
@ -4,10 +4,10 @@ class Moderation::ReportsController < ApplicationController
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@type = params[:type]
|
filter = ReportFilter.new(filter_params)
|
||||||
@reports = list_reports(type: @type, last_id: params[:last_id])
|
@reports = filter.cursored_results(last_id: params[:last_id])
|
||||||
@reports_last_id = @reports.map(&:id).min
|
@reports_last_id = @reports.map(&:id).min
|
||||||
@more_data_available = !list_reports(type: @type, last_id: @reports_last_id, size: 1).count.zero?
|
@more_data_available = filter.cursored_results(last_id: @reports_last_id, size: 1).count.positive?
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html
|
format.html
|
||||||
|
@ -17,6 +17,10 @@ class Moderation::ReportsController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def filter_params
|
||||||
|
params.slice(*ReportFilter::KEYS).permit(*ReportFilter::KEYS)
|
||||||
|
end
|
||||||
|
|
||||||
def list_reports(type:, last_id:, size: nil)
|
def list_reports(type:, last_id:, size: nil)
|
||||||
cursor_params = { last_id:, size: }.compact
|
cursor_params = { last_id:, size: }.compact
|
||||||
|
|
||||||
|
|
46
app/models/report_filter.rb
Normal file
46
app/models/report_filter.rb
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class ReportFilter
|
||||||
|
include CursorPaginatable
|
||||||
|
|
||||||
|
define_cursor_paginator :cursored_results, :results
|
||||||
|
|
||||||
|
KEYS = %i[
|
||||||
|
user
|
||||||
|
target_user
|
||||||
|
type
|
||||||
|
].freeze
|
||||||
|
|
||||||
|
attr_reader :params
|
||||||
|
|
||||||
|
def initialize(params)
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def results
|
||||||
|
scope = Report.where(deleted: false)
|
||||||
|
.order(:created_at)
|
||||||
|
.reverse_order
|
||||||
|
|
||||||
|
params.each do |key, value|
|
||||||
|
scope.merge!(scope_for(key, value)) if value.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
scope
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def scope_for(key, value)
|
||||||
|
case key.to_s
|
||||||
|
when "user"
|
||||||
|
Report.joins(:user)
|
||||||
|
.where(users: { screen_name: value })
|
||||||
|
when "target_user"
|
||||||
|
Report.joins(:target_user)
|
||||||
|
.where(users: { screen_name: value })
|
||||||
|
when "type"
|
||||||
|
Report.where('LOWER(type) = ?', "reports::#{value}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue