diff --git a/app/controllers/sidebars_controller.rb b/app/controllers/sidebars_controller.rb index ad2755a74..8bd350316 100644 --- a/app/controllers/sidebars_controller.rb +++ b/app/controllers/sidebars_controller.rb @@ -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 diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 1e1ee4b12..07b9ea0b9 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -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 diff --git a/app/models/campaign.rb b/app/models/campaign.rb index d698c5ef1..b486eb21e 100644 --- a/app/models/campaign.rb +++ b/app/models/campaign.rb @@ -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] The named attributes to pluck + # from the Article result set. + # + # @return [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