diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-22 09:47:24 -0500 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-22 09:47:24 -0500 |
commit | d50679f4eefde1aca1ab71ba3c0109739cfdff3f (patch) | |
tree | ac9034fe7c4aa64cd5e90ecebc346d478917387c /app/models/active_storage/variation.rb | |
parent | 5b7c31c23a708de77b3d73b68aec0ba99c8be861 (diff) | |
download | rails-d50679f4eefde1aca1ab71ba3c0109739cfdff3f.tar.gz rails-d50679f4eefde1aca1ab71ba3c0109739cfdff3f.tar.bz2 rails-d50679f4eefde1aca1ab71ba3c0109739cfdff3f.zip |
Move models and jobs to the app setup
Follow engine conventions more closely
Diffstat (limited to 'app/models/active_storage/variation.rb')
-rw-r--r-- | app/models/active_storage/variation.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/app/models/active_storage/variation.rb b/app/models/active_storage/variation.rb new file mode 100644 index 0000000000..f7c81bb99a --- /dev/null +++ b/app/models/active_storage/variation.rb @@ -0,0 +1,41 @@ +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 + class_attribute :verifier + + attr_reader :transformations + + class << self + def decode(key) + new verifier.verify(key) + end + + def encode(transformations) + verifier.generate(transformations) + 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 |