diff options
-rw-r--r-- | app/models/active_storage/blob.rb | 14 | ||||
-rw-r--r-- | test/models/blob_test.rb | 6 |
2 files changed, 20 insertions, 0 deletions
diff --git a/app/models/active_storage/blob.rb b/app/models/active_storage/blob.rb index 6a7836b9e5..b2d5b2362c 100644 --- a/app/models/active_storage/blob.rb +++ b/app/models/active_storage/blob.rb @@ -84,6 +84,20 @@ class ActiveStorage::Blob < ActiveRecord::Base ActiveStorage::Filename.new(self[:filename]) end + # Returns a `StringInquirer` based on the content_type that is broken into text, image, audio, video, pdf, or, + # the catch-all, file. Example: `messages.attachments.select(&:image?)`. + def type + @type ||= + case content_type + when /^text/ then 'text' + when /^image/ then 'image' + when /^audio/ then 'audio' + when /^video/ then 'video' + when /pdf/ then 'pdf' + else 'file' + end.inquiry + end + # Returns a `ActiveStorage::Variant` instance with the set of `transformations` passed in. This is only relevant # for image files, and it allows any image to be transformed for size, colors, and the like. Example: # diff --git a/test/models/blob_test.rb b/test/models/blob_test.rb index a5b291d5db..b51be7a93b 100644 --- a/test/models/blob_test.rb +++ b/test/models/blob_test.rb @@ -11,6 +11,12 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase assert_equal Digest::MD5.base64digest(data), blob.checksum end + test "inquery type" do + blob = create_blob data: "Hello world!" + assert blob.type.text? + assert_not blob.type.audio? + end + test "download yields chunks" do blob = create_blob data: "a" * 75.kilobytes chunks = [] |