[deploy] Update FlareTag not to cache AR object (#8430)

This commit is contained in:
Mac Siri 2020-06-16 13:37:08 -04:00 committed by GitHub
parent 9ada1dda10
commit 715a6ff130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 18 deletions

View file

@ -19,11 +19,9 @@ class FlareTag
end
def tag
@tag ||= Rails.cache.fetch("article_flare_tag-#{article.id}-#{article.updated_at}", expires_in: 12.hours) do
# Take the first flare tag to show up in the array
flare = FLARE_TAGS.detect { |tag| article.cached_tag_list_array.include?(tag) }
flare && flare != except_tag ? Tag.select(%i[name bg_color_hex text_color_hex]).find_by(name: flare) : nil
end
@tag ||= if cached_tag_id
Tag.select(%i[name bg_color_hex text_color_hex]).find_by(id: cached_tag_id)
end
end
def tag_hash
@ -37,4 +35,13 @@ class FlareTag
private
attr_reader :article, :except_tag
def cached_tag_id
Rails.cache.fetch("article-#{article.id}_flare_tag_id-#{article.updated_at}", expires_in: 12.hours) do
flare = FLARE_TAGS.detect { |tag| article.cached_tag_list_array.include?(tag) }
if flare && flare != except_tag
Tag.find_by(name: flare).id
end
end
end
end

View file

@ -9,18 +9,9 @@ RSpec.describe "AsyncInfo", type: :request do
describe "GET /async_info/base_data" do
context "when not logged-in" do
before { get "/async_info/base_data" }
it "returns broadcast" do
expect(response.body).to include("broadcast")
end
it "returns token" do
expect(response.body).to include("token")
end
it "does not return user" do
expect(response.body).not_to include("user")
it "returns json without user" do
get "/async_info/base_data"
expect(response.parsed_body.keys).to match_array(%w[broadcast param token])
end
end
@ -28,8 +19,9 @@ RSpec.describe "AsyncInfo", type: :request do
it "returns token and user" do
allow(controller_instance).to receive(:remember_user_token).and_return(nil)
sign_in create(:user)
get "/async_info/base_data"
expect(response.body).to include("broadcast", "token", "user")
expect(response.parsed_body.keys).to match_array(%w[broadcast param token user])
end
end
end