From 772f6cdd7b8d622f0490a26e551f716818304573 Mon Sep 17 00:00:00 2001 From: anes <75726773+aneshodza@users.noreply.github.com> Date: Wed, 24 May 2023 22:58:39 +0200 Subject: [PATCH] Fix the org post count issue (#19503) * Fix the org post count issue * Add regression specs * Move things around and sort them better * Implement PR review * Revert accidental changes --------- Co-authored-by: Mac Siri --- app/models/organization.rb | 4 ++++ app/views/organizations/_sidebar.html.erb | 2 +- spec/models/organization_spec.rb | 15 ++++++++++++ .../user_views_an_organization_spec.rb | 24 +++++++++++++++---- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/models/organization.rb b/app/models/organization.rb index b2fe0e130..cf066f5e5 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -120,6 +120,10 @@ class Organization < ApplicationRecord organization_memberships.count == 1 && articles.count.zero? && credits.count.zero? end + def public_articles_count + articles.published.count + end + # NOTE: We use Organization and User objects interchangeably. Since the former # don't have profiles we return self instead. def profile diff --git a/app/views/organizations/_sidebar.html.erb b/app/views/organizations/_sidebar.html.erb index 6bd139873..9f6f3458a 100644 --- a/app/views/organizations/_sidebar.html.erb +++ b/app/views/organizations/_sidebar.html.erb @@ -46,7 +46,7 @@
<%= crayons_icon_tag(:post, class: "mr-3 color-base", title: t("views.organizations.side.post.icon")) %> - <%= t "views.organizations.side.post.text", count: @stories.size %> + <%= t "views.organizations.side.post.text", count: @organization.public_articles_count %>
<%= crayons_icon_tag(:team, class: "mr-3 color-base", title: t("views.organizations.side.member.icon")) %> diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 2cd01cba1..c0b861b20 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -340,6 +340,21 @@ RSpec.describe Organization do end end + describe "#public_articles_count" do + it "returns the count of published articles" do + published_articles = create_list(:article, 2, organization: organization, published: true) + create_list(:article, 1, organization: organization, published: false) + + expect(organization.public_articles_count).to eq(published_articles.count) + end + + it "returns 0 if there are no published articles" do + create_list(:article, 2, organization: organization, published: false) + + expect(organization.public_articles_count).to eq(0) + end + end + describe ".simple_name_match" do before do create(:organization, name: "Not Matching") diff --git a/spec/system/organization/user_views_an_organization_spec.rb b/spec/system/organization/user_views_an_organization_spec.rb index ea3220c1e..a3bcc9865 100644 --- a/spec/system/organization/user_views_an_organization_spec.rb +++ b/spec/system/organization/user_views_an_organization_spec.rb @@ -4,13 +4,12 @@ RSpec.describe "Organization index" do let!(:org_user) { create(:user, :org_member) } let(:organization) { org_user.organizations.first } - before do - create_list(:article, 2, organization: organization) - end - context "when user does not follow organization" do context "when 2 articles" do - before { visit "/#{organization.slug}" } + before do + create_list(:article, 2, organization: organization) + visit "/#{organization.slug}" + end it "shows the header", js: true do within("h1.crayons-title") { expect(page).to have_content(organization.name) } @@ -30,6 +29,10 @@ RSpec.describe "Organization index" do end end + it "shows the right amount of articles in sidebar" do + expect(page).to have_content("2 posts published") + end + it "shows the proper title tag" do expect(page).to have_title("#{organization.name} - #{Settings::Community.community_name}") end @@ -41,6 +44,17 @@ RSpec.describe "Organization index" do visit "/#{organization.slug}" end end + + context "when more than 8 articles" do + before do + create_list(:article, 9, organization: organization) + visit "/#{organization.slug}" + end + + it "tells the user the correct amount of posts published" do + expect(page).to have_content("9 posts published") + end + end end context "when user follows an organization" do