diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 26dd8d58c..ee57c5b39 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -10,11 +10,7 @@ class ArticlesController < ApplicationController def feed skip_authorization - @articles = Article.published. - select(:published_at, :processed_html, :user_id, :organization_id, :title, :path). - order(published_at: :desc). - page(params[:page].to_i).per(12) - + @articles = Article.feed.order(published_at: :desc).page(params[:page].to_i).per(12) @articles = if params[:username] handle_user_or_organization_feed elsif params[:tag] @@ -213,11 +209,9 @@ class ArticlesController < ApplicationController end def handle_tag_feed - tag = Tag.find_by(name: params[:tag].downcase) + @tag = Tag.aliased_name(params[:tag]) + return unless @tag - return unless tag - - @tag = tag.alias_for.presence || tag @articles = @articles.cached_tagged_with(@tag) end diff --git a/app/models/article.rb b/app/models/article.rb index 30b6ad334..d07935c9e 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -137,6 +137,8 @@ class Article < ApplicationRecord order(column => dir.to_sym) } + scope :feed, -> { published.select(:id, :published_at, :processed_html, :user_id, :organization_id, :title, :path) } + algoliasearch per_environment: true, auto_remove: false, enqueue: :trigger_index do attribute :title add_index "searchables", id: :index_id, per_environment: true, enqueue: :trigger_index do diff --git a/app/models/tag.rb b/app/models/tag.rb index e67445ae4..88f0d0993 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -61,6 +61,13 @@ class Tag < ActsAsTaggableOn::Tag ALLOWED_CATEGORIES end + def self.aliased_name(word) + tag = find_by(name: word.downcase) + return unless tag + + tag.alias_for.presence || tag.name + end + private def evaluate_markdown diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 96f2f8fba..2bd956ff9 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -611,5 +611,15 @@ RSpec.describe Article, type: :model do end end + describe ".feed" do + it "returns records with a subset of attributes" do + create(:article, published: true, published_at: 2.hours.ago) + + feed_article = described_class.feed.first + + expect(feed_article.attributes.keys).to match_array(%w[id tag_list published_at processed_html user_id organization_id title path]) + end + end + include_examples "#sync_reactions_count", :article end