Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/assets/images/placeholder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
# frozen_string_literal: true

module ApplicationHelper
# Images
#
# Fallback image shown when a record-keyed asset (e.g. store/tool images
# named after the record) is missing.
PLACEHOLDER_IMAGE = 'placeholder.png'

# Render an image_tag that falls back to PLACEHOLDER_IMAGE when the asset is
# missing, instead of raising Propshaft::MissingAssetError and 500-ing the
# whole page. Falls back to nothing if even the placeholder is absent.
def safe_image_tag(source, options = {})
image_tag(source, options)
rescue Propshaft::MissingAssetError
begin
image_tag(PLACEHOLDER_IMAGE, options)
rescue Propshaft::MissingAssetError
nil
end
end

# asset_path that falls back to the placeholder (then nil) instead of raising,
# so it is safe to build data attributes (e.g. image_url) for the same source.
def safe_asset_path(source)
asset_path(source)
rescue Propshaft::MissingAssetError
begin
asset_path(PLACEHOLDER_IMAGE)
rescue Propshaft::MissingAssetError
nil
end
end

# Flash
#
# Map flash categories to css classes.
Expand Down
4 changes: 2 additions & 2 deletions app/views/store/items/_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
</style>

<div class="item-card">
<%= image_tag "store_images/#{item.name}.png",
style: 'height: 80px; object-fit: contain;' %>
<%= safe_image_tag "store_images/#{item.name}.png",
style: 'height: 80px; object-fit: contain;' %>
<div class="text"><%= link_to item.name, item %></div>
<div class="text"><%= number_to_currency item.price %></div>
<%= link_to t('.add_to_cart', default: t('helpers.links.add_to_cart')),
Expand Down
9 changes: 3 additions & 6 deletions app/views/store/items/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

<div class="content">
<h1><%= @store_item.name %></h1>
<% begin %>
<dt>
<%= image_tag("store_images/#{@store_item.name}.png", id: "item-image-#{@store_item.name}", class: 'item-image', data: { item_name: @store_item.name, image_url: asset_path("store_images/#{@store_item.name}.png") }, size: '70x70') %>
</dt>
<% rescue %>
<% end %>
<dt>
<%= safe_image_tag("store_images/#{@store_item.name}.png", id: "item-image-#{@store_item.name}", class: 'item-image', data: { item_name: @store_item.name, image_url: safe_asset_path("store_images/#{@store_item.name}.png") }, size: '70x70') %>
</dt>
<br/>
<dt><strong>Price:</strong></dt>
<dd><%= number_to_currency @store_item.price %></dd>
Expand Down
9 changes: 3 additions & 6 deletions app/views/tool_types/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

<div class="content">
<h1><%= @tool_type.name %></h1>
<% begin %>
<dt>
<%= image_tag("tool_images/#{@tool_type.name}.png", id: "item-image-#{@tool_type.name}", class: 'item-image', data: { item_name: @tool_type.name, image_url: asset_path("tool_images/#{@tool_type.name}.png") }, size: '70x70') %>
</dt>
<% rescue %>
<% end %>
<dt>
<%= safe_image_tag("tool_images/#{@tool_type.name}.png", id: "item-image-#{@tool_type.name}", class: 'item-image', data: { item_name: @tool_type.name, image_url: safe_asset_path("tool_images/#{@tool_type.name}.png") }, size: '70x70') %>
</dt>
<br/>
<% if can?(:update, @tool_type) %>
<%= link_to t('.edit', default: t('helpers.links.edit')),
Expand Down
Binary file removed db/binder_development.sqlite3-shm
Binary file not shown.
Empty file removed db/binder_development.sqlite3-wal
Empty file.
Loading