Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run image compress even if Rails.application.assets is nil #121

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ Style/SpaceBeforeBlockBraces:
Style/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Style/AlignParameters:
EnforcedStyle: with_first_parameter

#Style/TrailingCommaInArguments:
# EnforcedStyleForMultiline: comma
#Style/TrailingCommaInLiteral:
Style/TrailingComma:
EnforcedStyleForMultiline: comma
2 changes: 1 addition & 1 deletion image_optim.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'image_optim_pack', '~> 0.2'
s.add_development_dependency 'rspec', '~> 3.0'
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9.3')
s.add_development_dependency 'rubocop', '~> 0.35'
s.add_development_dependency 'rubocop', '0.35'
end
end
39 changes: 39 additions & 0 deletions lib/image_optim/image_optim_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ImageOptim
# Adds image_optim_processor as class to avoid LegacyTildPreprocessor
# in sprockets
class ImageOptimProcessor
def self.opti_images_options=(options)
@options = options == true ? {} : (options || {})
image_optim
end

def self.call(input)
@environment = input[:environment]
@uri = input[:uri]
@filename = input[:filename]
@dirname = File.dirname(@filename)
@content_type = input[:content_type]
@required = Set.new(input[:metadata][:required])
@stubbed = Set.new(input[:metadata][:stubbed])
@links = Set.new(input[:metadata][:links])
@dependencies = Set.new(input[:metadata][:dependencies])

data = process_source(input[:data])

{:data => data,
:required => @required,
:stubbed => @stubbed,
:links => @links,
:dependencies => @dependencies,
:charset => nil}
end

def self.process_source(data)
image_optim.optimize_image_data(data) || data
end

def self.image_optim
@image_optim ||= ImageOptim.new(@options)
end
end
end
32 changes: 23 additions & 9 deletions lib/image_optim/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'image_optim'
require 'image_optim/image_optim_processor'

class ImageOptim
# Adds image_optim as preprocessor for gif, jpeg, png and svg images
Expand All @@ -21,7 +22,7 @@ def register_preprocessor?(app)
return if app.config.assets.compress == false
return if app.config.assets.image_optim == false

app.assets
true
end

def options(app)
Expand All @@ -33,16 +34,29 @@ def options(app)
end

def register_preprocessor(app)
image_optim = ImageOptim.new(options(app))
ImageOptim::ImageOptimProcessor.opti_images_options = options(app)

processor = proc do |_context, data|
image_optim.optimize_image_data(data) || data
if defined?(Sprockets::Processor)
processor = proc do |_context, data|
ImageOptim::ImageOptimProcessor.process_source(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
else
app.config.assets.configure do |env|
env.register_preprocessor 'image/gif', :image_optim,
ImageOptim::ImageOptimProcessor
env.register_preprocessor 'image/jpeg', :image_optim,
ImageOptim::ImageOptimProcessor
env.register_preprocessor 'image/png', :image_optim,
ImageOptim::ImageOptimProcessor
env.register_preprocessor 'image/svg+xml', :image_optim,
ImageOptim::ImageOptimProcessor
end
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