Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linking to invariable image URLs #10342

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions decidim-core/app/uploaders/decidim/application_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def variant(key)
else
model.send(mounted_as)
end
rescue ActiveStorage::InvariableError
model.send(mounted_as)
end

def attached?
Expand Down
14 changes: 14 additions & 0 deletions decidim-core/spec/models/decidim/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions decidim-core/spec/uploaders/application_uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down