From 2219366af6cde03bd45ba393b13c6828eeea3fbb Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 23 Jun 2020 17:29:24 -0400 Subject: [PATCH] [deploy] Paginate /dashboard posts (#8868) * Paginate /dashboard * Fix awkward pagination --- app/controllers/dashboards_controller.rb | 4 ++++ app/views/dashboards/_analytics.html.erb | 4 ++-- app/views/dashboards/_dashboard_article_row.html.erb | 7 +++---- app/views/dashboards/show.html.erb | 6 ++++-- spec/requests/dashboard_spec.rb | 12 ++++++++++++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index a2349f9cd..5937e9844 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -22,7 +22,11 @@ class DashboardsController < ApplicationController @articles = target.articles.includes(:organization) end + @reactions_count = @articles.sum(&:public_reactions_count) + @page_views_count = @articles.sum(&:page_views_count) + @articles = @articles.sorting(params[:sort]).decorate + @articles = Kaminari.paginate_array(@articles).page(params[:page]).per(50) # Updates analytics in background if appropriate update_analytics = @articles && SiteConfig.ga_fetch_rate < 50 # Rate limited, sometimes we throttle down diff --git a/app/views/dashboards/_analytics.html.erb b/app/views/dashboards/_analytics.html.erb index e6ab64aa6..80676c3ca 100644 --- a/app/views/dashboards/_analytics.html.erb +++ b/app/views/dashboards/_analytics.html.erb @@ -1,8 +1,8 @@ -<% num_views = @articles.sum(&:page_views_count) %> +<% num_views = @page_views_count %>
- <%= number_with_delimiter(@articles.sum(&:public_reactions_count), delimeter: ",") %> + <%= number_with_delimiter(@reactions_count, delimeter: ",") %>
" alt="heart" /> " alt="unicorn" /> diff --git a/app/views/dashboards/_dashboard_article_row.html.erb b/app/views/dashboards/_dashboard_article_row.html.erb index 0c9ad9035..c3a9b5a85 100644 --- a/app/views/dashboards/_dashboard_article_row.html.erb +++ b/app/views/dashboards/_dashboard_article_row.html.erb @@ -100,10 +100,6 @@ <% elsif @user == article.user %> Delete <% end %> - <% if @current_user_pro %> - Stats - <% end %> - Edit
@@ -112,6 +108,9 @@
+ <% if @current_user_pro %> + Stats + <% end %> <%= form_for(article, method: :patch, remote: true, authenticity_token: true, html: { "data-type": "json", class: "js-archive-toggle" }) do |f| %> <%= f.hidden_field :archived, value: !article.archived %> diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index 3f32932b8..d79aa0c6b 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -42,7 +42,7 @@ Create/Manage Listings - <%= select_tag "dashhboard_sort", options_for_select(sort_options, params[:sort]), 'aria-label': 'Sort By' %> + <%= select_tag "dashhboard_sort", options_for_select(sort_options, params[:sort]), 'aria-label': "Sort By" %> <% if @articles.any? {|article| article.archived } %> <%= link_to "Show archived", "javascript:;", id: "toggleArchivedLink" %> <% end %> @@ -64,7 +64,9 @@ <% end %> - +
+ <%= paginate @articles, params: { i: nil } %> +
<% else %>

This is where you can manage your posts, but you haven't written anything yet.

diff --git a/spec/requests/dashboard_spec.rb b/spec/requests/dashboard_spec.rb index 142abea95..1abbf08bd 100644 --- a/spec/requests/dashboard_spec.rb +++ b/spec/requests/dashboard_spec.rb @@ -37,6 +37,18 @@ RSpec.describe "Dashboards", type: :request do get "/dashboard" expect(response.body).to include "Delete" end + + it "renders pagination if minimum amount of posts" do + create_list(:article, 52, user: user) + get "/dashboard" + expect(response.body).to include "pagination" + end + + it "does not render pagination if less than one full page" do + create_list(:article, 3, user: user) + get "/dashboard" + expect(response.body).not_to include "pagination" + end end context "when logged in as a super admin" do