diff --git a/decidim-core/app/uploaders/decidim/application_uploader.rb b/decidim-core/app/uploaders/decidim/application_uploader.rb index 682c33857b8f..522f7ca068b6 100644 --- a/decidim-core/app/uploaders/decidim/application_uploader.rb +++ b/decidim-core/app/uploaders/decidim/application_uploader.rb @@ -30,6 +30,8 @@ def variant(key) else model.send(mounted_as) end + rescue ActiveStorage::InvariableError + model.send(mounted_as) end def attached? diff --git a/decidim-core/spec/models/decidim/attachment_spec.rb b/decidim-core/spec/models/decidim/attachment_spec.rb index 9f2202398f8b..d331fc8a4d59 100644 --- a/decidim-core/spec/models/decidim/attachment_spec.rb +++ b/decidim-core/spec/models/decidim/attachment_spec.rb @@ -65,6 +65,20 @@ module Decidim expect(subject.big_url).not_to be_nil end + context "when the image is an invariable format" do + before do + allow(ActiveStorage).to receive(:variable_content_types).and_return(%w(image/bmp)) + end + + it "has a thumbnail" do + expect(subject.thumbnail_url).not_to be_nil + end + + it "has a big version" do + expect(subject.big_url).not_to be_nil + end + end + describe "photo?" do it "returns true" do expect(subject.photo?).to be(true) diff --git a/decidim-core/spec/uploaders/application_uploader_spec.rb b/decidim-core/spec/uploaders/application_uploader_spec.rb index 64033d882ae4..73ffe3c00622 100644 --- a/decidim-core/spec/uploaders/application_uploader_spec.rb +++ b/decidim-core/spec/uploaders/application_uploader_spec.rb @@ -15,6 +15,28 @@ module Decidim allow(ENV).to receive(:fetch).with("HOSTNAME", nil).and_return(hostname) if respond_to?(:hostname) end + describe "#variant" do + subject { test_class.new(model, mounted_as) } + + let(:test_class) do + Class.new(described_class) do + set_variants do + { testing: { resize_to_fit: [200, 100] } } + end + end + end + + context "when the provided file is invariable" do + before do + allow(ActiveStorage).to receive(:variable_content_types).and_return(%w(image/bmp)) + end + + it "returns the non-variant" do + expect(subject.variant(:testing)).to be(model.official_img_header) + end + end + end + describe "#variant_url" do shared_context "with force_ssl enabled" do before do @@ -67,6 +89,16 @@ module Decidim it "returns a URL to the variant" do expect(subject.variant_url(:testing)).to match(%r{^http://localhost:#{default_port}/rails/active_storage/representations/redirect/.*/avatar.jpg$}) end + + context "when the provided file is invariable" do + before do + allow(ActiveStorage).to receive(:variable_content_types).and_return(%w(image/bmp)) + end + + it "returns the original URL" do + expect(subject.variant_url(:testing)).to match(%r{^http://localhost:#{default_port}/rails/active_storage/blobs/redirect/.*/avatar.jpg$}) + end + end end context "with a variant that has a different format" do