Class: ImageOptim::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/image_optim/railtie.rb

Overview

Adds image_optim as preprocessor for gif, jpeg, png and svg images

Instance Method Summary collapse

Instance Method Details

#options(app) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/image_optim/railtie.rb', line 27

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

#register_preprocessor(app) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/image_optim/railtie.rb', line 35

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

#register_preprocessor?(app) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/image_optim/railtie.rb', line 20

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

  app.assets
end