Commit 2890959f authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets Committed by Grzegorz Bizon

Filter abuse reports by user

In admin area, add a user filter to abuse reports page.
It will allow admin to find a report for specific user.
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 2a59cb5f
/* eslint-disable no-new */
import AbuseReports from './abuse_reports';
import UsersSelect from '~/users_select';
document.addEventListener('DOMContentLoaded', () => new AbuseReports());
document.addEventListener('DOMContentLoaded', () => {
new AbuseReports();
new UsersSelect();
});
# frozen_string_literal: true
class Admin::AbuseReportsController < Admin::ApplicationController
# rubocop: disable CodeReuse/ActiveRecord
def index
@abuse_reports = AbuseReport.order(id: :desc).page(params[:page])
@abuse_reports = @abuse_reports.includes(:user, :reporter)
@abuse_reports = AbuseReportsFinder.new(params).execute
end
# rubocop: enable CodeReuse/ActiveRecord
def destroy
abuse_report = AbuseReport.find(params[:id])
......
# frozen_string_literal: true
class AbuseReportsFinder
attr_reader :params
def initialize(params = {})
@params = params
end
def execute
reports = AbuseReport.all
reports = reports.by_user(params[:user_id]) if params[:user_id].present?
reports.with_order_id_desc
.with_users
.page(params[:page])
end
end
......@@ -2,6 +2,7 @@
class AbuseReport < ApplicationRecord
include CacheMarkdownField
include Sortable
cache_markdown_field :message, pipeline: :single_line
......@@ -13,6 +14,9 @@ class AbuseReport < ApplicationRecord
validates :message, presence: true
validates :user_id, uniqueness: { message: 'has already been reported' }
scope :by_user, -> (user) { where(user_id: user) }
scope :with_users, -> { includes(:reporter, :user) }
# For CacheMarkdownField
alias_method :author, :reporter
......
- page_title 'Abuse Reports'
%h3.page-title Abuse Reports
%hr
- page_title _('Abuse Reports')
%h3.page-title= _('Abuse Reports')
.row-content-block.second-block
= form_tag admin_abuse_reports_path, method: :get, class: 'filter-form' do
.filter-categories.flex-fill
.filter-item.inline
= dropdown_tag(user_dropdown_label(params[:user_id], 'User'),
options: { toggle_class: 'js-filter-submit js-user-search',
title: _('Filter by user'), filter: true, filterInput: 'input#user-search',
dropdown_class: 'dropdown-menu-selectable dropdown-menu-user js-filter-submit',
placeholder: _('Search users'),
data: { current_user: true, field_name: 'user_id' }})
.abuse-reports
- if @abuse_reports.present?
.table-holder
......
---
title: Add user filtering to abuse reports page
merge_request: 19365
author:
type: changed
......@@ -7407,6 +7407,9 @@ msgstr ""
msgid "Filter by two-factor authentication"
msgstr ""
msgid "Filter by user"
msgstr ""
msgid "Filter projects"
msgstr ""
......
......@@ -51,5 +51,29 @@ describe "Admin::AbuseReports", :js do
end
end
end
describe 'filtering by user' do
let!(:user2) { create(:user) }
let!(:abuse_report) { create(:abuse_report, user: user) }
let!(:abuse_report_2) { create(:abuse_report, user: user2) }
it 'shows only single user report' do
visit admin_abuse_reports_path
page.within '.filter-form' do
click_button 'User'
wait_for_requests
page.within '.dropdown-menu-user' do
click_link user2.name
end
wait_for_requests
end
expect(page).to have_content(user2.name)
expect(page).not_to have_content(user.name)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe AbuseReportsFinder, '#execute' do
let(:params) { {} }
let!(:user1) { create(:user) }
let!(:user2) { create(:user) }
let!(:abuse_report_1) { create(:abuse_report, user: user1) }
let!(:abuse_report_2) { create(:abuse_report, user: user2) }
subject { described_class.new(params).execute }
context 'empty params' do
it 'returns all abuse reports' do
expect(subject).to match_array([abuse_report_1, abuse_report_2])
end
end
context 'params[:user_id] is present' do
let(:params) { { user_id: user2 } }
it 'returns abuse reports for the specified user' do
expect(subject).to match_array([abuse_report_2])
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment