Skip to content

Commit 6bab0f7

Browse files
committed
cache image uploads to avoid n+1 query on content#index pages
1 parent 84ab204 commit 6bab0f7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

app/controllers/content_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def index
5555
@questioned_content = @content.sample
5656
@attribute_field_to_question = SerendipitousService.question_for(@questioned_content)
5757

58+
@random_image_including_private_pool_cache = ImageUpload.where(
59+
content_type: @content_type_class.name,
60+
content_id: @content.pluck(:id)
61+
).group_by { |image| [image.content_type, image.content_id] }
62+
5863
# Uh, do we ever actually make JSON requests to logged-in user pages?
5964
respond_to do |format|
6065
format.html { render 'content/index' }

app/views/content/list/_cards.html.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<div class="js-content-cards-list">
22
<% content_list.each.with_index do |content, i| %>
3+
<%
4+
content_image = @random_image_including_private_pool_cache[[content.page_type, content.id]]
5+
.sample
6+
.try(:src, :medium) || asset_path("card-headers/#{content.page_type.downcase.pluralize}.jpg")
7+
%>
38
<div class="col s12 m6 l4 js-content-card-container">
49
<div class="hoverable card sticky-action" style="margin-bottom: 2px">
510
<div class="card-image waves-effect waves-block waves-light">
611
<%= render partial: 'content/display/favorite_control', locals: { content: content } %>
7-
<div class="activator" style="height: 265px; background: url('<%= content.random_image_including_private(format: :medium) %>'); background-size: cover;"></div>
12+
<div class="activator" style="height: 265px; background: url('<%= content_image %>'); background-size: cover;"></div>
813

914
<span class="card-title js-content-name activator">
1015
<div class="bordered-text">

0 commit comments

Comments
 (0)