提交 62ff514d 编写于 作者: G George Claghorn

Accept variation keys in #preview and #variant

上级 445c682a
......@@ -124,7 +124,7 @@ def text?
# 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 @@ def variant(transformations)
# 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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册