Skip to content

Commit 74aa62c

Browse files
Merge pull request rails#31854 from huacnlee/allow-more-options-for-service-url
Allow ActiveStorage::Blob#service_url to pass addition options to service.url
2 parents 8f2bb58 + 69ae9fe commit 74aa62c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

activestorage/app/models/active_storage/blob.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ def text?
109109
# with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL.
110110
# Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And
111111
# it allows permanent URLs that redirect to the +service_url+ to be cached in the view.
112-
def service_url(expires_in: service.url_expires_in, disposition: :inline, filename: self.filename)
113-
service.url key, expires_in: expires_in, disposition: forcibly_serve_as_binary? ? :attachment : disposition, filename: filename, content_type: content_type
112+
def service_url(expires_in: service.url_expires_in, disposition: :inline, filename: self.filename, **options)
113+
service.url key, expires_in: expires_in, filename: filename, content_type: content_type,
114+
disposition: forcibly_serve_as_binary? ? :attachment : disposition, **options
114115
end
115116

116117
# Returns a URL that can be used to directly upload a file for this blob on the service. This URL is intended to be

activestorage/test/models/blob_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
require "test_helper"
44
require "database/setup"
5+
require "active_support/testing/method_call_assertions"
56

67
class ActiveStorage::BlobTest < ActiveSupport::TestCase
8+
include ActiveSupport::Testing::MethodCallAssertions
9+
710
test "create after upload sets byte size and checksum" do
811
data = "Hello world!"
912
blob = create_blob data: data
@@ -82,6 +85,23 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
8285
end
8386
end
8487

88+
test "urls allow for custom options" do
89+
blob = create_blob(filename: "original.txt")
90+
91+
options = [
92+
blob.key,
93+
expires_in: blob.service.url_expires_in,
94+
disposition: :inline,
95+
content_type: blob.content_type,
96+
filename: blob.filename,
97+
thumb_size: "300x300",
98+
thumb_mode: "crop"
99+
]
100+
assert_called_with(blob.service, :url, options) do
101+
blob.service_url(thumb_size: "300x300", thumb_mode: "crop")
102+
end
103+
end
104+
85105
test "purge deletes file from external service" do
86106
blob = create_blob
87107

0 commit comments

Comments
 (0)