File tree Expand file tree Collapse file tree 3 files changed +40
-5
lines changed
app/models/active_storage Expand file tree Collapse file tree 3 files changed +40
-5
lines changed Original file line number Diff line number Diff line change
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
+
1
7
* Process preview image variant when calling ` ActiveStorage::Preview#processed ` .
2
8
For example, ` attached_pdf.preview(:thumb).processed ` will now immediately
3
9
generate the full-sized preview image and the ` :thumb ` variant of it.
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ def initialize(blob, variation_or_variation_key)
47
47
# image is stored with the blob, it is only generated once.
48
48
def processed
49
49
process unless processed?
50
- variant . processed
50
+ variant . processed if variant?
51
51
self
52
52
end
53
53
@@ -63,7 +63,7 @@ def image
63
63
# a stable URL that redirects to the URL returned by this method.
64
64
def url ( **options )
65
65
if processed?
66
- variant . url ( **options )
66
+ presentation . url ( **options )
67
67
else
68
68
raise UnprocessedError
69
69
end
@@ -72,7 +72,7 @@ def url(**options)
72
72
# Returns a combination key of the blob and the variation that together identifies a specific variant.
73
73
def key
74
74
if processed?
75
- variant . key
75
+ presentation . key
76
76
else
77
77
raise UnprocessedError
78
78
end
@@ -85,7 +85,7 @@ def key
85
85
# if the preview has not been processed yet.
86
86
def download ( &block )
87
87
if processed?
88
- variant . download ( &block )
88
+ presentation . download ( &block )
89
89
else
90
90
raise UnprocessedError
91
91
end
@@ -105,7 +105,15 @@ def process
105
105
end
106
106
107
107
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
109
117
end
110
118
111
119
Original file line number Diff line number Diff line change @@ -72,6 +72,27 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
72
72
end
73
73
end
74
74
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
+
75
96
test "preview of PDF is created on the same service" do
76
97
blob = create_file_blob ( filename : "report.pdf" , content_type : "application/pdf" , service_name : "local_public" )
77
98
preview = blob . preview ( resize_to_limit : [ 640 , 280 ] ) . processed
You can’t perform that action at this time.
0 commit comments