Class: ActiveStorage::Variation
- Inherits:
-
Object
- Object
- ActiveStorage::Variation
- Defined in:
- app/models/active_storage/variation.rb
Overview
A set of transformations that can be applied to a blob to create a variant. This class is exposed via the ‘ActiveStorage::Blob#variant` method and should rarely be used directly.
In case you do need to use this directly, it’s instantiated using a hash of transformations where the key is the command and the value is the arguments. Example:
ActiveStorage::Variation.new(resize: "100x100", monochrome: true, trim: true, rotate: "-90")
A list of all possible transformations is available at www.imagemagick.org/script/mogrify.php.
Instance Attribute Summary collapse
-
#transformations ⇒ Object
readonly
Returns the value of attribute transformations.
Class Method Summary collapse
-
.decode(key) ⇒ Object
Returns a variation instance with the transformations that were encoded by ‘#encode`.
-
.encode(transformations) ⇒ Object
Returns a signed key for the ‘transformations`, which can be used to refer to a specific variation in a URL or combined key (like `ActiveStorage::Variant#key`).
Instance Method Summary collapse
-
#initialize(transformations) ⇒ Variation
constructor
A new instance of Variation.
-
#key ⇒ Object
Returns a signed key for all the ‘transformations` that this variation was instantiated with.
-
#transform(image) ⇒ Object
Accepts an open MiniMagick image instance, like what’s return by ‘MiniMagick::Image.read(io)`, and performs the `transformations` against it.
Constructor Details
#initialize(transformations) ⇒ Variation
Returns a new instance of Variation.
28 29 30 |
# File 'app/models/active_storage/variation.rb', line 28 def initialize(transformations) @transformations = transformations end |
Instance Attribute Details
#transformations ⇒ Object (readonly)
Returns the value of attribute transformations.
13 14 15 |
# File 'app/models/active_storage/variation.rb', line 13 def transformations @transformations end |
Class Method Details
.decode(key) ⇒ Object
Returns a variation instance with the transformations that were encoded by ‘#encode`.
17 18 19 |
# File 'app/models/active_storage/variation.rb', line 17 def decode(key) new ActiveStorage.verifier.verify(key, purpose: :variation) end |
.encode(transformations) ⇒ Object
Returns a signed key for the ‘transformations`, which can be used to refer to a specific variation in a URL or combined key (like `ActiveStorage::Variant#key`).
23 24 25 |
# File 'app/models/active_storage/variation.rb', line 23 def encode(transformations) ActiveStorage.verifier.generate(transformations, purpose: :variation) end |
Instance Method Details
#key ⇒ Object
Returns a signed key for all the ‘transformations` that this variation was instantiated with.
45 46 47 |
# File 'app/models/active_storage/variation.rb', line 45 def key self.class.encode(transformations) end |
#transform(image) ⇒ Object
Accepts an open MiniMagick image instance, like what’s return by ‘MiniMagick::Image.read(io)`, and performs the `transformations` against it. The transformed image instance is then returned.
34 35 36 37 38 39 40 41 42 |
# File 'app/models/active_storage/variation.rb', line 34 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 |