From 27f27caff323d82668949544b72ccf80d1d2725d Mon Sep 17 00:00:00 2001 From: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com> Date: Mon, 1 Mar 2021 10:24:57 -0700 Subject: [PATCH] Part 1: Shows Relevant #help Posts in Homepage Sidebar (#12670) [deploy] * Replace #active_help scope with #active_help class method * WIP: Add tests around #active_help class method * Refactor #active_help and update article_spec.rb tests to pass * Replaces .active_help class method with .active_help scope - Reverts change from scope to class method, back to scope - Updates to use newer AR query syntax using endless ranges - Refactors scope to be more concise --- app/models/article.rb | 7 +++---- spec/models/article_spec.rb | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index ec57ef73b..745dee7a1 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -133,10 +133,9 @@ class Article < ApplicationRecord 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) + stories = published.cached_tagged_with("help").order(created_at: :desc) + + stories.where(published_at: 12.hours.ago.., comments_count: ..5, score: -3..).presence || stories } scope :limited_column_select, lambda { diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index ec37651f8..555f6972a 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -681,6 +681,22 @@ RSpec.describe Article, type: :model do end end + describe ".active_help" do + it "returns properly filtered articles under the 'help' tag" do + filtered_article = create(:article, user: user, tags: "help", + published_at: 13.hours.ago, comments_count: 5, score: -3) + articles = described_class.active_help + expect(articles).to include(filtered_article) + end + + it "returns any published articles tagged with 'help' when there are no articles that fit the criteria" do + unfiltered_article = create(:article, user: user, tags: "help", + published_at: 10.hours.ago, comments_count: 8, score: -5) + articles = described_class.active_help + expect(articles).to include(unfiltered_article) + end + end + describe ".seo_boostable" do let!(:top_article) do create(:article, organic_page_views_past_month_count: 20, score: 30, tags: "good, greatalicious", user: user)