Merge pull request #49195 from p8/activestorage/variant-interface

Keep VariantWithRecord API consistent with Variant
This commit is contained in:
Ryuta Kamizono 2023-09-08 17:52:05 +09:00 committed by GitHub
commit 099f979dc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

@ -7,6 +7,7 @@
class ActiveStorage::VariantWithRecord
attr_reader :blob, :variation
delegate :service, to: :blob
delegate :content_type, to: :variation
def initialize(blob, variation)
@blob, @variation = blob, ActiveStorage::Variation.wrap(variation)
@ -21,6 +22,10 @@ def image
record&.image
end
def filename
ActiveStorage::Filename.new "#{blob.filename.base}.#{variation.format.downcase}"
end
# Destroys record and deletes file from service.
def destroy
record&.destroy

@ -23,6 +23,8 @@ class ActiveStorage::VariantWithRecordTest < ActiveSupport::TestCase
end
assert_match(/racecar\.jpg/, variant.url)
assert_equal "racecar.jpg", variant.filename.to_s
assert_equal "image/jpeg", variant.content_type
image = read_image(variant.image)
assert_equal 100, image.width