[deploy] Render recently updated search preambled to sidebar (#8060)

* Render recently updated search preambled to sidebar

* Refactor

* Fix typo

* Mess with test
This commit is contained in:
Ben Halpern 2020-05-26 18:34:27 -04:00 committed by GitHub
parent 1a1b5b544e
commit e39f5b04d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 4 deletions

View file

@ -202,7 +202,7 @@ class Article < ApplicationRecord
order(organic_page_views_past_month_count: :desc).
where("score > ?", 8).
where("published_at > ?", time_ago).
limit(25)
limit(20)
fields = %i[path title comments_count created_at]
if tag
@ -212,6 +212,20 @@ class Article < ApplicationRecord
end
end
def self.search_optimized(tag = nil)
relation = Article.published.
order(updated_at: :desc).
where.not(search_optimized_title_preamble: nil).
limit(20)
fields = %i[path search_optimized_title_preamble comments_count created_at]
if tag
relation.cached_tagged_with(tag).pluck(*fields)
else
relation.pluck(*fields)
end
end
def search_id
"article_#{id}"
end

View file

@ -60,7 +60,7 @@
<% end %>
<% end %>
<% unless user_signed_in? %>
<% cache("seo-boostable-posts-homepage-#{params[:timeframe]}-xoxo", expires_in: 18.hours) do %>
<% cache("seo-boostable-posts-homepage-#{params[:timeframe]}", expires_in: 18.hours) do %>
<% boostable_posts = Article.seo_boostable(nil, Timeframer.new(params[:timeframe]).datetime) %>
<% if boostable_posts.present? %>
<div class="widget">
@ -76,6 +76,21 @@
</div>
</div>
<% end %>
<% recent_preamble_optimized_posts = Article.search_optimized(nil) %>
<% if params[:timeframe].blank? && recent_preamble_optimized_posts.present? %>
<div class="widget">
<header>
<h4>recently queried</h4>
</header>
<div class="widget-body">
<div class="widget-link-list">
<% recent_preamble_optimized_posts.each do |plucked_article| %>
<%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: false %>
<% end %>
</div>
</div>
</div>
<% end %>
<% end %>
<% end %>
</div>

View file

@ -22,7 +22,7 @@
<div id="sidebarWidget__pack" data-tag-info="<%= @tag_model.attributes.slice("id", "text_color_hex", "bg_color_hex", "name").to_json %>">
</div>
<% else %>
<% cache("seo-boostable-posts-for-tag-#{@tag}-#{params[:timeframe]}-xoxo", expires_in: 18.hours) do %>
<% cache("seo-boostable-posts-for-tag-#{@tag}-#{params[:timeframe]}", expires_in: 18.hours) do %>
<% boostable_posts = Article.seo_boostable(@tag, Timeframer.new(params[:timeframe]).datetime) %>
<% if boostable_posts.present? %>
<div class="widget">
@ -38,6 +38,21 @@
</div>
</div>
<% end %>
<% recent_preamble_optimized_posts = Article.search_optimized(@tag) %>
<% if params[:timeframe].blank? && recent_preamble_optimized_posts.present? %>
<div class="widget">
<header>
<h4>recently queried</h4>
</header>
<div class="widget-body">
<div class="widget-link-list">
<% recent_preamble_optimized_posts.each do |plucked_article| %>
<%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: false %>
<% end %>
</div>
</div>
</div>
<% end %>
<% end %>
<% end %>
</div>

View file

@ -644,6 +644,41 @@ RSpec.describe Article, type: :model do
end
end
describe ".search_optimized_title_preamble" do
let!(:top_article) do
create(:article, search_optimized_title_preamble: "Hello #{rand(1000)}", tags: "good, greatalicious")
end
it "returns article with title preamble" do
articles = described_class.search_optimized
expect(articles.first[0]).to eq(top_article.path)
expect(articles.first[1]).to eq(top_article.search_optimized_title_preamble)
end
it "does not return article without preamble" do
articles = described_class.search_optimized
new_article = create(:article)
expect(articles.flatten).not_to include(new_article.path)
end
it "does return multiple articles with preamble ordered by updated_at" do
new_article = create(:article, search_optimized_title_preamble: "Testerino")
articles = described_class.search_optimized
expect(articles.first[1]).to eq(new_article.search_optimized_title_preamble)
expect(articles.second[1]).to eq(top_article.search_optimized_title_preamble)
end
it "returns articles ordered by organic_page_views_count by tag" do
articles = described_class.search_optimized("greatalicious")
expect(articles.first[0]).to eq(top_article.path)
end
it "returns nothing if no tagged articles" do
articles = described_class.search_optimized("godsdsdsdsgoo")
expect(articles).to be_empty
end
end
context "when callbacks are triggered before save" do
it "assigns path on save" do
expect(article.path).to eq("/#{article.username}/#{article.slug}")

View file

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe "User index", type: :system do
let!(:user) { create(:user, username: "user3000") }
let!(:article) { create(:article, user: user) }
let!(:other_article) { create(:article) }
let!(:other_article) { create(:article, title: rand(10000000).to_s) }
let!(:comment) { create(:comment, user: user, commentable: other_article) }
let(:organization) { create(:organization) }