Skip to content

Commit 757f489

Browse files
Merge pull request rails#50044 from jonathanhefner/active_storage-preview-process-variant
Process preview variant when processing preview
2 parents 55c7b9b + a5e1fc9 commit 757f489

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

activestorage/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Process preview image variant when calling `ActiveStorage::Preview#processed`.
2+
For example, `attached_pdf.preview(:thumb).processed` will now immediately
3+
generate the full-sized preview image and the `:thumb` variant of it.
4+
Previously, the `:thumb` variant would not be generated until a further call
5+
to e.g. `processed.url`.
6+
7+
*Chedli Bourguiba* and *Jonathan Hefner*
8+
19
* Prevent `ActiveRecord::StrictLoadingViolationError` when strict loading is
210
enabled and the variant of an Active Storage preview has already been
311
processed (for example, by calling `ActiveStorage::Preview#url`).

activestorage/app/models/active_storage/preview.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +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
5051
self
5152
end
5253

activestorage/test/jobs/transform_job_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ class ActiveStorage::TransformJobTest < ActiveJob::TestCase
2020
@blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
2121
transformations = { resize_to_limit: [100, 100] }
2222

23-
assert_changes -> { @blob.reload.preview(transformations).send(:processed?) }, from: false, to: true do
23+
assert_changes -> { @blob.preview(transformations).send(:processed?) }, from: false, to: true do
2424
perform_enqueued_jobs do
2525
ActiveStorage::TransformJob.perform_later @blob, transformations
2626
end
27+
@blob.reload
2728
end
29+
30+
assert @blob.preview(transformations).image.variant(transformations).send(:processed?)
2831
end
2932

3033
test "creates variant when untracked" do

activestorage/test/models/preview_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
6262
assert_predicate blob.reload.preview_image, :attached?
6363
end
6464

65+
test "#processed also processes the preview's image variant" do
66+
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
67+
transformations = { resize_to_limit: [640, 280] }
68+
preview = blob.preview(transformations)
69+
70+
assert_changes -> { !!preview.image.variant(transformations)&.send(:processed?) }, to: true do
71+
preview.processed
72+
end
73+
end
74+
6575
test "preview of PDF is created on the same service" do
6676
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf", service_name: "local_public")
6777
preview = blob.preview(resize_to_limit: [640, 280]).processed

0 commit comments

Comments
 (0)