Sha256: f41ec8b0ca71e48149edab4d1b3ef94590ef8c35b962910034b8d88f95e95d55

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'image_optim'

class ImageOptim
  # Adds image_optim as preprocessor for gif, jpeg, png and svg images
  class Railtie < Rails::Railtie
    config.before_configuration do |app|
      worker_names = ImageOptim::Worker.klasses.map(&:bin_sym)
      app.config.assets.image_optim =
        ActiveSupport::OrderedOptions.new do |hash, key|
          if worker_names.include?(key.to_sym)
            hash[key] = ActiveSupport::OrderedOptions.new
          end
        end
    end

    initializer 'image_optim.initializer' do |app|
      register_preprocessor(app) if register_preprocessor?(app)
    end

    def register_preprocessor?(app)
      return if app.config.assets.compress == false
      return if app.config.assets.image_optim == false

      app.assets
    end

    def options(app)
      if app.config.assets.image_optim == true
        {}
      else
        app.config.assets.image_optim || {}
      end
    end

    def register_preprocessor(app)
      image_optim = ImageOptim.new(options(app))

      processor = proc do |_context, data|
        image_optim.optimize_image_data(data) || data
      end

      app.assets.register_preprocessor 'image/gif', :image_optim, &processor
      app.assets.register_preprocessor 'image/jpeg', :image_optim, &processor
      app.assets.register_preprocessor 'image/png', :image_optim, &processor
      app.assets.register_preprocessor 'image/svg+xml', :image_optim, &processor
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
image_optim-0.22.0 lib/image_optim/railtie.rb