From 8e4b6802829352ffcee0984cb7cca8ff10f68938 Mon Sep 17 00:00:00 2001 From: David Genord II Date: Fri, 16 Feb 2024 14:58:25 -0500 Subject: [PATCH] Fix current url_options without a port (default port) --- lib/active_storage/service/db_service_rails70.rb | 3 ++- .../active_storage/service/db_service_spec.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/active_storage/service/db_service_rails70.rb b/lib/active_storage/service/db_service_rails70.rb index d99edc5..3e86b03 100644 --- a/lib/active_storage/service/db_service_rails70.rb +++ b/lib/active_storage/service/db_service_rails70.rb @@ -20,7 +20,8 @@ def compose(source_keys, destination_key, **) def current_host opts = url_options || {} url = "#{opts[:protocol]}#{opts[:host]}" - url + ":#{opts[:port]}" if opts[:port] + url += ":#{opts[:port]}" if opts[:port] + url end def private_url(key, expires_in:, filename:, content_type:, disposition:, **) diff --git a/spec/service/active_storage/service/db_service_spec.rb b/spec/service/active_storage/service/db_service_spec.rb index 86fe38d..d9efc4b 100644 --- a/spec/service/active_storage/service/db_service_spec.rb +++ b/spec/service/active_storage/service/db_service_spec.rb @@ -46,6 +46,22 @@ url = service.url('some_key', filename: filename, disposition: :inline, content_type: 'text/plain', expires_in: nil) expect(url).to match %r{#{host}/active_storage_db/files/} end + + context 'without port' do + let(:url_options) do + { + protocol: 'https://', + host: 'test.example.com', + } + end + + it 'returns a public URL' do + filename = ActiveStorage::Filename.new('some_filename') + service = ActiveStorage::Service.configure(:db, config) + url = service.url('some_key', filename: filename, disposition: :inline, content_type: 'text/plain', expires_in: nil) + expect(url).to match %r{https://test.example.com/active_storage_db/files/} + end + end end if Rails::VERSION::MAJOR >= 7