Skip to content

Commit

Permalink
Fix fetch url generation with Active Storage folder configuration
Browse files Browse the repository at this point in the history
Fixes #537
  • Loading branch information
const-cloudinary committed May 5, 2024
1 parent 7ff50b8 commit 47badb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/active_storage/service/cloudinary_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module CloudinaryHelper
alias cloudinary_url_internal_original cloudinary_url_internal

def cloudinary_url_internal(source, options = {})
source = ActiveStorage::Blob.service.public_id(source) if defined? ActiveStorage::Blob.service.public_id
if defined?(ActiveStorage::Blob.service.public_id) && options.fetch(:type, "").to_s != "fetch"
source = ActiveStorage::Blob.service.public_id(source)
end
cloudinary_url_internal_original(source, options)
end
end
Expand Down Expand Up @@ -58,7 +60,7 @@ def upload(key, io, filename: nil, checksum: nil, **options)
def url(key, filename: nil, content_type: '', **options)
instrument :url, key: key do |payload|
url = Cloudinary::Utils.cloudinary_url(
full_public_id_internal(key),
full_public_id_internal(key, options),
resource_type: resource_type(nil, key, content_type),
format: ext_for_file(key, filename, content_type),
**@options.merge(options.symbolize_keys)
Expand Down Expand Up @@ -231,10 +233,12 @@ def ext_for_file(key, filename = nil, content_type = nil)
end

# Returns the full public id including folder.
def full_public_id_internal(key)
def full_public_id_internal(key, options = {})
public_id = public_id_internal(key)

return public_id unless @options[:folder]
options = @options.merge(options)

return public_id if !options[:folder] || options.fetch(:type).to_s == "fetch"

File.join(@options.fetch(:folder), public_id)
end
Expand Down
7 changes: 7 additions & 0 deletions spec/active_storage/service/cloudinary_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@
expect(url).to include(folder)
end

it "should omit global folder configuration for fetch urls" do
folder = SERVICE_CONFIGURATIONS[:cloudinary][:folder]
expect(folder).not_to be_empty, "Please set a folder value under cloudinary in #{CONFIGURATION_PATH}"
url = @service.url(TEST_IMAGE_URL, type: "fetch")
expect(url).not_to include(folder)
end

it "should accept options that override global configuration" do
tags = SERVICE_CONFIGURATIONS[:cloudinary][:tags]
expect(tags).not_to be_empty, "Please set a tags value under cloudinary in #{CONFIGURATION_PATH}"
Expand Down

0 comments on commit 47badb5

Please sign in to comment.