aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/transformers/mini_magick_transformer.rb
blob: e8e99cea9e682c63000d9c3dd32310af76022986 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

require "mini_magick"

module ActiveStorage
  module Transformers
    class MiniMagickTransformer < Transformer
      private
        def process(file, format:)
          image = MiniMagick::Image.new(file.path, file)

          transformations.each do |name, argument_or_subtransformations|
            image.mogrify do |command|
              if name.to_s == "combine_options"
                argument_or_subtransformations.each do |subtransformation_name, subtransformation_argument|
                  pass_transform_argument(command, subtransformation_name, subtransformation_argument)
                end
              else
                pass_transform_argument(command, name, argument_or_subtransformations)
              end
            end
          end

          image.format(format) if format

          image.tempfile.tap(&:open)
        end

        def pass_transform_argument(command, method, argument)
          if argument == true
            command.public_send(method)
          elsif argument.present?
            command.public_send(method, argument)
          end
        end
    end
  end
end