cache linkable image lookups

This commit is contained in:
drusepth 2021-09-30 16:44:16 -07:00
parent c0c559f1c4
commit 78bfb55d67
2 changed files with 10 additions and 1 deletions

View File

@ -17,7 +17,14 @@ module HasImageUploads
end
def random_image_including_private(format: :medium)
image_uploads.sample.try(:src, format).presence || "card-headers/#{self.class.name.downcase.pluralize}.jpg"
@cache ||= {}
key = self.class.name + self.id.to_s
return @cache[key] if @cache.key?(key)
result = image_uploads.sample.try(:src, format).presence || "card-headers/#{self.class.name.downcase.pluralize}.jpg"
@cache[key] = result
result
end
def first_public_image(format: :medium)

View File

@ -184,8 +184,10 @@
<% end %>
<div class="grey-text uppercase center">Create something new</div>
<% shown_creatures = false %>
<% @activated_content_types.sample(3).each do |type| %>
<% klass = content_class_from_name(type) %>
<% shown_creatures = true if type == 'Creature' %>
<div>
<%= link_to send("new_#{type.downcase}_path"), class: "white-text", style: 'width: 100%' do %>
<div class="hoverable card-panel <%= klass.color %>" style="margin-bottom: 4px">