Create Article#admin_published_with scope (#6794)

This commit is contained in:
Mac Siri 2020-03-24 18:15:43 -04:00 committed by GitHub
parent e3f91783c9
commit 84e84af8d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 24 deletions

View file

@ -58,7 +58,7 @@ class PagesController < ApplicationController
end
def welcome
daily_thread = latest_published_thread("welcome")
daily_thread = Article.admin_published_with("welcome").first
if daily_thread
redirect_to daily_thread.path
else
@ -68,7 +68,7 @@ class PagesController < ApplicationController
end
def challenge
daily_thread = latest_published_thread("challenge")
daily_thread = Article.admin_published_with("challenge").first
if daily_thread
redirect_to daily_thread.path
else
@ -88,13 +88,4 @@ class PagesController < ApplicationController
render :show if @page
set_surrogate_key_header "crayons_page"
end
private
def latest_published_thread(tag_name)
Article.published.
where(user_id: SiteConfig.staff_user_id).
order("published_at ASC").
tagged_with(tag_name).last
end
end

View file

@ -88,16 +88,23 @@ class Article < ApplicationRecord
scope :published, -> { where(published: true) }
scope :unpublished, -> { where(published: false) }
scope :admin_published_with, lambda { |tag_name|
published.
where(user_id: SiteConfig.staff_user_id).
order(published_at: :desc).
tagged_with(tag_name)
}
scope :cached_tagged_with, ->(tag) { where("cached_tag_list ~* ?", "^#{tag},| #{tag},|, #{tag}$|^#{tag}$") }
scope :cached_tagged_by_approval_with, ->(tag) { cached_tagged_with(tag).where(approved: true) }
scope :active_help, lambda {
published.
cached_tagged_with("help").
order("created_at DESC").
where("published_at > ? AND comments_count < ? AND score > ?", 12.hours.ago, 6, -4)
}
published.
cached_tagged_with("help").
order(created_at: :desc).
where("published_at > ? AND comments_count < ? AND score > ?", 12.hours.ago, 6, -4)
}
scope :limited_column_select, lambda {
select(:path, :title, :id, :published,

View file

@ -21,7 +21,7 @@ module Broadcasts
end
def commented_on_welcome_thread?
welcome_thread = latest_published_thread("welcome")
welcome_thread = Article.admin_published_with("welcome").first
Comment.where(commentable: welcome_thread, user: user).any?
end
@ -31,12 +31,6 @@ module Broadcasts
@welcome_broadcast ||= Broadcast.find_by(title: "Welcome Notification: welcome_thread")
end
def latest_published_thread(tag_name)
Article.published.
order("published_at ASC").
cached_tagged_with(tag_name).last
end
attr_reader :user
end
end

View file

@ -9,6 +9,11 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
before do
allow(User).to receive(:mascot_account).and_return(mascot_account)
SiteConfig.staff_user_id = mascot_account.id
end
after do
SiteConfig.staff_user_id = 1
end
context "when sending a set_up_profile notification" do
@ -20,7 +25,7 @@ RSpec.describe Broadcasts::WelcomeNotification::Generator, type: :service do
context "when sending a welcome_thread notification" do
before do
welcome_thread_article = create(:article, title: "Welcome Thread - v0", published: true, tags: "welcome")
welcome_thread_article = create(:article, title: "Welcome Thread - v0", published: true, tags: "welcome", user: mascot_account)
create(:comment, commentable: welcome_thread_article, commentable_type: "Article", user: user)
end