diff --git a/app/models/article.rb b/app/models/article.rb index 630b0d142..cd346525c 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/views/articles/_sidebar_additional.html.erb b/app/views/articles/_sidebar_additional.html.erb index 85d82b494..235bd3763 100644 --- a/app/views/articles/_sidebar_additional.html.erb +++ b/app/views/articles/_sidebar_additional.html.erb @@ -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? %>
@@ -76,6 +76,21 @@
<% end %> + <% recent_preamble_optimized_posts = Article.search_optimized(nil) %> + <% if params[:timeframe].blank? && recent_preamble_optimized_posts.present? %> +
+
+

recently queried

+
+
+ +
+
+ <% end %> <% end %> <% end %> diff --git a/app/views/articles/tags/_sidebar_additional.html.erb b/app/views/articles/tags/_sidebar_additional.html.erb index 30406aec8..0e91b823b 100644 --- a/app/views/articles/tags/_sidebar_additional.html.erb +++ b/app/views/articles/tags/_sidebar_additional.html.erb @@ -22,7 +22,7 @@
">
<% 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? %>
@@ -38,6 +38,21 @@
<% end %> + <% recent_preamble_optimized_posts = Article.search_optimized(@tag) %> + <% if params[:timeframe].blank? && recent_preamble_optimized_posts.present? %> +
+
+

recently queried

+
+
+ +
+
+ <% end %> <% end %> <% end %> diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 085ea824a..73e3aaf74 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -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}") diff --git a/spec/system/user/view_user_index_spec.rb b/spec/system/user/view_user_index_spec.rb index 7493f57d6..6acc83a34 100644 --- a/spec/system/user/view_user_index_spec.rb +++ b/spec/system/user/view_user_index_spec.rb @@ -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) }