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 000000000..61216ad39 Binary files /dev/null and b/spec/fixtures/files/apple-touch-icon-custom-200.png differ diff --git a/spec/system/admin/site_customization/images_spec.rb b/spec/system/admin/site_customization/images_spec.rb index 26f1b13c4..d79468563 100644 --- a/spec/system/admin/site_customization/images_spec.rb +++ b/spec/system/admin/site_customization/images_spec.rb @@ -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")