From 673b1406be62a30c2cebc68b03f1b4df893ee23d Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 22 Dec 2020 16:18:32 -0500 Subject: [PATCH] Allow any admin to publish welcome thread (#11597) * Allow any admin to publish welcome thread * Update app/models/article.rb Co-authored-by: rhymes * Fix specs by making user for welcome thread an admin * revert spec changes and update code to include either admin, super_admin, or site mascot * add specs for scope * Modify tests and include mascot user * Fix test Co-authored-by: rhymes Co-authored-by: mstruve --- app/models/article.rb | 7 ++++--- spec/models/article_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index a22573e04..41c930d58 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -112,9 +112,10 @@ class Article < ApplicationRecord scope :admin_published_with, lambda { |tag_name| published - .where(user_id: SiteConfig.staff_user_id) - .order(published_at: :desc) - .tagged_with(tag_name) + .where(user_id: User.with_role(:super_admin) + .union(User.with_role(:admin)) + .union(id: [SiteConfig.staff_user_id, SiteConfig.mascot_user_id].compact) + .select(:id)).order(published_at: :desc).tagged_with(tag_name) } scope :user_published_with, lambda { |user_id, tag_name| diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 01350a4a4..bca4ec074 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -53,6 +53,34 @@ RSpec.describe Article, type: :model do it { is_expected.not_to allow_value("foo").for(:main_image_background_hex_color) } + describe "::admin_published_with" do + it "includes mascot-published articles" do + allow(SiteConfig).to receive(:mascot_user_id).and_return(3) + user = create(:user, id: 3) + create(:article, user: user, tags: "challenge") + expect(described_class.admin_published_with("challenge").count).to eq(1) + end + + it "includes staff-user-published articles" do + allow(SiteConfig).to receive(:staff_user_id).and_return(3) + user = create(:user, id: 3) + create(:article, user: user, tags: "challenge") + expect(described_class.admin_published_with("challenge").count).to eq(1) + end + + it "includes admin published articles" do + user = create(:user, :admin) + create(:article, user: user, tags: "challenge") + expect(described_class.admin_published_with("challenge").count).to eq(1) + end + + it "does not include regular user published articles" do + user = create(:user) + create(:article, user: user, tags: "challenge") + expect(described_class.admin_published_with("challenge").count).to eq(0) + end + end + describe "#body_markdown" do it "is unique scoped for user_id and title" do art2 = build(:article, body_markdown: article.body_markdown, user: article.user, title: article.title)