retina_rails 1.0.4 → 2.0.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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -3
  3. data/.travis.yml +16 -4
  4. data/README.md +57 -29
  5. data/UPGRADING +7 -0
  6. data/UPGRADING.md +60 -0
  7. data/lib/retina_rails.rb +11 -7
  8. data/lib/retina_rails/extensions.rb +16 -0
  9. data/lib/retina_rails/extensions/carrierwave.rb +25 -0
  10. data/lib/retina_rails/extensions/paperclip.rb +23 -0
  11. data/lib/retina_rails/helpers.rb +42 -12
  12. data/lib/retina_rails/processors.rb +14 -0
  13. data/lib/retina_rails/processors/carrierwave.rb +49 -0
  14. data/lib/retina_rails/processors/paperclip.rb +30 -0
  15. data/lib/retina_rails/strategies.rb +6 -7
  16. data/lib/retina_rails/strategies/carrierwave.rb +56 -50
  17. data/lib/retina_rails/strategies/paperclip.rb +53 -55
  18. data/lib/retina_rails/version.rb +1 -1
  19. data/retina_rails.gemspec +11 -4
  20. data/spec/fixtures/db/retina_rails.sqlite3 +0 -0
  21. data/spec/helpers_spec.rb +87 -13
  22. data/spec/spec_helper.rb +10 -4
  23. data/spec/strategies/carrierwave_spec.rb +117 -154
  24. data/spec/strategies/paperclip_spec.rb +77 -104
  25. data/spec/support/carrierwave.rb +15 -0
  26. data/spec/support/file_string_io.rb +2 -0
  27. data/spec/support/paperclip.rb +9 -0
  28. data/spec/support/rails.rb +0 -11
  29. data/spec/support/schema.rb +8 -2
  30. metadata +53 -52
  31. data/lib/retina_rails/deprecation/carrierwave.rb +0 -23
  32. data/lib/retina_rails/deprecation/paperclip.rb +0 -23
  33. data/lib/retina_rails/exception.rb +0 -15
  34. data/spec/deprecation/carrierwave_spec.rb +0 -13
  35. data/spec/deprecation/paperclip_spec.rb +0 -14
  36. data/spec/fixtures/images/avatar.with.dots.jpeg +0 -0
  37. data/spec/fixtures/manifest.yml +0 -10
  38. data/vendor/assets/javascripts/retina.js +0 -113
@@ -1,35 +1,109 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe ActionView::Helpers::AssetTagHelper, :type => :helper do
3
+ class Image
4
4
 
5
- class SomeWrapperClassForUrl
6
- def to_s
7
- 'image.png'
8
- end
5
+ def url(version)
6
+ '/some_image.png'
7
+ end
8
+
9
+ end
10
+
11
+ class Upload
12
+
13
+ attr_accessor :avatar
14
+
15
+ def initialize
16
+ self.avatar = Image.new
17
+ end
18
+
19
+ def retina_dimensions
20
+ {
21
+ :avatar => {
22
+ :small => {
23
+ :width => 40,
24
+ :height => 30
25
+ }
26
+ }
27
+ }
9
28
  end
10
29
 
30
+ end
31
+
32
+ describe ActionView::Helpers::AssetTagHelper, :type => :helper do
33
+
11
34
  subject { helper }
12
35
 
13
- describe :image_tag do
36
+ describe '#retina_image_tag' do
37
+
38
+ context 'with dimensions present' do
39
+
40
+ it 'should set correct width and height' do
41
+ image = helper.retina_image_tag(Upload.new, :avatar, :small)
42
+
43
+ image.should include('width="40"')
44
+ image.should include('height="30"')
45
+ end
46
+
47
+ it 'should be able to add a class' do
48
+ image = helper.retina_image_tag(Upload.new, :avatar, :small, :class => 'foo')
49
+ image.should include('class="foo"')
50
+ end
51
+
52
+ end
53
+
54
+ context 'without dimensions present' do
55
+
56
+ before(:each) { Upload.any_instance.stub(:retina_dimensions).and_return(nil) }
14
57
 
15
- context 'with retina option' do
58
+ it 'should set correct width and height' do
59
+ image = helper.retina_image_tag(Upload.new, :avatar, :small, :default => { :width => 25, :height => 40 })
16
60
 
17
- it { subject.image_tag('image.png', :retina => true).should == '<img alt="Image" data-at2x="/assets/image@2x-0842b16379ded9ddcc299912621f76bc.png" src="/assets/image-e8de7f87c2b9d08490575267a4c9eddc.png" />' }
61
+ image.should include('width="25"')
62
+ image.should include('height="40"')
18
63
 
19
- it { subject.image_tag('image.some.png', :retina => true).should == '<img alt="Image.some" data-at2x="/assets/image.some@2x-0842b16379ded9ddcc299912621f76bc.png" src="/assets/image.some-e8de7f87c2b9d08490575267a4c9eddc.png" />' }
64
+ image = helper.retina_image_tag(Upload.new, :avatar, :small, :default => [25, 40])
20
65
 
21
- context 'with class' do
66
+ image.should include('width="25"')
67
+ image.should include('height="40"')
68
+ end
69
+
70
+ it 'should set no height and width if no defaults present' do
71
+ image = helper.retina_image_tag(Upload.new, :avatar, :small)
72
+
73
+ image.should_not include('width')
74
+ image.should_not include('height')
75
+ end
22
76
 
23
- it { subject.image_tag(SomeWrapperClassForUrl.new, :retina => true).should == '<img alt="Image" data-at2x="/assets/image@2x-0842b16379ded9ddcc299912621f76bc.png" src="/assets/image-e8de7f87c2b9d08490575267a4c9eddc.png" />' }
77
+ it 'should be able to add a class' do
78
+ image = helper.retina_image_tag(Upload.new, :avatar, :small, :default => { :width => 25, :height => 40 }, :class => 'foo')
24
79
 
80
+ image.should include('class="foo"')
81
+ end
82
+
83
+ it 'should strip default attributes' do
84
+ image = helper.retina_image_tag(Upload.new, :avatar, :small, :default => { :width => 25, :height => 40 })
85
+
86
+ image.should_not include('default')
25
87
  end
26
88
 
27
89
  end
28
90
 
29
- context 'without retina' do
91
+ end
92
+
93
+ describe '#image_tag' do
94
+
95
+ it 'should show a deprecation warning when used with retina option' do
96
+ ActiveSupport::Deprecation.should_receive(:warn)
97
+ .with("`image_tag('image.png', :retina => true)` is deprecated use `retina_image_tag` instead")
98
+
99
+ image_tag('image.png', :retina => true)
100
+ end
30
101
 
31
- it { subject.image_tag('image.png').should == '<img alt="Image" src="/assets/image-e8de7f87c2b9d08490575267a4c9eddc.png" />' }
102
+ it 'should not show a deprecation warning when used without retina option' do
103
+ ActiveSupport::Deprecation.should_not_receive(:warn)
104
+ .with("`image_tag('image.png', :retina => true)` is deprecated use `retina_image_tag` instead")
32
105
 
106
+ image_tag('image.png')
33
107
  end
34
108
 
35
109
  end
@@ -25,20 +25,26 @@ ActiveRecord::Base.establish_connection(
25
25
  :database => File.dirname(__FILE__) + "/fixtures/db/retina_rails.sqlite3"
26
26
  )
27
27
 
28
- ## Load support files
29
-
30
- Dir["spec/support/**/*.rb"].each { |f| load f }
31
-
32
28
  ## Load rspec rails after initializing rails app
33
29
 
30
+ load 'spec/support/rails.rb'
31
+ load 'spec/support/schema.rb'
34
32
  require 'rspec/rails'
35
33
 
36
34
  ## Load retina_rails
37
35
 
38
36
  require 'retina_rails'
39
37
 
38
+ RetinaRails::Extensions.include_extensions
39
+ RetinaRails::Processors.include_processors
40
40
  RetinaRails::Strategies.include_strategies
41
41
 
42
+ ## Load support files
43
+
44
+ load 'spec/support/carrierwave.rb'
45
+ load 'spec/support/paperclip.rb'
46
+ load 'spec/support/file_string_io.rb'
47
+
42
48
  RSpec.configure do |config|
43
49
  config.fixture_path = "#{File.dirname(__FILE__)}/fixtures"
44
50
  end
@@ -1,207 +1,170 @@
1
- require 'stringio'
2
1
  require 'spec_helper'
3
2
 
4
- class AnonymousUploader < CarrierWave::Uploader::Base
5
-
6
- include CarrierWave::RMagick
7
-
8
- retina!
9
-
10
- version :small do
11
- process :resize_to_fill => [30, 30]
12
- process :quality => 60
13
- process :retina_quality => 20
14
- end
15
-
16
- version :small_without_quality do
17
- process :resize_to_fill => [30, 30]
18
- end
19
-
20
- version :small_without_dimensions do
21
- process :desaturate
22
- end
23
-
24
- version :small_multiple_processors do
25
- process :resize_to_fill => [30, 30]
26
- process :desaturate
27
- end
28
-
29
- version :small_without_retina, :retina => false do
30
- process :quality => 60
31
- end
32
-
33
- version :small_custom_processor, :retina => false do
34
- process :resize_to_fill_with_gravity => [100, 100, 'North', :jpg, 75]
35
- end
36
-
37
- version :small_custom_processor_retina, :retina => false do
38
- process :resize_to_fill_with_gravity => [200, 200, 'North', :jpg, 40]
39
- end
40
-
41
- version :small_with_failing_conditional, :if => ->(img, opts) { false } do
42
- process :resize_to_fill => [30, 30]
43
- end
44
-
45
- def desaturate
46
- manipulate! do |img|
47
- img = img.quantize 256, Magick::GRAYColorspace
48
- end
49
- end
50
-
51
- def resize_to_fill_with_gravity(width, height, gravity, format, quality); end
52
-
53
- def quality(percentage)
54
- manipulate! do |img|
55
- img.write(current_path) { self.quality = percentage } unless img.quality == percentage
56
- img = yield(img) if block_given?
57
- img
58
- end
59
- end
60
-
61
- version :small_without_processor
62
-
63
- end
64
-
65
- class CarrierWaveUpload
66
-
67
- extend CarrierWave::Mount
68
-
69
- attr_accessor :avatar
70
- attr_accessor :id
71
-
72
- mount_uploader :avatar, AnonymousUploader
73
-
74
- def initialize
75
- self.id = 999
76
- end
77
-
78
- end
79
-
80
3
  describe RetinaRails::Strategies::CarrierWave do
81
4
 
82
5
  include CarrierWave::Test::Matchers
83
6
 
84
- subject { AnonymousUploader }
85
-
86
- before do
7
+ ##
8
+ # Store image so we can run tests against it
9
+ #
10
+ def upload!
87
11
  AnonymousUploader.enable_processing = true
88
12
  @uploader = AnonymousUploader.new(CarrierWaveUpload.new, :avatar)
89
13
  @uploader.store!(File.open("#{fixture_path}/images/avatar.jpeg"))
90
14
  end
91
15
 
92
- after do
16
+ ##
17
+ # Remove image after testing
18
+ #
19
+ after(:each) do
93
20
  AnonymousUploader.enable_processing = false
94
21
  @uploader.remove!
22
+ AnonymousUploader.versions[:small][:uploader].processors = [] ## Reset processors
95
23
  end
96
24
 
25
+ ##
26
+ # Actual tests
27
+ #
97
28
  context 'with dimensions processor' do
98
29
 
99
- its(:versions) { should include :small_retina }
100
-
101
- it { @uploader.small.should have_dimensions(30, 30) }
102
- it { @uploader.small_retina.should have_dimensions(60, 60) }
103
-
104
- it { File.basename(@uploader.small.current_path, 'jpeg').should include 'small_'}
105
-
106
- it { File.basename(@uploader.small_retina.current_path, 'jpeg').should include '@2x'}
107
- it { File.basename(@uploader.small_retina.current_path, 'jpeg').should_not include 'retina_'}
108
-
109
- end
110
-
111
- context 'with quality processor' do
112
-
113
- it { Magick::Image.read(@uploader.small.current_path).first.quality.should == 60 }
114
-
115
- it { Magick::Image.read(@uploader.small_retina.current_path).first.quality.should == 20 }
116
-
117
- end
118
-
119
- context 'without quality processor' do
120
-
121
- it { Magick::Image.read(@uploader.small_without_quality.current_path).first.quality.should == 84 }
122
-
123
- it { Magick::Image.read(@uploader.small_without_quality_retina.current_path).first.quality.should == 40 }
30
+ ##
31
+ # Setup Anonymous uploader with a resize processor
32
+ #
33
+ before(:each) do
34
+ AnonymousUploader.class_eval do
35
+ version :small do
36
+ process :resize_to_fill => [30, 40]
37
+ end
38
+ end
39
+ upload!
40
+ end
124
41
 
125
- end
42
+ it 'should double the height and width of an image' do
43
+ @uploader.small.should have_dimensions(60, 80)
44
+ end
126
45
 
127
- context 'without dimensions processor' do
46
+ it 'should store original width and height attributes for version' do
47
+ @uploader.model.retina_dimensions[:avatar][:small].should == { :width => 30, :height => 40 }
48
+ end
128
49
 
129
- its(:versions) { should_not include :small_without_dimensions_retina }
50
+ it "should set quality to it's default 60%" do
51
+ quality = Magick::Image.read(@uploader.small.current_path).first.quality
52
+ quality.should == 60
53
+ end
130
54
 
131
55
  end
132
56
 
133
- context 'with multiple processors' do
134
-
135
- its(:versions) { should include :small_multiple_processors_retina }
57
+ context 'override quality' do
136
58
 
137
- it { subject.versions[:small_multiple_processors][:uploader].processors.should include([:desaturate, [], nil]) }
138
-
139
- it { @uploader.small_multiple_processors.should have_dimensions(30, 30) }
140
- it { @uploader.small_multiple_processors_retina.should have_dimensions(60, 60) }
141
-
142
- it { File.basename(@uploader.small_multiple_processors.current_path, 'jpeg').should include 'small_'}
143
-
144
- it { File.basename(@uploader.small_multiple_processors_retina.current_path, 'jpeg').should include '@2x'}
145
- it { File.basename(@uploader.small_multiple_processors_retina.current_path, 'jpeg').should_not include 'retina_'}
59
+ ##
60
+ # Setup Anonymous uploader with a resize processor and override quality
61
+ #
62
+ before(:each) do
63
+ AnonymousUploader.class_eval do
64
+ version :small do
65
+ process :resize_to_fill => [30, 40]
66
+ process :retina_quality => 80
67
+ end
68
+ end
69
+ end
146
70
 
147
- end
71
+ it "should override quality" do
72
+ upload!
73
+ quality = Magick::Image.read(@uploader.small.current_path).first.quality
74
+ quality.should == 80
75
+ end
148
76
 
149
- context 'without processor' do
77
+ it 'should receive quality processor once' do
78
+ AnonymousUploader.any_instance.should_receive(:retina_quality).once
150
79
 
151
- its(:versions) { should include :small_without_processor }
152
- its(:versions) { should_not include :small_without_processor_retina }
80
+ upload!
81
+ end
153
82
 
154
83
  end
155
84
 
156
- context 'with failing conditional version' do
85
+ context 'multiple processors' do
157
86
 
158
- it { @uploader.version_exists?(:small_with_failing_conditional).should == false }
159
- it { @uploader.small_with_failing_conditional.current_path.should_not be_present }
87
+ ##
88
+ # Setup Anonymous uploader with a custom processor
89
+ #
90
+ before(:each) do
91
+ AnonymousUploader.class_eval do
92
+ version :small do
93
+ process :resize_to_fill => [30, 40]
94
+ process :desaturate
95
+ end
160
96
 
161
- end
97
+ def desaturate
98
+ manipulate! do |img|
99
+ img = img.quantize 256, Magick::GRAYColorspace
100
+ end
101
+ end
102
+ end
162
103
 
163
- context 'file with multiple dots' do
104
+ upload!
105
+ end
164
106
 
165
- before do
166
- AnonymousUploader.enable_processing = true
167
- @uploader = AnonymousUploader.new(CarrierWaveUpload.new, :avatar)
168
- @uploader.store!(File.open("#{fixture_path}/images/avatar.with.dots.jpeg"))
107
+ it 'should double the height and width of an image' do
108
+ @uploader.small.should have_dimensions(60, 80)
169
109
  end
170
110
 
171
- it { File.basename(@uploader.small.current_path, 'jpeg').should == 'small_avatar.with.dots.' }
172
- it { File.basename(@uploader.small_retina.current_path, 'jpeg').should == 'small_avatar.with.dots@2x.' }
111
+ it 'should store original width and height attributes for version' do
112
+ @uploader.model.retina_dimensions[:avatar][:small].should == { :width => 30, :height => 40 }
113
+ end
173
114
 
174
115
  end
175
116
 
176
- context 'without retina' do
117
+ context 'with custom resize processor' do
177
118
 
178
- its(:versions) { should_not include :small_without_retina_retina }
119
+ ##
120
+ # Setup Anonymous uploader with a custom resize processor
121
+ #
122
+ before(:each) do
123
+ AnonymousUploader.class_eval do
124
+ version :small, :retina => false do
125
+ process :custom_resize => [200, 200]
126
+ process :store_retina_dimensions
127
+ end
179
128
 
180
- end
129
+ def custom_resize(width, height)
130
+ manipulate! do |img|
131
+ img.resize_to_fill!(width, height)
132
+ end
133
+ end
134
+ end
181
135
 
182
- context 'custom processor' do
183
-
184
- its(:versions) { should_not include :small_custom_processor_retina_retina }
136
+ upload!
137
+ end
185
138
 
186
- it { File.basename(@uploader.small_custom_processor.current_path, 'jpeg').should include 'small_custom_processor_'}
139
+ it 'should double the height and width of an image' do
140
+ @uploader.small.should have_dimensions(200, 200)
141
+ end
187
142
 
188
- it { File.basename(@uploader.small_custom_processor_retina.current_path, 'jpeg').should include '@2x'}
189
- it { File.basename(@uploader.small_custom_processor_retina.current_path, 'jpeg').should_not include 'retina_'}
143
+ it 'should store original width and height attributes for version' do
144
+ @uploader.model.retina_dimensions[:avatar][:small].should == { :width => 100, :height => 100 }
145
+ end
190
146
 
191
147
  end
192
148
 
193
- context 'file without extension name' do
149
+ context 'with failing conditional version' do
194
150
 
195
- before do
196
- AnonymousUploader.enable_processing = true
197
- @uploader = AnonymousUploader.new(CarrierWaveUpload.new, :avatar)
198
- stream = FileStringIO.new('avatar', File.read("#{fixture_path}/images/avatar.jpeg"))
199
- @uploader.store!(stream)
151
+ ##
152
+ # Setup Anonymous uploader with a failing condition
153
+ #
154
+ before(:each) do
155
+ AnonymousUploader.class_eval do
156
+ version :small_conditional, :if => ->(img, opts) { false } do
157
+ process :resize_to_fill => [30, 30]
158
+ end
159
+ end
160
+
161
+ upload!
200
162
  end
201
163
 
202
- it { File.basename(@uploader.small.current_path, 'jpeg').should include 'small_'}
203
- it { File.basename(@uploader.small_retina.current_path, 'jpeg').should include '@2x'}
204
- it { File.basename(@uploader.small_retina.current_path, 'jpeg').should_not include 'retina_'}
164
+ it 'should not create a version' do
165
+ @uploader.version_exists?(:small_conditional).should be_false
166
+ @uploader.small_conditional.current_path.should_not be_present
167
+ end
205
168
 
206
169
  end
207
170