From 299c59e34f4a9b59f5d1f02efacc37e4c8dd3bcf Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 4 Sep 2023 13:12:25 +0300 Subject: [PATCH] Change diary comments pagination to before/after id --- app/controllers/diary_entries_controller.rb | 24 +++++++++++++------ app/views/diary_entries/comments.html.erb | 26 +++++++++++++++++---- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index dcb625d83..3d0919dea 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -248,15 +248,25 @@ class DiaryEntriesController < ApplicationController def comments @title = t ".title", :user => @user.display_name - conditions = { :user_id => @user } + comments = DiaryComment.where(:users => @user) + comments = comments.visible unless can? :unhidecomment, DiaryEntry - conditions[:visible] = true unless can? :unhidecomment, DiaryEntry + @params = params.permit(:display_name) - @comment_pages, @comments = paginate(:diary_comments, - :conditions => conditions, - :order => "created_at DESC", - :per_page => 20) - @page = (params[:page] || 1).to_i + @comments = if params[:before] + comments.where("diary_comments.id < ?", params[:before]).order(:id => :desc) + elsif params[:after] + comments.where("diary_comments.id > ?", params[:after]).order(:id => :asc) + else + comments.order(:id => :desc) + end + + @comments = @comments.limit(20) + @comments = @comments.includes(:user) + @comments = @comments.sort.reverse + + @newer_comments = @comments.count.positive? && comments.exists?(["diary_comments.id > ?", @comments.first.id]) + @older_comments = @comments.count.positive? && comments.exists?(["diary_comments.id < ?", @comments.last.id]) end private diff --git a/app/views/diary_entries/comments.html.erb b/app/views/diary_entries/comments.html.erb index 0d6ddfb01..2ffce6ff6 100644 --- a/app/views/diary_entries/comments.html.erb +++ b/app/views/diary_entries/comments.html.erb @@ -24,9 +24,27 @@ <% end -%> -
- <%= link_to t(".older_comments"), :page => @comment_pages.current.next if @comment_pages.current.next %> - <%= link_to t(".newer_comments"), :page => @comment_pages.current.previous if @comment_pages.current.previous %> -
+ <% end -%> -- 2.39.5