From fd813ff0eb66acdb56bddd0e43ded37ed0379c4c Mon Sep 17 00:00:00 2001 From: Chris Gunther Date: Thu, 23 Oct 2025 08:56:49 -0400 Subject: [PATCH] Fix enabling `use_s3_urls` or `use_gcs_urls` without using S3 or GCS service If you enable using the service URLs globally, but are in an environment that doesn't use the corresponding service (ie. development), the service class is never required, leading to an uninitialized constant error checking whether the service for the image matches. The alternative would be to only enable using the service URLs in environments that also use the corresponding service, but that feels duplicative. --- CHANGELOG.md | 1 + lib/imgproxy/url_adapters/active_storage.rb | 4 ++++ spec/url_adapters/active_storage_spec.rb | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb914fb..ce96222 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Fixed - Fix `calc_hashsums` info option name. +- Fix enabling `use_s3_urls` or `use_gcs_urls` without using S3 or GCS service ## [3.0.0] - 2023-12-28 diff --git a/lib/imgproxy/url_adapters/active_storage.rb b/lib/imgproxy/url_adapters/active_storage.rb index ffc2724..b6ff53b 100644 --- a/lib/imgproxy/url_adapters/active_storage.rb +++ b/lib/imgproxy/url_adapters/active_storage.rb @@ -30,6 +30,8 @@ def s3_url(image) end def use_s3_url(image) + return unless defined?(::ActiveStorage::Service::S3Service) + config.use_s3_urls && service(image).is_a?(::ActiveStorage::Service::S3Service) end @@ -38,6 +40,8 @@ def gcs_url(image) end def use_gcs_url(image) + return unless defined?(::ActiveStorage::Service::GCSService) + config.use_gcs_urls && service(image).is_a?(::ActiveStorage::Service::GCSService) end diff --git a/spec/url_adapters/active_storage_spec.rb b/spec/url_adapters/active_storage_spec.rb index c06a371..c9620cb 100644 --- a/spec/url_adapters/active_storage_spec.rb +++ b/spec/url_adapters/active_storage_spec.rb @@ -93,6 +93,15 @@ end end + context 'with non-S3 service' do + let(:active_storage_service_name) { :local } + + it 'builds http URL' do + expect(Imgproxy.url_for(user.avatar)).to end_with \ + "/plain/#{Rails.application.routes.url_helpers.url_for(user.avatar)}" + end + end + describe "extension" do it "builds URL with ActiveStorage extension" do expect(user.avatar.imgproxy_url(width: 200)).to eq( @@ -134,6 +143,15 @@ end end + context 'with non-GCS service' do + let(:active_storage_service_name) { :local } + + it 'builds http URL' do + expect(Imgproxy.url_for(user.avatar)).to end_with \ + "/plain/#{Rails.application.routes.url_helpers.url_for(user.avatar)}" + end + end + describe "extension" do it "build URL with ActiveStorage extension" do expect(user.avatar.imgproxy_url(width: 200)).to eq(