Merge pull request #269 from Retrospring/feature/mod-pagination
Pagination in moderation panel
This commit is contained in:
commit
75d24db1b9
|
@ -3,10 +3,13 @@ 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
|
||||
@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
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,2 +1,11 @@
|
|||
- @reports.each do |r|
|
||||
#reports
|
||||
- @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'
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
$('#reports').append('<% @reports.each do |report| %><%= j render "moderation/moderationbox", report: report %><% end %>');
|
||||
<% if @more_data_available %>
|
||||
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @reports_last_id, permitted_params: %i[type] %>');
|
||||
<% else %>
|
||||
$('#pagination, #load-more-btn').remove();
|
||||
<% end %>
|
Loading…
Reference in New Issue