[deploy] Update ReactionsController not to cache AR Object (#8393)

This commit is contained in:
Mac Siri 2020-06-11 14:22:53 -04:00 committed by GitHub
parent 8cb74b3e20
commit 7d58818efa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View file

@ -35,7 +35,7 @@ class ReactionsController < ApplicationController
reactions = if session_current_user_id
comment_ids = reaction_counts.map { |rc| rc[:id] }
cached_user_public_reactions(current_user).where(reactable_id: comment_ids)
Reaction.where(id: cached_user_public_reaction_ids(current_user), reactable_id: comment_ids)
else
Reaction.none
end
@ -102,14 +102,14 @@ class ReactionsController < ApplicationController
render json: { result: result, category: category }
end
def cached_user_public_reactions(user)
Rails.cache.fetch("cached-user-reactions-#{user.id}-#{user.public_reactions_count}", expires_in: 24.hours) do
user.reactions.public_category
private
def cached_user_public_reaction_ids(user)
Rails.cache.fetch("cached-user-#{user.id}-reaction-ids-#{user.public_reactions_count}", expires_in: 24.hours) do
user.reactions.public_category.pluck(:id)
end
end
private
def build_reaction(category)
create_params = {
user_id: current_user.id,

View file

@ -87,11 +87,11 @@ RSpec.describe "Reactions", type: :request do
end
it "returns the correct json response" do
result = response.parsed_body
expect(result["current_user"]).to eq("id" => user.id)
expect(result["public_reaction_counts"]).to eq([{ "id" => article.comments.last.id, "count" => 1 }])
expect(result["reactions"].to_json).to eq(user.reactions.where(reactable: comment).to_json)
expect(response.parsed_body).to eq(
"current_user" => { "id" => user.id },
"public_reaction_counts" => [{ "id" => article.comments.last.id, "count" => 1 }],
"reactions" => JSON.parse(user.reactions.where(reactable: comment).to_json),
)
end
it "does not set surrogate key headers" do