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
<% 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 -%>