From 8e6df7f5d93dc0b362d7474bb7cc4ff4189acd3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 27 Jul 2021 22:51:28 +0200 Subject: [PATCH] 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. --- app/helpers/application_helper.rb | 8 +++++++- app/models/site_customization/image.rb | 13 ++++++++++--- .../site_customization/images/index.html.erb | 4 ++-- .../files/apple-touch-icon-custom-200.png | Bin 0 -> 3463 bytes .../admin/site_customization/images_spec.rb | 11 +++++++++++ 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/files/apple-touch-icon-custom-200.png diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e57d06576..0b9fb23f4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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) diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index 592ea3b47..bb6859c2a 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -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 diff --git a/app/views/admin/site_customization/images/index.html.erb b/app/views/admin/site_customization/images/index.html.erb index f6ce49975..43da53e9e 100644 --- a/app/views/admin/site_customization/images/index.html.erb +++ b/app/views/admin/site_customization/images/index.html.erb @@ -16,12 +16,12 @@ <%= form_for([:admin, image], html: { id: "edit_#{dom_id(image)}" }) do |f| %>
- <%= image_tag image.image.url if image.image.exists? %> + <%= image_tag image.storage_image if image.persisted_attachment? %> <%= f.file_field :image, label: false %>
<%= 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? %>
<% end %> diff --git a/spec/fixtures/files/apple-touch-icon-custom-200.png b/spec/fixtures/files/apple-touch-icon-custom-200.png new file mode 100644 index 0000000000000000000000000000000000000000..61216ad39b043a5fb91c67bfeb8926438802c393 GIT binary patch literal 3463 zcmcgvc{r478<%#H;%lQAV@ZPcLuCk)ZDNcy%M4A-k{Nq(q$7%PNG4m(5l(jV zAv={VTcON^Y{{|HGRQK{H#*h%{`mU-JnwzI@AKTxb^m_%{oK#*damn@vNS&@Atoax zA|fJTVvNBF$7h@GE>YnZh(fjsha*fw0`mgRgX!<;LlQYdq`8p*CKOjs5{~3b47}1x zIwc~qRh4W_U=qyEqugl}uu0}z8>II07^(eAu(M60Tgd40~LS~(+ds!t0;n*CBT5@Ljvf4;TrBxt&;!*5)9Qro`h+u17Mm^I7AZ(fof}LLQ$GX zlqMAL^#KaA`4Bx&IE>NPTtW{Gyu@VEQ4ol~zdzVt3rzFzgg}u=vwm_kOr=>E!wn^A9NPsIH0tIVs zZs}W~nc4pjrBJ>{GnhEi-}U}aVup1fodm&=7&KoWcj3W#C~l^rqYQjVu1uPbHI3%| zt&5hIXiOU85{(WpFxc!G0-$Q<>Q1I^E}r;;GBZP&P#H{DsyoR9g9ZwDz+^HJ1vk{u zg2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