diff options
Diffstat (limited to 'activestorage')
-rw-r--r-- | activestorage/app/models/active_storage/variant.rb | 2 | ||||
-rw-r--r-- | activestorage/app/models/active_storage/variation.rb | 22 | ||||
-rw-r--r-- | activestorage/config/routes.rb | 2 | ||||
-rw-r--r-- | activestorage/test/models/variant_test.rb | 14 |
4 files changed, 32 insertions, 8 deletions
diff --git a/activestorage/app/models/active_storage/variant.rb b/activestorage/app/models/active_storage/variant.rb index bdcb5e6a41..e08a2271ec 100644 --- a/activestorage/app/models/active_storage/variant.rb +++ b/activestorage/app/models/active_storage/variant.rb @@ -18,7 +18,7 @@ require "active_storage/downloading" # To refer to such a delayed on-demand variant, simply link to the variant through the resolved route provided # by Active Storage like so: # -# <%= image_tag url_for(Current.user.avatar.variant(resize: "100x100")) %> +# <%= image_tag Current.user.avatar.variant(resize: "100x100") %> # # This will create a URL for that specific blob with that specific variant, which the ActiveStorage::VariantsController # can then produce on-demand. diff --git a/activestorage/app/models/active_storage/variation.rb b/activestorage/app/models/active_storage/variation.rb index fc4305dcc5..09997eb97f 100644 --- a/activestorage/app/models/active_storage/variation.rb +++ b/activestorage/app/models/active_storage/variation.rb @@ -46,13 +46,15 @@ class ActiveStorage::Variation # Accepts an open MiniMagick image instance, like what's returned by <tt>MiniMagick::Image.read(io)</tt>, # and performs the +transformations+ against it. The transformed image instance is then returned. def transform(image) - transformations.each do |method, argument| - image.mogrify do |command| - if eligible_argument?(argument) - command.public_send(method, argument) - else - command.public_send(method) + transformations.each do |(method, argument)| + if method.to_s == "combine_options" + image.combine_options do |combination| + argument.each do |(method, argument)| + pass_transform_argument(combination, method, argument) + end end + else + pass_transform_argument(image, method, argument) end end end @@ -66,4 +68,12 @@ class ActiveStorage::Variation def eligible_argument?(argument) argument.present? && argument != true end + + def pass_transform_argument(instance, method, argument) + if eligible_argument?(argument) + instance.public_send(method, argument) + else + instance.public_send(method) + end + end end diff --git a/activestorage/config/routes.rb b/activestorage/config/routes.rb index ad9640ce03..3f4129d915 100644 --- a/activestorage/config/routes.rb +++ b/activestorage/config/routes.rb @@ -7,7 +7,7 @@ Rails.application.routes.draw do route_for(:rails_service_blob, blob.signed_id, blob.filename, options) end - resolve("ActiveStorage::Blob") { |blob, options| route_for(:rails_blob, blob) } + resolve("ActiveStorage::Blob") { |blob, options| route_for(:rails_blob, blob, options) } resolve("ActiveStorage::Attachment") { |attachment, options| route_for(:rails_blob, attachment.blob, options) } diff --git a/activestorage/test/models/variant_test.rb b/activestorage/test/models/variant_test.rb index 8eced41ee0..2a96dfb142 100644 --- a/activestorage/test/models/variant_test.rb +++ b/activestorage/test/models/variant_test.rb @@ -25,6 +25,20 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase assert_match(/Gray/, image.colorspace) end + test "center-weighted crop of JPEG blob" do + blob = create_file_blob(filename: "racecar.jpg") + variant = blob.variant(combine_options: { + gravity: "center", + resize: "100x100^", + crop: "100x100+0+0", + }).processed + assert_match(/racecar\.jpg/, variant.service_url) + + image = read_image(variant) + assert_equal 100, image.width + assert_equal 100, image.height + end + test "resized variation of PSD blob" do blob = create_file_blob(filename: "icon.psd", content_type: "image/vnd.adobe.photoshop") variant = blob.variant(resize: "20x20").processed |