ArticlesController#feed refactor (#4498)

* Move query details into model.
Reorganize filtering initial results by params.

* Fixups from feedback.
This commit is contained in:
Brad Pauly 2019-10-30 12:35:53 -04:00 committed by Ben Halpern
parent b162e8b848
commit b59aba415d
4 changed files with 22 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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