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 <mac@forem.com>
This commit is contained in:
anes 2023-05-24 22:58:39 +02:00 committed by GitHub
parent e72160284c
commit 772f6cdd7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 6 deletions

View file

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

View file

@ -46,7 +46,7 @@
<div class="crayons-card crayons-card--secondary p-4">
<div class="flex items-center mb-4">
<%= 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 %>
</div>
<div class="flex items-center">
<%= crayons_icon_tag(:team, class: "mr-3 color-base", title: t("views.organizations.side.member.icon")) %>

View file

@ -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")

View file

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