Skip to content

Commit

Permalink
allow setting individual worker options in rails config, related to #111
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Nov 21, 2015
1 parent 4a6c1f7 commit 4f3137e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/image_optim/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ class ImageOptim
# Adds image_optim as preprocessor for gif, jpeg, png and svg images
class Railtie < Rails::Railtie
config.before_configuration do |app|
app.config.assets.image_optim = ActiveSupport::OrderedOptions.new
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|
Expand Down
21 changes: 20 additions & 1 deletion spec/image_optim/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,32 @@ def self.name
end
end

it 'is possible to assign individual values' do
it 'is possible to set individual options' do
hash = {:config_paths => 'config/image_optim.yml'}
expect(ImageOptim).to receive(:new).with(hash)
init_rails_app do |config|
config.assets.image_optim.config_paths = 'config/image_optim.yml'
end
end

it 'is possible to set individual worker options' do
hash = {:advpng => {:level => 3}}
expect(ImageOptim).to receive(:new).with(hash)
init_rails_app do |config|
expect(config.assets.image_optim.advpng).to eq({})
config.assets.image_optim.advpng.level = 3
end
end

it 'is not possible to set unknown worker options' do
expect(ImageOptim).to receive(:new).with({})
init_rails_app do |config|
expect(config.assets.image_optim.unknown).to eq(nil)
expect do
config.assets.image_optim.unknown.level = 3
end.to raise_error(NoMethodError)
end
end
end
end

Expand Down

0 comments on commit 4f3137e

Please sign in to comment.