This commit is contained in:
Rafael Mendonça França 2023-03-25 16:55:06 +00:00
commit febd9ab4f6
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
5 changed files with 42 additions and 0 deletions

@ -1,3 +1,11 @@
* Allow destroying active storage variants
```ruby
User.first.avatar.variant(resize_to_limit: [100, 100]).destroy
```
*Shouichi Kamiya*, *Yuichiro NAKAGAWA*, *Ryohei UEDA*
* Add `sample_rate` to `ActiveStorage::Analyzer::AudioAnalyzer` output
*Matija Čupić*

@ -100,6 +100,11 @@ def image
self
end
# Deletes variant file from service.
def destroy
service.delete(key)
end
private
def processed?
service.exist?(key)

@ -27,6 +27,11 @@ def image
record&.image
end
# Destroys record and deletes file from service.
def destroy
record&.destroy
end
delegate :key, :url, :download, to: :image, allow_nil: true
private

@ -273,6 +273,15 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
end
end
test "destroy deletes file from service" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize_to_limit: [100, 100]).processed
assert_changes -> { blob.service.exist?(variant.key) }, from: true, to: false do
variant.destroy
end
end
private
def process_variants_with(processor)
previous_processor, ActiveStorage.variant_processor = ActiveStorage.variant_processor, processor

@ -4,6 +4,8 @@
require "database/setup"
class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
setup do
@was_tracking, ActiveStorage.track_variants = ActiveStorage.track_variants, true
end
@ -204,4 +206,17 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
end
end
end
test "destroy deletes file from service" do
blob = create_file_blob(filename: "racecar.jpg")
variant = blob.variant(resize_to_limit: [100, 100]).processed
assert_equal 1, ActiveStorage::VariantRecord.count
assert blob.service.exist?(variant.key)
variant.destroy
assert_equal 0, ActiveStorage::VariantRecord.count
assert_enqueued_with(job: ActiveStorage::PurgeJob, args: [variant.image.blob])
end
end