From 004c7eef9eebfae97ce735f1cb382176e6724a66 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 14 Jun 2022 15:07:46 +1000 Subject: [PATCH] Show SVG product images --- app/models/spree/image.rb | 8 ++++++-- config/application.rb | 1 + spec/models/spree/image_spec.rb | 10 +++++++++- spec/views/spree/orders/show.html.haml_spec.rb | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index da48229f80..761fc62807 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -15,11 +15,15 @@ module Spree validate :no_attachment_errors def variant(name) - attachment.variant(SIZES[name]) + if attachment.variable? + attachment.variant(SIZES[name]) + else + attachment + end end def url(size) - return unless attachment.variable? + return unless attachment.attached? Rails.application.routes.url_helpers.url_for(variant(size)) end diff --git a/config/application.rb b/config/application.rb index 7d88b45db8..940fc797de 100644 --- a/config/application.rb +++ b/config/application.rb @@ -240,5 +240,6 @@ module Openfoodnetwork Rails.autoloaders.main.ignore(Rails.root.join('app/webpacker')) config.active_storage.service = ENV["S3_BUCKET"].present? ? :amazon : :local + config.active_storage.content_types_to_serve_as_binary -= ["image/svg+xml"] end end diff --git a/spec/models/spree/image_spec.rb b/spec/models/spree/image_spec.rb index 52debc535a..40481cf1ea 100644 --- a/spec/models/spree/image_spec.rb +++ b/spec/models/spree/image_spec.rb @@ -21,11 +21,19 @@ module Spree ) end - it "returns nil for unsupported formats" do + it "returns download link for unsupported formats" do subject.attachment_blob.update_columns( content_type: "application/octet-stream" ) + expect(subject.url(:small)).to match( + %r|^http://test\.host/rails/active_storage/blobs/redirect/.+/logo-black\.png$| + ) + end + + it "returns nil when the attachment is missing" do + subject.attachment = nil + expect(subject.url(:small)).to eq nil end end diff --git a/spec/views/spree/orders/show.html.haml_spec.rb b/spec/views/spree/orders/show.html.haml_spec.rb index 1b9a2882cf..6980b58caf 100644 --- a/spec/views/spree/orders/show.html.haml_spec.rb +++ b/spec/views/spree/orders/show.html.haml_spec.rb @@ -51,7 +51,7 @@ describe "spree/orders/show.html.haml" do render - expect(rendered).to have_no_css("img[src*='logo.png']") + expect(rendered).to have_css("img[src*='logo.png']") expect(rendered).to have_content("R123456789") end end