Skip to content

Commit a205f51

Browse files
authored
Respect default serving strategy in rails_{blob,representation}_{url,path}
1 parent 7a6ece3 commit a205f51

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

activestorage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* After setting `config.active_storage.resolve_model_to_route = :rails_storage_proxy`
2+
`rails_blob_path` and `rails_representation_path` will generate proxy URLs by default.
3+
4+
*Ali Ismayilov*
5+
16
* Declare `ActiveStorage::FixtureSet` and `ActiveStorage::FixtureSet.blob` to
27
improve fixture integration
38

activestorage/config/routes.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@
1616
end
1717

1818
direct :rails_representation do |representation, options|
19-
signed_blob_id = representation.blob.signed_id
20-
variation_key = representation.variation.key
21-
filename = representation.blob.filename
22-
23-
route_for(:rails_blob_representation, signed_blob_id, variation_key, filename, options)
19+
route_for(ActiveStorage.resolve_model_to_route, representation, options)
2420
end
2521

2622
resolve("ActiveStorage::Variant") { |variant, options| route_for(ActiveStorage.resolve_model_to_route, variant, options) }
2723
resolve("ActiveStorage::VariantWithRecord") { |variant, options| route_for(ActiveStorage.resolve_model_to_route, variant, options) }
2824
resolve("ActiveStorage::Preview") { |preview, options| route_for(ActiveStorage.resolve_model_to_route, preview, options) }
2925

3026
direct :rails_blob do |blob, options|
31-
route_for(:rails_service_blob, blob.signed_id, blob.filename, options)
27+
route_for(ActiveStorage.resolve_model_to_route, blob, options)
3228
end
3329

3430
resolve("ActiveStorage::Blob") { |blob, options| route_for(ActiveStorage.resolve_model_to_route, blob, options) }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "database/setup"
5+
6+
class RailsStorageProxyUrlTest < ActiveSupport::TestCase
7+
include Rails.application.routes.url_helpers
8+
9+
setup do
10+
@blob = create_file_blob filename: "racecar.jpg"
11+
@was_resolve_model_to_route, ActiveStorage.resolve_model_to_route = ActiveStorage.resolve_model_to_route, :rails_storage_proxy
12+
end
13+
14+
teardown do
15+
ActiveStorage.resolve_model_to_route = @was_resolve_model_to_route
16+
end
17+
18+
test "rails_storage_proxy_path generates proxy path" do
19+
assert_includes rails_storage_proxy_path(@blob, only_path: true), "/rails/active_storage/blobs/proxy/"
20+
end
21+
22+
test "rails_storage_redirect_path generates redirect path" do
23+
assert_includes rails_storage_redirect_path(@blob, only_path: true), "/rails/active_storage/blobs/redirect/"
24+
end
25+
26+
test "rails_blob_path generates proxy path" do
27+
assert_includes rails_blob_path(@blob, only_path: true), "/rails/active_storage/blobs/proxy/"
28+
end
29+
30+
test "rails_blob_path with variant generates proxy path" do
31+
variant = @blob.variant(resize: "100x100")
32+
assert_includes rails_blob_path(variant, only_path: true), "/rails/active_storage/representations/proxy/"
33+
end
34+
35+
test "rails_representation_path generates proxy path" do
36+
variant = @blob.variant(resize: "100x100")
37+
assert_includes rails_representation_path(variant, only_path: true), "/rails/active_storage/representations/proxy/"
38+
end
39+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
require "database/setup"
5+
6+
class RailsStorageRedirectUrlTest < ActiveSupport::TestCase
7+
include Rails.application.routes.url_helpers
8+
9+
setup do
10+
@blob = create_file_blob filename: "racecar.jpg"
11+
@was_resolve_model_to_route, ActiveStorage.resolve_model_to_route = ActiveStorage.resolve_model_to_route, :rails_storage_redirect
12+
end
13+
14+
teardown do
15+
ActiveStorage.resolve_model_to_route = @was_resolve_model_to_route
16+
end
17+
18+
test "rails_storage_proxy_path generates proxy path" do
19+
assert_includes rails_storage_proxy_path(@blob, only_path: true), "/rails/active_storage/blobs/proxy/"
20+
end
21+
22+
test "rails_storage_redirect_path generates redirect path" do
23+
assert_includes rails_storage_redirect_path(@blob, only_path: true), "/rails/active_storage/blobs/redirect/"
24+
end
25+
26+
test "rails_blob_path generates redirect path" do
27+
assert_includes rails_blob_path(@blob, only_path: true), "/rails/active_storage/blobs/redirect/"
28+
end
29+
30+
test "rails_blob_path with variant generates redirect path" do
31+
variant = @blob.variant(resize: "100x100")
32+
assert_includes rails_blob_path(variant, only_path: true), "/rails/active_storage/representations/redirect/"
33+
end
34+
35+
test "rails_representation_path generates proxy path" do
36+
variant = @blob.variant(resize: "100x100")
37+
assert_includes rails_representation_path(variant, only_path: true), "/rails/active_storage/representations/redirect/"
38+
end
39+
end

0 commit comments

Comments
 (0)