Make less queries when displaying articles flare tags (#1957)

* Make less queries when displaying flare tag

* Make less sql queries to display articles flare tags
This commit is contained in:
Anna Buianova 2019-03-05 18:27:51 +03:00 committed by Ben Halpern
parent e491cf1f65
commit 0ac8eb1f73
5 changed files with 76 additions and 56 deletions

View file

@ -1,27 +1,25 @@
class FlareTag
attr_accessor :article
def initialize(article)
FLARES = %w[explainlikeimfive
ama
techtalks
help
news
healthydebate
showdev
challenge
anonymous
hiring
discuss].freeze
def initialize(article, except_tag = nil)
@article = article.decorate
@except_tag = except_tag
end
def tag
Rails.cache.
fetch("article_flare_tag-#{article.id}-#{article.updated_at}", expires_in: 12.hours) do
flares = ["explainlikeimfive",
"ama",
"techtalks",
"help",
"news",
"healthydebate",
"showdev",
"challenge",
"anonymous",
"hiring",
"discuss"]
flares.each do |f|
return Tag.find_by_name(f) if article.cached_tag_list_array.include?(f)
end
nil
@tag ||= Rails.cache.fetch("article_flare_tag-#{article.id}-#{article.updated_at}", expires_in: 12.hours) do
flare = FLARES.detect { |f| article.cached_tag_list_array.include?(f) }
flare && flare != except_tag ? Tag.find_by_name(flare) : nil
end
end
@ -32,4 +30,8 @@ class FlareTag
bg_color_hex: tag.bg_color_hex,
text_color_hex: tag.text_color_hex }
end
private
attr_reader :article, :except_tag
end

View file

@ -333,7 +333,7 @@ class Article < ApplicationRecord
end
def flare_tag
FlareTag.new(self).tag_hash
@flare_tag ||= FlareTag.new(self).tag_hash
end
def update_main_image_background_hex

View file

@ -1,5 +1,7 @@
<% if FlareTag.new(story).tag && @tag != FlareTag.new(story).tag.name %>
<span class="tag-identifier black-identifier" style="background:<%= FlareTag.new(story).tag.bg_color_hex %>;color:<%= FlareTag.new(story).tag.text_color_hex %>">#<%= FlareTag.new(story).tag.name %></span><%= story.title %>
<% else %>
<%= story.title %>
<% flare_tag = FlareTag.new(story, @tag).tag %>
<% if flare_tag %>
<span class="tag-identifier black-identifier" style="background:<%= flare_tag.bg_color_hex %>;color:<%= flare_tag.text_color_hex %>">
#<%= flare_tag.name %>
</span>
<% end %>
<%= story.title %>

View file

@ -0,0 +1,48 @@
require "rails_helper"
RSpec.describe FlareTag do
let(:user) { create(:user) }
let(:article) { create(:article, user_id: user.id) }
describe "#flare_tag" do
it "returns nil if there is no flare tag" do
expect(described_class.new(article).tag).to be nil
end
it "returns a flare tag if there is a flare tag in the list" do
valid_article = create(:article, tags: "ama")
expect(described_class.new(valid_article).tag.name).to eq("ama")
end
it "returns nil if an except is provided" do
valid_article = create(:article, tags: "explainlikeimfive")
expect(described_class.new(valid_article, "explainlikeimfive").tag).to eq(nil)
end
it "returns a flare tag if there are 2 flare tags in the list" do
valid_article = create(:article, tags: ["ama", "explainlikeimfive"])
expect(described_class.new(valid_article).tag.name).to eq("explainlikeimfive")
end
end
describe "#flare_tag_hash" do
let (:tag) { create(:tag, name: "ama", bg_color_hex: "#f3f3f3", text_color_hex: "#cccccc") }
let (:valid_article) { create(:article, tags: tag.name) }
it "returns nil if an article doesn't have a flare tag" do
expect(described_class.new(article).tag_hash).to be nil
end
it "returns a hash with the flare tag's name" do
expect(described_class.new(valid_article).tag_hash.value?("ama")).to be true
end
it "returns a hash with the flare tag's bg_color_hex" do
expect(described_class.new(valid_article).tag_hash.value?("#f3f3f3")).to be true
end
it "returns a hash with the flare tag's text_color_hex" do
expect(described_class.new(valid_article).tag_hash.value?("#cccccc")).to be true
end
end
end

View file

@ -370,38 +370,6 @@ RSpec.describe Article, type: :model do
end
end
describe "#flare_tag" do
it "returns nil if there is no flare tag" do
expect(FlareTag.new(article).tag).to be nil
end
it "returns a flare tag if there is a flare tag in the list" do
valid_article = create(:article, tags: "ama")
expect(FlareTag.new(valid_article).tag.name).to eq("ama")
end
end
describe "#flare_tag_hash" do
let (:tag) { create(:tag, name: "ama", bg_color_hex: "#f3f3f3", text_color_hex: "#cccccc") }
let (:valid_article) { create(:article, tags: tag.name) }
it "returns nil if an article doesn't have a flare tag" do
expect(FlareTag.new(article).tag_hash).to be nil
end
it "returns a hash with the flare tag's name" do
expect(FlareTag.new(valid_article).tag_hash.value?("ama")).to be true
end
it "returns a hash with the flare tag's bg_color_hex" do
expect(FlareTag.new(valid_article).tag_hash.value?("#f3f3f3")).to be true
end
it "returns a hash with the flare tag's text_color_hex" do
expect(FlareTag.new(valid_article).tag_hash.value?("#cccccc")).to be true
end
end
describe "before save" do
# before do
# article = create(:article, user_id: user.id)