diff options
author | George Claghorn <george.claghorn@gmail.com> | 2017-10-12 11:47:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-12 11:47:21 -0400 |
commit | 445c682a8465b1a42f1335ae2cf7d20b9a112fcd (patch) | |
tree | e86aac9a6dfba21a78b7ce1901a95adc7b42a9ab /activestorage/app/models | |
parent | 8bcb4fab366611d3e9b34690682415281440c128 (diff) | |
download | rails-445c682a8465b1a42f1335ae2cf7d20b9a112fcd.tar.gz rails-445c682a8465b1a42f1335ae2cf7d20b9a112fcd.tar.bz2 rails-445c682a8465b1a42f1335ae2cf7d20b9a112fcd.zip |
Introduce ActiveStorage::Blob#representation
Diffstat (limited to 'activestorage/app/models')
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 26 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/variant.rb | 4 |
2 files changed, 30 insertions, 0 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index ff785d4f61..d43049e32c 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -15,6 +15,7 @@ # If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one. class ActiveStorage::Blob < ActiveRecord::Base class UnpreviewableError < StandardError; end + class UnrepresentableError < StandardError; end self.table_name = "active_storage_blobs" @@ -155,6 +156,31 @@ class ActiveStorage::Blob < ActiveRecord::Base end + # Returns an ActiveStorage::Preview instance for a previewable blob or an ActiveStorage::Variant instance for an image blob. + # + # blob.representation(resize: "100x100").processed.service_url + # + # Raises ActiveStorage::Blob::UnrepresentableError if the receiving blob is neither an image nor previewable. Call + # ActiveStorage::Blob#representable? to determine whether a blob is representable. + # + # See ActiveStorage::Blob#preview and ActiveStorage::Blob#variant for more information. + def representation(transformations) + case + when previewable? + preview transformations + when image? + variant transformations + else + raise UnrepresentableError + end + end + + # Returns true if the blob is an image or is previewable. + def representable? + image? || previewable? + end + + # Returns the URL of the blob on the service. This URL is intended to be short-lived for security and not used directly # with users. Instead, the +service_url+ should only be exposed as a redirect from a stable, possibly authenticated URL. # Hiding the +service_url+ behind a redirect also gives you the power to change services without updating all URLs. And diff --git a/activestorage/app/models/active_storage/variant.rb b/activestorage/app/models/active_storage/variant.rb index 90a3605331..915b78162c 100644 --- a/activestorage/app/models/active_storage/variant.rb +++ b/activestorage/app/models/active_storage/variant.rb @@ -65,6 +65,10 @@ class ActiveStorage::Variant service.url key, expires_in: expires_in, disposition: disposition, filename: blob.filename, content_type: blob.content_type end + # Returns the receiving variant. Allows ActiveStorage::Variant and ActiveStorage::Preview instances to be duck-typed. + def image + self + end private def processed? |