Factoring code to define Article.approved (#15600)

As I'm working on #15359 I wanted to favor using scopes over the raw
`where(approved: true)`.
This commit is contained in:
Jeremy Friesen 2021-12-01 22:45:32 -05:00 committed by GitHub
parent abec2e58e9
commit 5ee8764749
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 5 deletions

View file

@ -15,7 +15,7 @@ class SidebarsController < ApplicationController
.order(hotness_score: :desc)
requires_approval = Campaign.current.articles_require_approval?
campaign_articles_scope = campaign_articles_scope.where(approved: true) if requires_approval
campaign_articles_scope = campaign_articles_scope.approved if requires_approval
@campaign_articles_count = campaign_articles_scope.count
@latest_campaign_articles = campaign_articles_scope.limit(5).pluck(:path, :title, :comments_count, :created_at)

View file

@ -32,7 +32,7 @@ module Stories
def set_number_of_articles
@num_published_articles = if @tag_model.requires_approval?
@tag_model.articles.published.where(approved: true).count
@tag_model.articles.published.approved.count
elsif Settings::UserExperience.feed_strategy == "basic"
tagged_count
else
@ -47,7 +47,7 @@ module Stories
def set_stories
@stories = Articles::Feeds::Tag.call(@tag, number_of_articles: @number_of_articles, page: @page)
@stories = @stories.where(approved: true) if @tag_model&.requires_approval
@stories = @stories.approved if @tag_model&.requires_approval
@stories = stories_by_timeframe
@stories = @stories.decorate

View file

@ -61,7 +61,7 @@ class StoriesController < ApplicationController
.order(hotness_score: :desc)
requires_approval = Campaign.current.articles_require_approval?
campaign_articles_scope = campaign_articles_scope.where(approved: true) if requires_approval
campaign_articles_scope = campaign_articles_scope.approved if requires_approval
@campaign_articles_count = campaign_articles_scope.count
@latest_campaign_articles = campaign_articles_scope.limit(5).pluck(:path, :title, :comments_count, :created_at)

View file

@ -187,6 +187,11 @@ class Article < ApplicationRecord
}
scope :unpublished, -> { where(published: false) }
# [@jeremyf] For approved articles is there always an assumption of
# published? Regardless, the scope helps us deal with
# that in the future.
scope :approved, -> { where(approved: true) }
scope :admin_published_with, lambda { |tag_name|
published
.where(user_id: User.with_role(:super_admin)

View file

@ -67,7 +67,7 @@ class ArticleApiIndexService
articles = published_articles_with_users_and_organizations.cached_tagged_with(tag)
articles = if Tag.find_by(name: tag)&.requires_approval
articles.where(approved: true).order(featured_number: :desc)
articles.approved.order(featured_number: :desc)
elsif top.present?
articles.where("published_at > ?", top.to_i.days.ago)
.order(public_reactions_count: :desc)