Skip to content

Commit

Permalink
Keep VariantWithRecord API consistent with Variant
Browse files Browse the repository at this point in the history
Similar to c18bcd5 adds the
content_type and filename methods to VariantWithRecord.
  • Loading branch information
p8 committed Sep 8, 2023
1 parent 6633671 commit 3e27f43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions activestorage/test/models/variant_with_record_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 3e27f43

Please sign in to comment.