From 2cab96b9afd468fa12ef459362ed5e94df026524 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Mon, 3 Jan 2022 22:43:37 +0100 Subject: [PATCH] Implement cursored pagination in Moderation Panel --- app/controllers/moderation_controller.rb | 25 +++++++++++++++++++----- app/models/report.rb | 15 ++++++++++++++ app/views/moderation/index.haml | 8 ++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/controllers/moderation_controller.rb b/app/controllers/moderation_controller.rb index 792fee95..f2ba17a0 100644 --- a/app/controllers/moderation_controller.rb +++ b/app/controllers/moderation_controller.rb @@ -3,11 +3,14 @@ class ModerationController < ApplicationController def index @type = params[:type] - @reports = if @type == 'all' - Report.where(deleted: false).reverse_order - else - Report.where(deleted: false).where('LOWER(type) = ?', "reports::#{@type}").reverse_order - end + @reports = list_reports(type: @type, last_id: params[:last_id]) + @reports_last_id = @reports.map(&:id).min + @more_data_available = !list_reports(type: @type, last_id: @reports_last_id, size: 1).count.zero? + + respond_to do |format| + format.html + format.js { render layout: false } + end end def priority @@ -62,4 +65,16 @@ class ModerationController < ApplicationController render template: 'moderation/priority' end + + private + + def list_reports(type:, last_id:, size: nil) + cursor_params = { last_id: last_id, size: size }.compact + + if type == 'all' + Report.cursored_reports(**cursor_params) + else + Report.cursored_reports_of_type(type, **cursor_params) + end + end end diff --git a/app/models/report.rb b/app/models/report.rb index 9ed86152..b801d322 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -13,4 +13,19 @@ class Report < ApplicationRecord def votes moderation_votes.where(upvote: true).count - moderation_votes.where(upvote: false).count end + + class << self + include CursorPaginatable + + define_cursor_paginator :cursored_reports, :list_reports + define_cursor_paginator :cursored_reports_of_type, :list_reports_of_type + + def list_reports(options = {}) + self.where(options.merge!(deleted: false)).reverse_order + end + + def list_reports_of_type(type, options = {}) + self.where(options.merge!(deleted: false)).where('LOWER(type) = ?', "reports::#{type}").reverse_order + end + end end diff --git a/app/views/moderation/index.haml b/app/views/moderation/index.haml index 0e7b0a7a..a3d90ac1 100644 --- a/app/views/moderation/index.haml +++ b/app/views/moderation/index.haml @@ -1,2 +1,10 @@ - @reports.each do |r| = render 'moderation/moderationbox', report: r + +- unless @reports.count.zero? + = render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @reports_last_id, permitted_params: %i[type] + + - if @more_data_available + .d-flex.justify-content-center.justify-content-sm-start + %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @reports_last_id } } + = t 'views.actions.load'