Skip to content

Commit

Permalink
railtie: support 2.x and 3.x of both sprockets and sprockets-rails, p…
Browse files Browse the repository at this point in the history
…ass processor to register_processor, resolves #120, resolves #121, resolves #126
  • Loading branch information
toy committed Feb 21, 2016
1 parent 2e6a2d6 commit 72543cd
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/image_optim/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ class Railtie < Rails::Railtie

@image_optim = ImageOptim.new(options(app))

register_preprocessor(app)
register_preprocessor(app) do |*args|
if args[1] # context and data arguments in sprockets 2
optimize_image_data(args[1])
else
input = args[0]
{
:data => optimize_image_data(input[:data]),
:charset => nil, # no gzipped version with rails/sprockets#228
}
end
end
end

def options(app)
Expand All @@ -41,13 +51,15 @@ def optimize_image_data(data)
@image_optim.optimize_image_data(data) || data
end

def register_preprocessor(app)
processor = proc do |_context, data|
optimize_image_data(data)
end

def register_preprocessor(app, &processor)
MIME_TYPES.each do |mime_type|
app.assets.register_preprocessor mime_type, :image_optim, &processor
if app.assets
app.assets.register_preprocessor mime_type, :image_optim, &processor
else
app.config.assets.configure do |env|
env.register_preprocessor mime_type, :image_optim, &processor
end
end
end
end
end
Expand Down

0 comments on commit 72543cd

Please sign in to comment.