Skip to content

Commit aee9d5d

Browse files
committed
Fixed error for download method with block - #211
1 parent ef24e9d commit aee9d5d

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.2 (unreleased)
2+
3+
- Fixed error for `download` method with block
4+
15
## 2.0.1 (2024-12-29)
26

37
- Added support for Ruby 3.4

lib/lockbox/active_storage_extensions.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def initialize(name, record, attachable)
8080

8181
module Attachment
8282
def download
83-
result = super
83+
result = super(&nil)
8484

8585
options = Utils.encrypted_options(record, name)
8686
# only trust the metadata when migrating
@@ -91,7 +91,15 @@ def download
9191
result = Utils.decrypt_result(record, name, options, result)
9292
end
9393

94-
result
94+
if block_given?
95+
io = StringIO.new(result)
96+
chunk_size = 5.megabytes
97+
while (chunk = io.read(chunk_size))
98+
yield chunk
99+
end
100+
else
101+
result
102+
end
95103
end
96104

97105
def variant(*args)

test/active_storage_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ def test_open_blank
281281
end
282282
end
283283

284+
def test_download_block
285+
user = User.create!(avatar: attachment)
286+
chunks = []
287+
user.avatar.download do |chunk|
288+
chunks << chunk
289+
end
290+
assert_equal content, chunks.join
291+
end
292+
284293
def test_metadata
285294
user = User.create!
286295

0 commit comments

Comments
 (0)