image_optim 0.23.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/.appveyor.yml +8 -2
- data/.travis.yml +2 -11
- data/CHANGELOG.markdown +7 -0
- data/Gemfile +0 -9
- data/README.markdown +3 -8
- data/image_optim.gemspec +7 -2
- data/lib/image_optim.rb +34 -17
- data/lib/image_optim/space.rb +1 -1
- data/lib/image_optim/worker.rb +2 -1
- data/lib/image_optim/worker/optipng.rb +4 -0
- data/spec/image_optim/worker/optipng_spec.rb +44 -0
- data/spec/image_optim_spec.rb +29 -0
- data/spec/spec_helper.rb +2 -2
- metadata +9 -20
- data/lib/image_optim/railtie.rb +0 -66
- data/spec/image_optim/railtie_spec.rb +0 -147
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZmNjZjBjNmNkYzRmNjQxMmZkMDg3ZGYzYjY5MTQ3MjllYmVjOTQ1MQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmZlNWUxYzFmZmU4NDFmZjgxZDYwOTNlNzBlZTA0NTc0YzQ4NzJkNA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NGYzMjA1MTMxMTJhMzM1ODg1NTc4MGIyNzk5ZWNiMzFkMGRlMjQ5OTZjNTFm
|
10
|
+
YjJiZTdkZjgxYmQ3Y2U2MmNhMmRhYzE0YjkxZmJlY2EwMTg1MDNiMjUyMGNk
|
11
|
+
NjFhYWM3ZGVlOWY4MTRjYjg4ZDUwZmUyMzI1OTBhYTM4ZDI4ZGY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MjQ0MjZkN2RhYjYyMWU2ZWJhYTVhMDkyMjI3YWY0NTM1ZTgxN2U5YjY2NTM3
|
14
|
+
YmQ3OWNkNzNjZmI4NjFjM2I0YTYxZWE1ODQ5ZjU0ODYyNTVkOGZjMmE1MDE4
|
15
|
+
YjIyODM5OTI3YzQ5N2Y4MmYxMDFkYTc0OGQ2MmNkMjQ4MGZmMGQ=
|
data/.appveyor.yml
CHANGED
@@ -81,15 +81,21 @@ test_script:
|
|
81
81
|
- ps: |
|
82
82
|
$path = $env:Path
|
83
83
|
$rubypaths = ls -Path C:\Ruby*\bin
|
84
|
-
foreach ($rubypath in $rubypaths[
|
84
|
+
foreach ($rubypath in $rubypaths[-2, -1]) {
|
85
85
|
echo "################################################################################"
|
86
86
|
$env:Path = "$rubypath;" + $path
|
87
87
|
ruby --version
|
88
|
+
bundle package --all
|
88
89
|
bundle install --local -j4
|
89
90
|
bundle exec rspec
|
90
91
|
if ($LASTEXITCODE -gt 0) {
|
91
92
|
exit 1
|
92
93
|
}
|
94
|
+
rm tmp/optimize_test -recurse -force
|
95
|
+
cp spec/images tmp/optimize_test -recurse -force
|
96
|
+
bundle exec image_optim --allow-lossy -r tmp/optimize_test
|
97
|
+
if ($LASTEXITCODE -gt 0) {
|
98
|
+
exit 1
|
99
|
+
}
|
93
100
|
}
|
94
101
|
$env:Path = $path
|
95
|
-
- bundle exec image_optim --allow-lossy -r spec/images
|
data/.travis.yml
CHANGED
@@ -18,9 +18,6 @@ script: |
|
|
18
18
|
set -ex
|
19
19
|
if [ -n "$RUBOCOP" ]; then
|
20
20
|
bundle exec rubocop
|
21
|
-
elif [ -n "$RAILS_VERSION" ] && [ -z "$CODECLIMATE" ]; then
|
22
|
-
bundle exec image_optim --info
|
23
|
-
bundle exec rspec spec/image_optim/railtie_spec.rb
|
24
21
|
else
|
25
22
|
bundle exec image_optim --info
|
26
23
|
bundle exec rspec
|
@@ -40,16 +37,10 @@ before_install: |
|
|
40
37
|
matrix:
|
41
38
|
fast_finish: true
|
42
39
|
include:
|
43
|
-
- env:
|
44
|
-
rvm: default
|
45
|
-
- env: RAILS_VERSION='~> 3.2'
|
46
|
-
rvm: default
|
47
|
-
- env: RAILS_VERSION='~> 4.0' SPROCKETS_RAILS_VERSION='~> 2.0'
|
48
|
-
rvm: '2'
|
49
|
-
- env: RAILS_VERSION='~> 4.0' CODECLIMATE=1
|
40
|
+
- env: CODECLIMATE=true
|
50
41
|
rvm: '2'
|
51
42
|
- env: RUBOCOP=true
|
52
|
-
rvm:
|
43
|
+
rvm: '2'
|
53
44
|
allow_failures:
|
54
45
|
- rvm: jruby-19mode
|
55
46
|
addons:
|
data/CHANGELOG.markdown
CHANGED
@@ -2,6 +2,13 @@
|
|
2
2
|
|
3
3
|
## unreleased
|
4
4
|
|
5
|
+
## v0.24.0 (2016-08-14)
|
6
|
+
|
7
|
+
* Rails image assets optimization is extracted into [image\_optim\_rails gem](https://github.com/toy/image_optim_rails) [#127](https://github.com/toy/image_optim/issues/127) [@toy](https://github.com/toy)
|
8
|
+
* Add proper handling of `ImageOptim.respond_to?` [@toy](https://github.com/toy)
|
9
|
+
* Fix an issue not working OptiPNG `interlace` option [#136](https://github.com/toy/image_optim/pull/136) [@mrk21](https://github.com/mrk21)
|
10
|
+
* Minimize number of file system calls in default implementation of `optimized?` [#137](https://github.com/toy/image_optim/issues/137) [@toy](https://github.com/toy)
|
11
|
+
|
5
12
|
## v0.23.0 (2016-07-17)
|
6
13
|
|
7
14
|
* Added `cache_dir` and `cache_worker_digests` options to cache results [#83](https://github.com/toy/image_optim/issues/83) [@gpakosz](https://github.com/gpakosz)
|
data/Gemfile
CHANGED
@@ -5,12 +5,3 @@ gemspec
|
|
5
5
|
if ENV['CODECLIMATE']
|
6
6
|
gem 'codeclimate-test-reporter', :group => :test, :require => nil
|
7
7
|
end
|
8
|
-
|
9
|
-
%w[
|
10
|
-
rails
|
11
|
-
sprockets
|
12
|
-
sprockets-rails
|
13
|
-
].each do |gem_name|
|
14
|
-
version = ENV[gem_name.tr('-', '_').upcase + '_VERSION']
|
15
|
-
gem gem_name, version if version
|
16
|
-
end
|
data/README.markdown
CHANGED
@@ -58,7 +58,7 @@ With version:
|
|
58
58
|
|
59
59
|
<!---<update-version>-->
|
60
60
|
```ruby
|
61
|
-
gem 'image_optim', '~> 0.
|
61
|
+
gem 'image_optim', '~> 0.24'
|
62
62
|
```
|
63
63
|
<!---</update-version>-->
|
64
64
|
|
@@ -98,7 +98,7 @@ Besides permanently setting environment variables in `~/.profile`, `~/.bash_prof
|
|
98
98
|
|
99
99
|
### Binaries pack
|
100
100
|
|
101
|
-
Easiest way to get latest versions of most binaries for `image_optim` for Linux
|
101
|
+
Easiest way to get latest versions of most binaries for `image_optim` for Linux, Mac OS X, FreeBSD and OpenBSD is by installing [`image_optim_pack`](https://github.com/toy/image_optim_pack) gem.
|
102
102
|
|
103
103
|
Check installation instructions in [Gem installation](#gem-installation) section.
|
104
104
|
|
@@ -241,12 +241,7 @@ image_optim.optimize_images_data(datas)
|
|
241
241
|
|
242
242
|
### From rails
|
243
243
|
|
244
|
-
|
245
|
-
|
246
|
-
You can provide options for image_optim used for preprocessor through config `config.assets.image_optim = {nice: 20, svgo: false}` (ruby1.8 style: `{:nice => 20, :svgo => false}`).
|
247
|
-
Check available options in [options section](#options).
|
248
|
-
|
249
|
-
Image optimization can be time consuming, so depending on your deployment process you may prefer to optimize original asset files.
|
244
|
+
Rails image assets optimization is extracted into [image\_optim\_rails gem](https://github.com/toy/image_optim_rails).
|
250
245
|
|
251
246
|
## Configuration
|
252
247
|
|
data/image_optim.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'image_optim'
|
5
|
-
s.version = '0.
|
5
|
+
s.version = '0.24.0'
|
6
6
|
s.summary = %q{Optimize (lossless compress, optionally lossy) images (jpeg, png, gif, svg) using external utilities (advpng, gifsicle, jhead, jpeg-recompress, jpegoptim, jpegrescan, jpegtran, optipng, pngcrush, pngout, pngquant, svgo)}
|
7
7
|
s.homepage = "http://github.com/toy/#{s.name}"
|
8
8
|
s.authors = ['Ivan Kuchin']
|
@@ -15,6 +15,11 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
16
|
s.require_paths = %w[lib]
|
17
17
|
|
18
|
+
s.post_install_message = <<-EOF
|
19
|
+
Rails image assets optimization is extracted into image_optim_rails gem
|
20
|
+
You can safely remove `config.assets.image_optim = false` if you are not going to use that gem
|
21
|
+
EOF
|
22
|
+
|
18
23
|
s.add_dependency 'fspath', '~> 3.0'
|
19
24
|
s.add_dependency 'image_size', '~> 1.3'
|
20
25
|
s.add_dependency 'exifr', '~> 1.2', '>= 1.2.2'
|
@@ -23,7 +28,7 @@ Gem::Specification.new do |s|
|
|
23
28
|
|
24
29
|
s.add_development_dependency 'image_optim_pack', '~> 0.2', '>= 0.2.2'
|
25
30
|
s.add_development_dependency 'rspec', '~> 3.0'
|
26
|
-
if RUBY_VERSION >= '
|
31
|
+
if RUBY_VERSION >= '2.0'
|
27
32
|
s.add_development_dependency 'rubocop', '~> 0.37'
|
28
33
|
end
|
29
34
|
end
|
data/lib/image_optim.rb
CHANGED
@@ -5,7 +5,6 @@ require 'image_optim/handler'
|
|
5
5
|
require 'image_optim/image_meta'
|
6
6
|
require 'image_optim/optimized_path'
|
7
7
|
require 'image_optim/path'
|
8
|
-
require 'image_optim/railtie' if defined?(Rails)
|
9
8
|
require 'image_optim/worker'
|
10
9
|
require 'in_threads'
|
11
10
|
require 'shellwords'
|
@@ -168,25 +167,43 @@ class ImageOptim
|
|
168
167
|
run_method_for(datas, :optimize_image_data, &block)
|
169
168
|
end
|
170
169
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
170
|
+
class << self
|
171
|
+
# Optimization methods with default options
|
172
|
+
def method_missing(method, *args, &block)
|
173
|
+
if optimize_image_method?(method)
|
174
|
+
new.send(method, *args, &block)
|
175
|
+
else
|
176
|
+
super
|
177
|
+
end
|
177
178
|
end
|
178
|
-
end
|
179
179
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
rescue
|
184
|
-
'DEV'
|
185
|
-
end
|
180
|
+
def respond_to_missing?(method, include_private = false)
|
181
|
+
optimize_image_method?(method) || super
|
182
|
+
end
|
186
183
|
|
187
|
-
|
188
|
-
|
189
|
-
|
184
|
+
if RUBY_VERSION < '1.9'
|
185
|
+
def respond_to?(method, include_private = false)
|
186
|
+
optimize_image_method?(method) || super
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# Version of image_optim gem spec loaded
|
191
|
+
def version
|
192
|
+
Gem.loaded_specs['image_optim'].version.to_s
|
193
|
+
rescue
|
194
|
+
'DEV'
|
195
|
+
end
|
196
|
+
|
197
|
+
# Full version of image_optim
|
198
|
+
def full_version
|
199
|
+
"image_optim v#{version}"
|
200
|
+
end
|
201
|
+
|
202
|
+
private
|
203
|
+
|
204
|
+
def optimize_image_method?(method)
|
205
|
+
method_defined?(method) && method.to_s =~ /^optimize_image/
|
206
|
+
end
|
190
207
|
end
|
191
208
|
|
192
209
|
# Are there workers for file at path?
|
data/lib/image_optim/space.rb
CHANGED
@@ -15,7 +15,7 @@ class ImageOptim
|
|
15
15
|
else
|
16
16
|
log_denominator = Math.log(size.abs) / Math.log(BASE)
|
17
17
|
degree = [log_denominator.floor, SIZE_SYMBOLS.length - 1].min
|
18
|
-
number_string = if degree
|
18
|
+
number_string = if degree.zero?
|
19
19
|
size.to_s
|
20
20
|
else
|
21
21
|
denominator = BASE**degree
|
data/lib/image_optim/worker.rb
CHANGED
@@ -55,4 +55,48 @@ describe ImageOptim::Worker::Optipng do
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
describe '#optimized?' do
|
60
|
+
let(:src){ instance_double(ImageOptim::Path, src_options) }
|
61
|
+
let(:dst){ instance_double(ImageOptim::Path, dst_options) }
|
62
|
+
let(:src_options){ {:size => 10} }
|
63
|
+
let(:dst_options){ {:size? => 9} }
|
64
|
+
let(:instance){ described_class.new(ImageOptim.new, instance_options) }
|
65
|
+
let(:instance_options){ {} }
|
66
|
+
|
67
|
+
subject{ instance.optimized?(src, dst) }
|
68
|
+
|
69
|
+
context 'when interlace option is enabled' do
|
70
|
+
let(:instance_options){ {:interlace => true} }
|
71
|
+
|
72
|
+
context 'when dst is empty' do
|
73
|
+
let(:dst_options){ {:size? => nil} }
|
74
|
+
it{ is_expected.to be_falsy }
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'when dst is not empty' do
|
78
|
+
let(:dst_options){ {:size? => 20} }
|
79
|
+
it{ is_expected.to be_truthy }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when interlace option is disabled' do
|
84
|
+
let(:instance_options){ {:interlace => false} }
|
85
|
+
|
86
|
+
context 'when dst is empty' do
|
87
|
+
let(:dst_options){ {:size? => nil} }
|
88
|
+
it{ is_expected.to be_falsy }
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'when dst is greater than or equal to src' do
|
92
|
+
let(:dst_options){ {:size? => 10} }
|
93
|
+
it{ is_expected.to be_falsy }
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when dst is less than src' do
|
97
|
+
let(:dst_options){ {:size? => 9} }
|
98
|
+
it{ is_expected.to be_truthy }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
58
102
|
end
|
data/spec/image_optim_spec.rb
CHANGED
@@ -227,4 +227,33 @@ describe ImageOptim do
|
|
227
227
|
end
|
228
228
|
end
|
229
229
|
end
|
230
|
+
|
231
|
+
%w[
|
232
|
+
optimize_image
|
233
|
+
optimize_image!
|
234
|
+
optimize_image_data
|
235
|
+
optimize_images
|
236
|
+
optimize_images!
|
237
|
+
optimize_images_data
|
238
|
+
].each do |method|
|
239
|
+
describe ".#{method}" do
|
240
|
+
it 'is checkable by respond_to?' do
|
241
|
+
expect(ImageOptim).to respond_to(method)
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'calls same method on a new ImageOptim instance' do
|
245
|
+
image_optim = instance_double(ImageOptim)
|
246
|
+
method_arg = double
|
247
|
+
method_block = proc{ :x }
|
248
|
+
|
249
|
+
allow(ImageOptim).to receive(:new).and_return(image_optim)
|
250
|
+
expect(image_optim).to receive(method) do |arg, &block|
|
251
|
+
expect(arg).to eq(method_arg)
|
252
|
+
expect(block).to eq(method_block)
|
253
|
+
end
|
254
|
+
|
255
|
+
ImageOptim.send(method, method_arg, &method_block)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
230
259
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -78,12 +78,12 @@ module CapabilityCheckHelpers
|
|
78
78
|
def any_file_modes_allowed?
|
79
79
|
Tempfile.open 'posix' do |f|
|
80
80
|
File.chmod(0, f.path)
|
81
|
-
File.stat(f.path).mode & 0o777
|
81
|
+
(File.stat(f.path).mode & 0o777).zero?
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
85
85
|
def inodes_supported?
|
86
|
-
File.stat(__FILE__).ino
|
86
|
+
!File.stat(__FILE__).ino.zero?
|
87
87
|
end
|
88
88
|
|
89
89
|
def signals_supported?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: image_optim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.24.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Kuchin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fspath
|
@@ -126,20 +126,6 @@ dependencies:
|
|
126
126
|
- - ~>
|
127
127
|
- !ruby/object:Gem::Version
|
128
128
|
version: '3.0'
|
129
|
-
- !ruby/object:Gem::Dependency
|
130
|
-
name: rubocop
|
131
|
-
requirement: !ruby/object:Gem::Requirement
|
132
|
-
requirements:
|
133
|
-
- - ~>
|
134
|
-
- !ruby/object:Gem::Version
|
135
|
-
version: '0.37'
|
136
|
-
type: :development
|
137
|
-
prerelease: false
|
138
|
-
version_requirements: !ruby/object:Gem::Requirement
|
139
|
-
requirements:
|
140
|
-
- - ~>
|
141
|
-
- !ruby/object:Gem::Version
|
142
|
-
version: '0.37'
|
143
129
|
description:
|
144
130
|
email:
|
145
131
|
executables:
|
@@ -178,7 +164,6 @@ files:
|
|
178
164
|
- lib/image_optim/option_definition.rb
|
179
165
|
- lib/image_optim/option_helpers.rb
|
180
166
|
- lib/image_optim/path.rb
|
181
|
-
- lib/image_optim/railtie.rb
|
182
167
|
- lib/image_optim/runner.rb
|
183
168
|
- lib/image_optim/runner/glob_helpers.rb
|
184
169
|
- lib/image_optim/runner/option_parser.rb
|
@@ -216,7 +201,6 @@ files:
|
|
216
201
|
- spec/image_optim/option_definition_spec.rb
|
217
202
|
- spec/image_optim/option_helpers_spec.rb
|
218
203
|
- spec/image_optim/path_spec.rb
|
219
|
-
- spec/image_optim/railtie_spec.rb
|
220
204
|
- spec/image_optim/runner/glob_helpers_spec.rb
|
221
205
|
- spec/image_optim/runner/option_parser_spec.rb
|
222
206
|
- spec/image_optim/space_spec.rb
|
@@ -256,7 +240,13 @@ homepage: http://github.com/toy/image_optim
|
|
256
240
|
licenses:
|
257
241
|
- MIT
|
258
242
|
metadata: {}
|
259
|
-
post_install_message:
|
243
|
+
post_install_message: ! 'Rails image assets optimization is extracted into image_optim_rails
|
244
|
+
gem
|
245
|
+
|
246
|
+
You can safely remove `config.assets.image_optim = false` if you are not going to
|
247
|
+
use that gem
|
248
|
+
|
249
|
+
'
|
260
250
|
rdoc_options: []
|
261
251
|
require_paths:
|
262
252
|
- lib
|
@@ -293,7 +283,6 @@ test_files:
|
|
293
283
|
- spec/image_optim/option_definition_spec.rb
|
294
284
|
- spec/image_optim/option_helpers_spec.rb
|
295
285
|
- spec/image_optim/path_spec.rb
|
296
|
-
- spec/image_optim/railtie_spec.rb
|
297
286
|
- spec/image_optim/runner/glob_helpers_spec.rb
|
298
287
|
- spec/image_optim/runner/option_parser_spec.rb
|
299
288
|
- spec/image_optim/space_spec.rb
|
data/lib/image_optim/railtie.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
require 'image_optim'
|
2
|
-
|
3
|
-
class ImageOptim
|
4
|
-
# Adds image_optim as preprocessor for gif, jpeg, png and svg images
|
5
|
-
class Railtie < Rails::Railtie
|
6
|
-
MIME_TYPES = %w[
|
7
|
-
image/gif
|
8
|
-
image/jpeg
|
9
|
-
image/png
|
10
|
-
image/svg+xml
|
11
|
-
].freeze
|
12
|
-
|
13
|
-
config.before_configuration do |app|
|
14
|
-
worker_names = ImageOptim::Worker.klasses.map(&:bin_sym)
|
15
|
-
app.config.assets.image_optim =
|
16
|
-
ActiveSupport::OrderedOptions.new do |hash, key|
|
17
|
-
if worker_names.include?(key.to_sym)
|
18
|
-
hash[key] = ActiveSupport::OrderedOptions.new
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
initializer 'image_optim.initializer' do |app|
|
24
|
-
next if app.config.assets.compress == false
|
25
|
-
next if app.config.assets.image_optim == false
|
26
|
-
|
27
|
-
@image_optim = ImageOptim.new(options(app))
|
28
|
-
|
29
|
-
register_preprocessor(app) do |*args|
|
30
|
-
if args[1] # context and data arguments in sprockets 2
|
31
|
-
optimize_image_data(args[1])
|
32
|
-
else
|
33
|
-
input = args[0]
|
34
|
-
{
|
35
|
-
:data => optimize_image_data(input[:data]),
|
36
|
-
:charset => nil, # no gzipped version with rails/sprockets#228
|
37
|
-
}
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def options(app)
|
43
|
-
if app.config.assets.image_optim == true
|
44
|
-
{}
|
45
|
-
else
|
46
|
-
app.config.assets.image_optim || {}
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def optimize_image_data(data)
|
51
|
-
@image_optim.optimize_image_data(data) || data
|
52
|
-
end
|
53
|
-
|
54
|
-
def register_preprocessor(app, &processor)
|
55
|
-
MIME_TYPES.each do |mime_type|
|
56
|
-
if app.assets
|
57
|
-
app.assets.register_preprocessor mime_type, :image_optim, &processor
|
58
|
-
else
|
59
|
-
app.config.assets.configure do |env|
|
60
|
-
env.register_preprocessor mime_type, :image_optim, &processor
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,147 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'ImageOptim::Railtie' do
|
4
|
-
require 'rails/all'
|
5
|
-
require 'image_optim/railtie'
|
6
|
-
|
7
|
-
def init_rails_app
|
8
|
-
Class.new(Rails::Application) do
|
9
|
-
# Rails 4 requires application class to have name
|
10
|
-
define_singleton_method :name do
|
11
|
-
'Dummy'
|
12
|
-
end
|
13
|
-
|
14
|
-
config.active_support.deprecation = :stderr
|
15
|
-
config.eager_load = false
|
16
|
-
|
17
|
-
config.logger = Logger.new('/dev/null')
|
18
|
-
|
19
|
-
config.assets.tap do |assets|
|
20
|
-
assets.enabled = true
|
21
|
-
assets.version = '1.0'
|
22
|
-
assets.cache_store = :null_store
|
23
|
-
assets.paths = %w[spec/images]
|
24
|
-
|
25
|
-
assets.delete(:compress)
|
26
|
-
end
|
27
|
-
|
28
|
-
yield config if block_given?
|
29
|
-
end.initialize!
|
30
|
-
end
|
31
|
-
|
32
|
-
after do
|
33
|
-
Rails.application = nil
|
34
|
-
end
|
35
|
-
|
36
|
-
describe 'initialization' do
|
37
|
-
it 'initializes by default' do
|
38
|
-
expect(ImageOptim).to receive(:new)
|
39
|
-
init_rails_app
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'initializes if config.assets.image_optim is nil' do
|
43
|
-
expect(ImageOptim).to receive(:new)
|
44
|
-
init_rails_app do |config|
|
45
|
-
config.assets.image_optim = nil
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'does not initialize if config.assets.image_optim is false' do
|
50
|
-
expect(ImageOptim).not_to receive(:new)
|
51
|
-
init_rails_app do |config|
|
52
|
-
config.assets.image_optim = false
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'does not initialize if config.assets.compress is false' do
|
57
|
-
expect(ImageOptim).not_to receive(:new)
|
58
|
-
init_rails_app do |config|
|
59
|
-
config.assets.compress = false
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe 'options' do
|
64
|
-
it 'initializes with empty hash by default' do
|
65
|
-
expect(ImageOptim).to receive(:new).with({})
|
66
|
-
init_rails_app
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'initializes with empty hash if config.assets.image_optim is true' do
|
70
|
-
expect(ImageOptim).to receive(:new).with({})
|
71
|
-
init_rails_app do |config|
|
72
|
-
config.assets.image_optim = true
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'initializes with empty hash if config.assets.image_optim is nil' do
|
77
|
-
expect(ImageOptim).to receive(:new).with({})
|
78
|
-
init_rails_app do |config|
|
79
|
-
config.assets.image_optim = nil
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'initializes with hash assigned to config.assets.image_optim' do
|
84
|
-
hash = double
|
85
|
-
expect(ImageOptim).to receive(:new).with(hash)
|
86
|
-
init_rails_app do |config|
|
87
|
-
config.assets.image_optim = hash
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'is possible to set individual options' do
|
92
|
-
hash = {:config_paths => 'config/image_optim.yml'}
|
93
|
-
expect(ImageOptim).to receive(:new).with(hash)
|
94
|
-
init_rails_app do |config|
|
95
|
-
config.assets.image_optim.config_paths = 'config/image_optim.yml'
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'is possible to set individual worker options' do
|
100
|
-
hash = {:advpng => {:level => 3}}
|
101
|
-
expect(ImageOptim).to receive(:new).with(hash)
|
102
|
-
init_rails_app do |config|
|
103
|
-
expect(config.assets.image_optim.advpng).to eq({})
|
104
|
-
config.assets.image_optim.advpng.level = 3
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'is not possible to set unknown worker options' do
|
109
|
-
expect(ImageOptim).to receive(:new).with({})
|
110
|
-
init_rails_app do |config|
|
111
|
-
expect(config.assets.image_optim.unknown).to eq(nil)
|
112
|
-
expect do
|
113
|
-
config.assets.image_optim.unknown.level = 3
|
114
|
-
end.to raise_error(NoMethodError)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
describe 'optimizing assets' do
|
121
|
-
before do
|
122
|
-
stub_const('Path', ImageOptim::Path)
|
123
|
-
end
|
124
|
-
|
125
|
-
%w[
|
126
|
-
icecream.gif
|
127
|
-
lena.jpg
|
128
|
-
rails.png
|
129
|
-
test.svg
|
130
|
-
].each do |asset_name|
|
131
|
-
it "optimizes #{asset_name}" do
|
132
|
-
asset = init_rails_app.assets.find_asset(asset_name)
|
133
|
-
|
134
|
-
asset_data = asset.source
|
135
|
-
original = Path.convert(asset.pathname)
|
136
|
-
|
137
|
-
expect(asset_data).to be_smaller_than(original)
|
138
|
-
|
139
|
-
Path.temp_file_path %W[spec .#{original.image_format}] do |temp|
|
140
|
-
temp.binwrite(asset_data)
|
141
|
-
|
142
|
-
expect(temp).to be_similar_to(original, 0)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end if ENV['RAILS_VERSION']
|