Commit aac77781 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'dz-abuse-reports-filter' into 'master'

Filter abuse reports by user

See merge request gitlab-org/gitlab!19365
parents 2a59cb5f 2890959f
/* eslint-disable no-new */
import AbuseReports from './abuse_reports'; 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 # frozen_string_literal: true
class Admin::AbuseReportsController < Admin::ApplicationController class Admin::AbuseReportsController < Admin::ApplicationController
# rubocop: disable CodeReuse/ActiveRecord
def index def index
@abuse_reports = AbuseReport.order(id: :desc).page(params[:page]) @abuse_reports = AbuseReportsFinder.new(params).execute
@abuse_reports = @abuse_reports.includes(:user, :reporter)
end end
# rubocop: enable CodeReuse/ActiveRecord
def destroy def destroy
abuse_report = AbuseReport.find(params[:id]) 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 @@ ...@@ -2,6 +2,7 @@
class AbuseReport < ApplicationRecord class AbuseReport < ApplicationRecord
include CacheMarkdownField include CacheMarkdownField
include Sortable
cache_markdown_field :message, pipeline: :single_line cache_markdown_field :message, pipeline: :single_line
...@@ -13,6 +14,9 @@ class AbuseReport < ApplicationRecord ...@@ -13,6 +14,9 @@ class AbuseReport < ApplicationRecord
validates :message, presence: true validates :message, presence: true
validates :user_id, uniqueness: { message: 'has already been reported' } 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 # For CacheMarkdownField
alias_method :author, :reporter alias_method :author, :reporter
......
- page_title 'Abuse Reports' - page_title _('Abuse Reports')
%h3.page-title Abuse Reports
%hr %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 .abuse-reports
- if @abuse_reports.present? - if @abuse_reports.present?
.table-holder .table-holder
......
---
title: Add user filtering to abuse reports page
merge_request: 19365
author:
type: changed
...@@ -7407,6 +7407,9 @@ msgstr "" ...@@ -7407,6 +7407,9 @@ msgstr ""
msgid "Filter by two-factor authentication" msgid "Filter by two-factor authentication"
msgstr "" msgstr ""
msgid "Filter by user"
msgstr ""
msgid "Filter projects" msgid "Filter projects"
msgstr "" msgstr ""
......
...@@ -51,5 +51,29 @@ describe "Admin::AbuseReports", :js do ...@@ -51,5 +51,29 @@ describe "Admin::AbuseReports", :js do
end end
end 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
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