Use Active Storage to render custom images

Just like we did with regular attachments, we're moving the logic to
generate URLs out of the model.

Note we're changing the `image_path_for` helper method in order to
return a `polymorphic_path` because sometimes it's used in combination
with `favicon_link_tag`, and `favicon_link_tag` doesn't automatically
generate a polymorphic URL when given an `ActiveStorage::Attachment`
record.
This commit is contained in:
Javi Martín
2021-07-27 22:51:28 +02:00
parent e0e35298d5
commit 8e6df7f5d9
5 changed files with 30 additions and 6 deletions

View File

@@ -49,7 +49,13 @@ module ApplicationHelper
end
def image_path_for(filename)
SiteCustomization::Image.image_path_for(filename) || filename
image = SiteCustomization::Image.image_for(filename)
if image
polymorphic_path(image)
else
filename
end
end
def content_block(name, locale = I18n.locale)

View File

@@ -23,11 +23,10 @@ class SiteCustomization::Image < ApplicationRecord
end
end
def self.image_path_for(filename)
def self.image_for(filename)
image_name = filename.split(".").first
imageable = find_by(name: image_name)
imageable.present? && imageable.image.exists? ? imageable.image.url : nil
find_by(name: image_name)&.persisted_image
end
def required_width
@@ -38,6 +37,14 @@ class SiteCustomization::Image < ApplicationRecord
VALID_IMAGES[name]&.second
end
def persisted_image
storage_image if persisted_attachment?
end
def persisted_attachment?
storage_image.attachment&.persisted?
end
private
def check_image

View File

@@ -16,12 +16,12 @@
<td class="small-12 medium-8">
<%= form_for([:admin, image], html: { id: "edit_#{dom_id(image)}" }) do |f| %>
<div class="small-12 medium-6 large-6 column">
<%= image_tag image.image.url if image.image.exists? %>
<%= image_tag image.storage_image if image.persisted_attachment? %>
<%= f.file_field :image, label: false %>
</div>
<div class="small-12 medium-6 large-6 column">
<%= f.submit(t("admin.site_customization.images.index.update"), class: "button hollow") %>
<%= link_to t("admin.site_customization.images.index.delete"), admin_site_customization_image_path(image), method: :delete, class: "button hollow alert" if image.image.exists? %>
<%= link_to t("admin.site_customization.images.index.delete"), admin_site_customization_image_path(image), method: :delete, class: "button hollow alert" if image.persisted_attachment? %>
</div>
<% end %>
</td>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -60,6 +60,17 @@ describe "Admin custom images", :admin do
end
end
scenario "Custom apple touch icon is replaced on front views" do
create(:site_customization_image,
name: "apple-touch-icon-200",
image: File.new("spec/fixtures/files/apple-touch-icon-custom-200.png"))
visit root_path
expect(page).not_to have_css("link[href*='apple-touch-icon-200']", visible: :all)
expect(page).to have_css("link[href*='apple-touch-icon-custom-200']", visible: :hidden)
end
scenario "Image is replaced on admin newsletters" do
newsletter = create(:newsletter, segment_recipient: "all_users")