diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 91ef0c727..427ef803f 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -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 diff --git a/app/models/article.rb b/app/models/article.rb index a595d2e43..5d314418d 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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, diff --git a/app/services/broadcasts/welcome_notification/generator.rb b/app/services/broadcasts/welcome_notification/generator.rb index c3e6c75c2..d33553177 100644 --- a/app/services/broadcasts/welcome_notification/generator.rb +++ b/app/services/broadcasts/welcome_notification/generator.rb @@ -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 diff --git a/spec/services/broadcasts/welcome_notification/generator_spec.rb b/spec/services/broadcasts/welcome_notification/generator_spec.rb index cb524dcc1..6439ad702 100644 --- a/spec/services/broadcasts/welcome_notification/generator_spec.rb +++ b/spec/services/broadcasts/welcome_notification/generator_spec.rb @@ -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