diff options
author | Jacob Smith <jacob.wesley.smith@gmail.com> | 2018-05-19 22:14:57 -0400 |
---|---|---|
committer | Jacob Smith <jacob.wesley.smith@gmail.com> | 2018-05-21 10:38:15 -0400 |
commit | 0210ac0b430757f2b5ba5e81f4391e6a37b769a4 (patch) | |
tree | 389f87256bdc031e67b09e076b1820282ed77233 /activestorage/app | |
parent | ce4d467f7c2fc812e257a87bd4875c1f1f08a981 (diff) | |
download | rails-0210ac0b430757f2b5ba5e81f4391e6a37b769a4.tar.gz rails-0210ac0b430757f2b5ba5e81f4391e6a37b769a4.tar.bz2 rails-0210ac0b430757f2b5ba5e81f4391e6a37b769a4.zip |
Disable variant options when false or nil present
In response to https://github.com/rails/rails/issues/32917
In the current implementation, ActiveStorage passes all options to the underlying processor,
including when a key has a value of false.
For example, passing:
```
avatar.variant(resize: "100x100", monochrome: false, flip: "-90")
```
will return a monochrome image (or an error, pending on ImageMagick configuration) because
it passes `-monochrome false` to the command (but the command line does not allow disabling
flags this way, as usually a user would omit the flag entirely to disable that feature).
This fix only passes those keys forward to the underlying processor if the value responds to
`present?`. In practice, this means that `false` or `nil` will be filtered out before going
to the processor.
One possible use case would be for a user to be able to apply different filters to an avatar.
The code might look something like:
```
variant_options = {
monochrome: params[:monochrome],
resize: params[:resize]
}
avatar.variant(*variant_options)
```
Obviously some sanitization may be beneficial in a real-world scenario, but this type of
configuration object could be used in many other places as well.
- Add removing falsy values from varaints to changelog
- The entirety of #image_processing_transformation inject block was wrapped in `list.tap`
to guard against the default `nil` being returned if no conditional was called.
- add test for explicitly true variant options
Diffstat (limited to 'activestorage/app')
-rw-r--r-- | activestorage/app/models/active_storage/variation.rb | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/activestorage/app/models/active_storage/variation.rb b/activestorage/app/models/active_storage/variation.rb index 42f00beb82..806af6366d 100644 --- a/activestorage/app/models/active_storage/variation.rb +++ b/activestorage/app/models/active_storage/variation.rb @@ -66,11 +66,13 @@ class ActiveStorage::Variation # Applies image transformations using the ImageProcessing gem. def image_processing_transform(file, format) operations = transformations.inject([]) do |list, (name, argument)| - if name.to_s == "combine_options" - ActiveSupport::Deprecation.warn("The ImageProcessing ActiveStorage variant backend doesn't need :combine_options, as it already generates a single MiniMagick command. In Rails 6.1 :combine_options will not be supported anymore.") - list.concat argument.to_a - else - list << [name, argument] + list.tap do |list| + if name.to_s == "combine_options" + ActiveSupport::Deprecation.warn("The ImageProcessing ActiveStorage variant backend doesn't need :combine_options, as it already generates a single MiniMagick command. In Rails 6.1 :combine_options will not be supported anymore.") + list.concat argument.keep_if { |key, value| value.present? }.to_a + elsif argument.present? + list << [name, argument] + end end end @@ -116,14 +118,10 @@ class ActiveStorage::Variation end def pass_transform_argument(command, method, argument) - if eligible_argument?(argument) - command.public_send(method, argument) - else + if argument == true command.public_send(method) + elsif argument.present? + command.public_send(method, argument) end end - - def eligible_argument?(argument) - argument.present? && argument != true - end end |