Refactoring to consolidate logic (#15851)

* Refactoring to consolidate logic

Prior to this commit, two controllers had nearly identical chunks of
logic.  This refactor extracts the logic to a common and more canonical
location.

* Addressing rubocop's aggressive auto-fix

* Adding spat operator for pluck

* Renaming method for greater clarity
This commit is contained in:
Jeremy Friesen 2021-12-29 11:08:04 -05:00 committed by GitHub
parent ba590c1750
commit 973ff20eb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 18 deletions

View file

@ -10,14 +10,7 @@ class SidebarsController < ApplicationController
private
def get_latest_campaign_articles
campaign_articles_scope = Article.tagged_with(Campaign.current.featured_tags, any: true)
.where("published_at > ? AND score > ?", Settings::Campaign.articles_expiry_time.weeks.ago, 0)
.order(hotness_score: :desc)
requires_approval = Campaign.current.articles_require_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)
@campaign_articles_count = Campaign.current.count
@latest_campaign_articles = Campaign.current.plucked_article_attributes
end
end

View file

@ -56,15 +56,8 @@ class StoriesController < ApplicationController
end
def get_latest_campaign_articles
campaign_articles_scope = Article.tagged_with(Campaign.current.featured_tags, any: true)
.where("published_at > ? AND score > ?", Settings::Campaign.articles_expiry_time.weeks.ago, 0)
.order(hotness_score: :desc)
requires_approval = Campaign.current.articles_require_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)
@campaign_articles_count = Campaign.current.count
@latest_campaign_articles = Campaign.current.plucked_article_attributes
end
def redirect_to_changed_username_profile

View file

@ -10,6 +10,7 @@ class Campaign
end
METHODS = %w[
articles_expiry_time
articles_require_approval?
call_to_action
featured_tags
@ -24,4 +25,44 @@ class Campaign
def show_in_sidebar?
sidebar_enabled? && sidebar_image.present?
end
# @return [Integer] The total number of articles in the campaign.
delegate :count, to: :articles_scope
# Get the "plucked" attribute information for the campaign's
# articles.
#
# @param limit [Integer] The limit of the number of articles to
# fetch and render.
# @param attributes [Array<Symbol>] The named attributes to pluck
# from the Article result set.
#
# @return [Array<Array>] The inner array is the plucked attribute
# values for the selected articles. Which means be mindful
# of the order you pass for attributes.
#
# @note The order of attributes and behavior of this method is from
# past implementations. A refactor to consider would be to
# create a data structure.
#
# @see `./app/views/articles/_widget_list_item.html.erb` for the
# importance of maintaining position of these parameters.
def plucked_article_attributes(limit: 5, attributes: %i[path title comments_count created_at])
articles_scope.limit(limit).pluck(*attributes)
end
private
# @note [@jeremyf] My inclination was to extract a scoping method
# for this. However, I've since consolidated the logic into a
# single location, so the scope is less necessary.
def articles_scope
articles_scope = Article
.tagged_with(featured_tags, any: true)
.where("published_at > ? AND score > ?", articles_expiry_time.weeks.ago, 0)
.order(hotness_score: :desc)
articles_scope = articles_scope.approved if articles_require_approval?
articles_scope
end
end