diff options
-rw-r--r-- | app/models/active_storage/blob.rb | 24 | ||||
-rw-r--r-- | test/models/blob_test.rb | 6 |
2 files changed, 14 insertions, 16 deletions
diff --git a/app/models/active_storage/blob.rb b/app/models/active_storage/blob.rb index b2d5b2362c..0f9562e23b 100644 --- a/app/models/active_storage/blob.rb +++ b/app/models/active_storage/blob.rb @@ -84,19 +84,17 @@ 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 true if the content_type of this blob is in the image range, like image/png. + def image?() content_type =~ /^image/ end + + # Returns true if the content_type of this blob is in the audio range, like audio/mpeg. + def audio?() content_type =~ /^audio/ end + + # Returns true if the content_type of this blob is in the video range, like video/mp4. + def video?() content_type =~ /^video/ end + + # Returns true if the content_type of this blob is in the text range, like text/plain. + def text?() content_type =~ /^text/ 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 b51be7a93b..8b14bcec87 100644 --- a/test/models/blob_test.rb +++ b/test/models/blob_test.rb @@ -11,10 +11,10 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase assert_equal Digest::MD5.base64digest(data), blob.checksum end - test "inquery type" do + test "text?" do blob = create_blob data: "Hello world!" - assert blob.type.text? - assert_not blob.type.audio? + assert blob.text? + assert_not blob.audio? end test "download yields chunks" do |