Skip to content

Commit f98bb7e

Browse files
Merge pull request rails#50046 from chaadow/refactor_preview_tests_and_code
[ActiveStorage] Prevent `AS::Preview#processed` to generate a variant for an empty transformation
2 parents c12140a + eae1965 commit f98bb7e

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

activestorage/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Prevent `ActiveStorage::Blob#preview` to generate a variant if an empty variation is passed.
2+
Calls to `#url`, `#key` or `#download` will now use the original preview
3+
image instead of generating a variant with the exact same dimensions.
4+
5+
*Chedli Bourguiba*
6+
17
* Process preview image variant when calling `ActiveStorage::Preview#processed`.
28
For example, `attached_pdf.preview(:thumb).processed` will now immediately
39
generate the full-sized preview image and the `:thumb` variant of it.

activestorage/app/models/active_storage/preview.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def initialize(blob, variation_or_variation_key)
4747
# image is stored with the blob, it is only generated once.
4848
def processed
4949
process unless processed?
50-
variant.processed
50+
variant.processed if variant?
5151
self
5252
end
5353

@@ -63,7 +63,7 @@ def image
6363
# a stable URL that redirects to the URL returned by this method.
6464
def url(**options)
6565
if processed?
66-
variant.url(**options)
66+
presentation.url(**options)
6767
else
6868
raise UnprocessedError
6969
end
@@ -72,7 +72,7 @@ def url(**options)
7272
# Returns a combination key of the blob and the variation that together identifies a specific variant.
7373
def key
7474
if processed?
75-
variant.key
75+
presentation.key
7676
else
7777
raise UnprocessedError
7878
end
@@ -85,7 +85,7 @@ def key
8585
# if the preview has not been processed yet.
8686
def download(&block)
8787
if processed?
88-
variant.download(&block)
88+
presentation.download(&block)
8989
else
9090
raise UnprocessedError
9191
end
@@ -105,7 +105,15 @@ def process
105105
end
106106

107107
def variant
108-
image.variant(variation).processed
108+
image.variant(variation)
109+
end
110+
111+
def variant?
112+
variation.transformations.present?
113+
end
114+
115+
def presentation
116+
variant? ? variant.processed : image
109117
end
110118

111119

activestorage/test/models/preview_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
7272
end
7373
end
7474

75+
test "image-related methods raise UnprocessedError when preview is not processed" do
76+
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
77+
preview = blob.preview(resize_to_limit: [640, 280])
78+
79+
assert_raises(ActiveStorage::Preview::UnprocessedError) { preview.url }
80+
assert_raises(ActiveStorage::Preview::UnprocessedError) { preview.key }
81+
assert_raises(ActiveStorage::Preview::UnprocessedError) { preview.download }
82+
end
83+
84+
test "previewing with empty transformations does not generate a variant" do
85+
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
86+
preview = blob.preview({})
87+
88+
preview.processed
89+
90+
freeze_time { assert_equal blob.preview_image.url, preview.url }
91+
assert_equal blob.preview_image.key, preview.key
92+
assert_equal blob.preview_image.download, preview.download
93+
assert_empty preview.image.variant_records
94+
end
95+
7596
test "preview of PDF is created on the same service" do
7697
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf", service_name: "local_public")
7798
preview = blob.preview(resize_to_limit: [640, 280]).processed

0 commit comments

Comments
 (0)