]> git.openstreetmap.org Git - rails.git/commitdiff
Change diary comments pagination to before/after id
authorAnton Khorev <tony29@yandex.ru>
Mon, 4 Sep 2023 10:12:25 +0000 (13:12 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sat, 9 Sep 2023 11:23:10 +0000 (14:23 +0300)
app/controllers/diary_entries_controller.rb
app/views/diary_entries/comments.html.erb

index dcb625d836932a9746e2aac99bc5d2952577ae92..3d0919dea2e76c1736fd7b44fe387d7da173bf67 100644 (file)
@@ -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
index 0d6ddfb01c7c007aab216b1a05ceda51486d9334..2ffce6ff6d2817e995cbde2d3dc76d1c0e8bb06a 100644 (file)
     <% end -%>
   </table>
 
-  <div class='secondary-actions clearfix'>
-    <span><%= 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 %></span>
-  </div>
+  <nav>
+    <ul class="pagination">
+      <% if @older_comments -%>
+        <li class="page-item">
+          <%= link_to t(".older_comments"), @params.merge(:before => @comments.last.id), :class => "page-link" %>
+        </li>
+      <% else -%>
+        <li class="page-item disabled">
+          <span class="page-link"><%= t(".older_comments") %></span>
+        </li>
+      <% end -%>
 
+      <% if @newer_comments -%>
+        <li class="page-item">
+          <%= link_to t(".newer_comments"), @params.merge(:after => @comments.first.id), :class => "page-link" %>
+        </li>
+      <% else -%>
+        <li class="page-item disabled">
+          <span class="page-link"><%= t(".newer_comments") %></span>
+        </li>
+      <% end -%>
+    </ul>
+  </nav>
 <% end -%>