2017-07-21 16:51:31 -04:00
|
|
|
require "active_support/core_ext/object/inclusion"
|
|
|
|
|
|
|
|
# A set of transformations that can be applied to a blob to create a variant.
|
|
|
|
class ActiveStorage::Variation
|
|
|
|
attr_reader :transformations
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def decode(key)
|
2017-07-23 12:03:25 -04:00
|
|
|
new ActiveStorage.verifier.verify(key)
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
2017-07-22 00:14:46 -04:00
|
|
|
|
2017-07-21 16:51:31 -04:00
|
|
|
def encode(transformations)
|
2017-07-23 12:03:25 -04:00
|
|
|
ActiveStorage.verifier.generate(transformations)
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(transformations)
|
|
|
|
@transformations = transformations
|
|
|
|
end
|
|
|
|
|
|
|
|
def transform(image)
|
|
|
|
transformations.each do |(method, argument)|
|
|
|
|
if eligible_argument?(argument)
|
|
|
|
image.public_send(method, argument)
|
|
|
|
else
|
|
|
|
image.public_send(method)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
self.class.encode(transformations)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def eligible_argument?(argument)
|
|
|
|
argument.present? && argument != true
|
|
|
|
end
|
|
|
|
end
|