aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2017-10-12 13:40:25 -0400
committerGeorge Claghorn <george@basecamp.com>2017-10-12 13:40:49 -0400
commit62ff514d33d3a3b0930956a4b4866e6b228c278c (patch)
tree7fb2843cf601c9181596e427543018f3e1e61bf4
parent445c682a8465b1a42f1335ae2cf7d20b9a112fcd (diff)
downloadrails-62ff514d33d3a3b0930956a4b4866e6b228c278c.tar.gz
rails-62ff514d33d3a3b0930956a4b4866e6b228c278c.tar.bz2
rails-62ff514d33d3a3b0930956a4b4866e6b228c278c.zip
Accept variation keys in #preview and #variant
-rw-r--r--activestorage/app/models/active_storage/blob.rb4
-rw-r--r--activestorage/app/models/active_storage/variation.rb15
2 files changed, 12 insertions, 7 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb
index d43049e32c..84b8f3827b 100644
--- a/activestorage/app/models/active_storage/blob.rb
+++ b/activestorage/app/models/active_storage/blob.rb
@@ -124,7 +124,7 @@ class ActiveStorage::Blob < ActiveRecord::Base
# This will create a URL for that specific blob with that specific variant, which the ActiveStorage::VariantsController
# can then produce on-demand.
def variant(transformations)
- ActiveStorage::Variant.new(self, ActiveStorage::Variation.new(transformations))
+ ActiveStorage::Variant.new(self, ActiveStorage::Variation.wrap(transformations))
end
@@ -144,7 +144,7 @@ class ActiveStorage::Blob < ActiveRecord::Base
# whether a blob is accepted by any previewer, call ActiveStorage::Blob#previewable?.
def preview(transformations)
if previewable?
- ActiveStorage::Preview.new(self, ActiveStorage::Variation.new(transformations))
+ ActiveStorage::Preview.new(self, ActiveStorage::Variation.wrap(transformations))
else
raise UnpreviewableError
end
diff --git a/activestorage/app/models/active_storage/variation.rb b/activestorage/app/models/active_storage/variation.rb
index df2643442a..13bad87cac 100644
--- a/activestorage/app/models/active_storage/variation.rb
+++ b/activestorage/app/models/active_storage/variation.rb
@@ -13,16 +13,21 @@ class ActiveStorage::Variation
attr_reader :transformations
class << self
- def wrap(variation_or_key)
- case variation_or_key
+ # Returns a Variation instance based on the given variator. If the variator is a Variation, it is
+ # returned unmodified. If it is a String, it is passed to ActiveStorage::Variation.decode. Otherwise,
+ # it is assumed to be a transformations Hash and is passed directly to the constructor.
+ def wrap(variator)
+ case variator
when self
- variation_or_key
+ variator
+ when String
+ decode variator
else
- decode variation_or_key
+ new variator
end
end
- # Returns a variation instance with the transformations that were encoded by +encode+.
+ # Returns a Variation instance with the transformations that were encoded by +encode+.
def decode(key)
new ActiveStorage.verifier.verify(key, purpose: :variation)
end