From e2e54b35c64a15b40b47da811ee9c9a37791fde3 Mon Sep 17 00:00:00 2001 From: Anshuman Bhardwaj Date: Mon, 17 Jan 2022 19:22:34 -0800 Subject: [PATCH] Series count to only include non empty series (#16130) Co-authored-by: Michael Kohl --- app/controllers/collections_controller.rb | 2 +- app/controllers/dashboards_controller.rb | 1 + app/models/collection.rb | 2 ++ app/views/dashboards/_actions.html.erb | 2 +- 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 18abeb6cb..196bd3bd8 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -1,7 +1,7 @@ class CollectionsController < ApplicationController def index @user = User.find_by!(username: params[:username]) - @collections = @user.collections.joins(:articles).distinct.order(created_at: :desc) + @collections = @user.collections.non_empty.order(created_at: :desc) end def show diff --git a/app/controllers/dashboards_controller.rb b/app/controllers/dashboards_controller.rb index 676091a18..24090495c 100644 --- a/app/controllers/dashboards_controller.rb +++ b/app/controllers/dashboards_controller.rb @@ -25,6 +25,7 @@ class DashboardsController < ApplicationController @articles = @articles.includes(:collection).sorting(params[:sort]).decorate @articles = Kaminari.paginate_array(@articles).page(params[:page]).per(50) + @collections_count = target.collections.non_empty.count end def following_tags diff --git a/app/models/collection.rb b/app/models/collection.rb index 3c9e4696c..a917ea414 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -6,6 +6,8 @@ class Collection < ApplicationRecord validates :slug, presence: true, uniqueness: { scope: :user_id } + scope :non_empty, -> { joins(:articles).distinct } + after_touch :touch_articles def self.find_series(slug, user) diff --git a/app/views/dashboards/_actions.html.erb b/app/views/dashboards/_actions.html.erb index bb6b1f844..3a88db0f7 100644 --- a/app/views/dashboards/_actions.html.erb +++ b/app/views/dashboards/_actions.html.erb @@ -14,7 +14,7 @@ href="<%= user_series_path(current_user.username) %>" <%= params[:action] == "series" && (params[:which] == "organization" || params[:which].blank?) ? ' aria-current="page"'.html_safe : "" %>> <%= t("views.dashboard.actions.series") %> - <%= @user.collections.count %> + <%= @collections_count %>