skip n+1 on linkable images in content#edit

This commit is contained in:
drusepth 2021-11-11 00:19:36 -08:00
parent 6bab0f705b
commit 8cd54092ce
2 changed files with 10 additions and 1 deletions

View File

@ -152,6 +152,10 @@ class ContentController < ApplicationController
return redirect_to @content, notice: t(:no_do_permission)
end
@random_image_including_private_pool_cache = ImageUpload.where(
user_id: current_user.id,
).group_by { |image| [image.content_type, image.content_id] }
respond_to do |format|
format.html { render 'content/edit', locals: { content: @content } }
format.json { render json: @content }

View File

@ -30,8 +30,13 @@
)
%>
<% @linkables_raw.fetch(page_type.name, []).each do |linkable| %>
<%
linkable_image = @random_image_including_private_pool_cache.fetch([linkable.page_type, linkable.id], [])
.sample
.try(:src, :thumb) || asset_path("card-headers/#{linkable.page_type.downcase.pluralize}.jpg")
%>
<option value="<%= page_type %>-<%= linkable.id %>"
data-icon="<%= asset_path linkable.random_image_including_private(format: :thumb) %>"
data-icon="<%= asset_path linkable_image %>"
<%= 'selected' if field[:value].include?("#{page_type}-#{linkable.id}") %>
>
<%= linkable.name %>