artwork 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 706406a5c99a2c2c08fbf910749cc250725a88b7
4
- data.tar.gz: 3b555e730a0aaad2708b0c75f1d78780fcacf3ea
3
+ metadata.gz: 8ebd31f20d2755cc9c814519721157b8381e4d31
4
+ data.tar.gz: e302263d9af85029c512d50c0100557ff8c0a100
5
5
  SHA512:
6
- metadata.gz: 8cc3e0a95a39eb4c175d4572603a7371d63d3e6ae7fce2a547683d88a4d7718d477a0335e618e01e37ac6567b3088d7250d90ef47964dd13ba9b46ff09864c80
7
- data.tar.gz: 7070f597ab0d885c94e9249ad56e3867b2b97df0fb4c444b33f978684f9bc9ded1eb34acc53996a8dcac1554b61faf554f84cf224e0092ab26da0e91f0aff98b
6
+ metadata.gz: 4223d517c83f899f61485a2e1ad5b0b52a970fc97736765d92d20beb9faef8e2377246459b98e01fffe3475896e3c4956a6fd4ae66ff3ca89e2a19c87fd1b24a
7
+ data.tar.gz: f897de740bc861b0b8559e89641e3cc086a6b7d4de2793609c162c2d28c7c3ea6ed79676a8e7656f47eb3aa7f9aae314571f39aaf82c378a59ae4d76d1c5cd23
@@ -1,3 +1,8 @@
1
+ ## v0.7.3
2
+
3
+ - Paperclip is no longer a dependency
4
+ - Change a little the `auto_height` option of `artwork_tag`
5
+
1
6
  ## v0.7.2
2
7
 
3
8
  - Use `Artwork.current_resolution` instead of `Artwork.actual_resolution` in
data/README.md CHANGED
@@ -4,8 +4,7 @@ The Artwork gem provides simple server-side responsive images support for Rails
4
4
  which is similar in concept to the `<picture>` tag spec but requires no
5
5
  JavaScript, doesn't make extra requests and works in all browsers.
6
6
 
7
- Currently it supports only Paperclip attachments, but that can be changed
8
- failry easily.
7
+ You could use it either with Paperclip attachments or implement a couple of methods yourself in plain Ruby.
9
8
 
10
9
  The gem should be thread-safe and should work with Rails 2.3 or newer.
11
10
 
@@ -50,9 +49,10 @@ you have a _2x versions of your thumbs, the helper will choose the _2x one.
50
49
 
51
50
  - Ruby 1.8.7 or newer
52
51
  - Rails 2.3 or newer
53
- - Paperclip 2.3 or newer
54
52
  - A JavaScript runtime
55
53
 
54
+ If you're using Paperclip, it should be 2.3 or newer.
55
+
56
56
  ## Installation
57
57
 
58
58
  Add these lines to your application's Gemfile:
@@ -109,7 +109,7 @@ Set the following variables in an app initializer:
109
109
  - `Artwork.supported_resolutions_list`
110
110
  - `Artwork.base_resolution`
111
111
 
112
- Name your Paperclip attachment styles using the following convention:
112
+ Name your attachment styles using the following convention:
113
113
 
114
114
  :'320x'
115
115
  :'320x_2x'
@@ -125,6 +125,69 @@ Name your Paperclip attachment styles using the following convention:
125
125
  The artwork methods will recognize and work with these styles. All other naming
126
126
  conventions will be ignored and will bypass the artwork auto-sizing logic.
127
127
 
128
+ ### Plain Ruby Model
129
+
130
+ A plain Ruby model will look like this:
131
+ [example in the specs](spec/examples/model_without_paperclip_spec.rb)
132
+
133
+ ```ruby
134
+ class User
135
+ include Artwork::Model
136
+
137
+ def attachment_styles_for(attachment_name)
138
+ if attachment_name.to_sym == :avatar
139
+ [
140
+ :'320x',
141
+ :'320x_2x',
142
+ :'320x_some_label',
143
+ :'320x_some_label_2x',
144
+ :'320x500',
145
+ :'320x500_2x',
146
+ :'320x500_crop',
147
+ :'320x500_crop_2x',
148
+ :'320x500_black_and_white_crop',
149
+ :'320x500_black_and_white_crop_2x',
150
+ ]
151
+ end
152
+ end
153
+
154
+ def avatar
155
+ Avatar.new
156
+ end
157
+ end
158
+
159
+ class Avatar
160
+ def url(style, options)
161
+ "/avatars/avatar-#{style}.jpg"
162
+ end
163
+ end
164
+ ```
165
+
166
+ ### ActiveRecord Model with Paperclip
167
+
168
+ An ActiveRecord Model with Paperclip will look like this:
169
+ [example in the specs](spec/examples/model_with_paperclip_spec.rb)
170
+
171
+ ```ruby
172
+ class UserWithPaperclip < ActiveRecord::Base
173
+ include Artwork::Model
174
+
175
+ has_attached_file :avatar,
176
+ path: '/tmp/avatars/:basename-:style.:extension',
177
+ url: '/avatars/:basename-:style.:extension',
178
+ styles: {
179
+ :'320x' => '320x>',
180
+ :'320x_2x' => '640x>',
181
+ :'320x_some_label' => '320x>',
182
+ :'320x_some_label_2x' => '640x>',
183
+ :'320x500' => '320x500>',
184
+ :'320x500_2x' => '640x1000>',
185
+ :'320x500_crop' => '320x500#',
186
+ :'320x500_crop_2x' => '640x1000#',
187
+ }
188
+ end
189
+ ```
190
+
128
191
  ## Usage Example
129
192
 
130
193
  Configure the gem by putting the following code in `config/initializers/artwork.rb`:
@@ -21,8 +21,9 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'bundler', '~> 1.6'
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'rspec', '~> 3'
24
+ spec.add_development_dependency 'paperclip', '>= 2.3'
25
+ spec.add_development_dependency 'sqlite3'
24
26
 
25
27
  spec.add_dependency 'rails', '>= 2.3'
26
- spec.add_dependency 'paperclip', '>= 2.3'
27
28
  spec.add_dependency 'uglifier'
28
29
  end
@@ -1,3 +1,3 @@
1
1
  module Artwork
2
- VERSION = '0.7.2'
2
+ VERSION = '0.7.3'
3
3
  end
@@ -15,10 +15,18 @@ module Artwork
15
15
  image_url = record.artwork_url attachment_name, size, options
16
16
 
17
17
  if options[:auto_height]
18
- image = record.send(attachment_name)
18
+ if options[:auto_height].respond_to?(:first)
19
+ image_width = options[:auto_height].first
20
+ image_height = options[:auto_height].last
21
+ else
22
+ image = record.send(attachment_name)
23
+
24
+ image_width = image.width
25
+ image_height = image.height
26
+ end
19
27
 
20
- if image.height.present? and image.width.present?
21
- padding = ((image.height.to_f / image.width) * 100).round(4)
28
+ if image_width.present? and image_height.present?
29
+ padding = ((image_height.to_f / image_width) * 100).round(4)
22
30
 
23
31
  img_holder_options[:style] = "padding-bottom:#{padding}%;"
24
32
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+ require 'paperclip'
4
+
5
+ ActiveRecord::Base.raise_in_transactional_callbacks = true
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
8
+ ActiveRecord::Schema.define do
9
+ self.verbose = false
10
+
11
+ create_table :user_with_paperclips, :force => true do |t|
12
+ t.string :avatar_file_name
13
+ t.string :avatar_content_type
14
+ t.integer :avatar_file_size
15
+ t.datetime :avatar_updated_at
16
+ end
17
+ end
18
+
19
+ describe 'Model with paperclip', :type => :model do
20
+ class UserWithPaperclip < ActiveRecord::Base
21
+ include Paperclip::Glue
22
+ include Artwork::Model
23
+
24
+ has_attached_file :avatar,
25
+ :path => ':basename-:style.:extension',
26
+ :url => '/:basename-:style.:extension',
27
+ :styles => ModelSpec::IMAGE_STYLES
28
+ end
29
+
30
+ it_behaves_like 'an artwork model' do
31
+ let(:model) { UserWithPaperclip }
32
+ let(:instance) do
33
+ UserWithPaperclip.find_or_create_by(
34
+ avatar_file_name: 'avatar.jpg',
35
+ avatar_content_type: 'image/jpeg',
36
+ avatar_file_size: 3000,
37
+ avatar_updated_at: 1.day.ago
38
+ )
39
+ end
40
+ let(:attachment_name) { :avatar }
41
+ end
42
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Model without paperclip' do
4
+ class UserWithoutPaperclip
5
+ include Artwork::Model
6
+
7
+ def attachment_styles_for(attachment_name)
8
+ if attachment_name.to_sym == :avatar
9
+ ModelSpec::IMAGE_STYLES.keys
10
+ end
11
+ end
12
+
13
+ def avatar
14
+ Avatar.new
15
+ end
16
+ end
17
+
18
+ class Avatar
19
+ def url(style, options)
20
+ "/avatar-#{style}.jpg"
21
+ end
22
+ end
23
+
24
+ it_behaves_like 'an artwork model' do
25
+ let(:model) { UserWithoutPaperclip }
26
+ let(:instance) { UserWithoutPaperclip.new }
27
+ let(:attachment_name) { :avatar }
28
+ end
29
+
30
+ # if you want to use artwork_tag like this:
31
+ # <%= artwork_tag record, :avatar, '320x', auto_height: true %>
32
+ # you will have to provide Avatar#width and Avatar#height which will be used to generate
33
+ # the padding-bottom % of the .img-holder
34
+ #
35
+ # n.b. you could use the artwork_tag also like this:
36
+ # <%= artwork_tag record, :avatar, '320x', auto_height: [image_width, image_height] %>
37
+ end
@@ -0,0 +1,263 @@
1
+ module ModelSpec
2
+ IMAGE_STYLES = {
3
+ :'320x' => '320x>',
4
+ :'320x_2x' => '640x>',
5
+ :'640x_2x' => '1280x>',
6
+ :'640x' => '640x>',
7
+ :'1280x' => '1280x>',
8
+ :'1280x_2x' => '2560x>',
9
+ :'2000x' => '2000x>',
10
+ :'1500x_2x' => '3000x>',
11
+ :'320x_some_label' => '320x>',
12
+ :'320x_some_label_2x' => '640x>',
13
+ :'320x500' => '320x500>',
14
+ :'320x500_2x' => '640x1000>',
15
+ :'320x500_crop' => '320x500#',
16
+ :'320x500_crop_2x' => '640x1000#',
17
+ :'400x500' => '400x500>',
18
+ :'400x500_2x' => '800x1000>',
19
+ :'320x_' => '320x>',
20
+ :unsupported => '100x100>'
21
+ }
22
+ end
23
+
24
+ #
25
+ # To use this example you have to provide
26
+ # let (:model) { ... }
27
+ # let (:instance) { ... }
28
+ # let (:attachment_name) { ... }
29
+ #
30
+ # also the attachment class should have an url method that receives (style, options)
31
+ # and should return "/#{attachment_name}-#{style}.jpg"
32
+ #
33
+ RSpec.shared_examples 'an artwork model' do
34
+ describe '#attachment_styles_for' do
35
+ it 'returns the list of available thumbnails' do
36
+ expect(instance.attachment_styles_for(attachment_name)).to match_array [
37
+ :'320x',
38
+ :'320x_2x',
39
+ :'640x_2x',
40
+ :'640x',
41
+ :'1280x',
42
+ :'1280x_2x',
43
+ :'2000x',
44
+ :'1500x_2x',
45
+ :'320x_some_label',
46
+ :'320x_some_label_2x',
47
+ :'320x500',
48
+ :'320x500_2x',
49
+ :'320x500_crop',
50
+ :'320x500_crop_2x',
51
+ :'400x500',
52
+ :'400x500_2x',
53
+ :'320x_',
54
+ :unsupported,
55
+ ]
56
+ end
57
+ end
58
+
59
+ describe '#artwork_url' do
60
+ describe 'behaviour' do
61
+ it 'returns the computed url of an attachment by delegating to artwork_thumb_for' do
62
+ expect(instance).to receive(:artwork_thumb_for).with(:photo, :size, 'options').and_return(:computed_size)
63
+
64
+ attachment = double
65
+ expect(attachment).to receive(:url).with(:computed_size, 'options').and_return 'some/url'
66
+ expect(instance).to receive(:photo).and_return(attachment)
67
+
68
+ expect(instance.artwork_url(:photo, :size, 'options')).to eq 'some/url'
69
+ end
70
+
71
+ it 'works with two arguments and a hash options' do
72
+ expect(instance).to receive(:artwork_thumb_for).with(:photo, :size, :some => 'options').and_return(:computed_size)
73
+
74
+ attachment = double
75
+ expect(attachment).to receive(:url).with(:computed_size, :some => 'options').and_return 'some/url'
76
+ expect(instance).to receive(:photo).and_return(attachment)
77
+
78
+ expect(instance.artwork_url(:photo, :size, :some => 'options')).to eq 'some/url'
79
+ end
80
+
81
+ it 'works with two arguments only without any options hash' do
82
+ expect(instance).to receive(:artwork_thumb_for).with(:photo, :size, {}).and_return(:computed_size)
83
+
84
+ attachment = double
85
+ expect(attachment).to receive(:url).with(:computed_size, {}).and_return 'some/url'
86
+ expect(instance).to receive(:photo).and_return(attachment)
87
+
88
+ expect(instance.artwork_url(:photo, :size)).to eq 'some/url'
89
+ end
90
+ end
91
+
92
+ describe 'with real attachment' do
93
+ before :each do
94
+ Artwork.base_resolution = 1000
95
+ Artwork.current_resolution = 1000
96
+ Artwork.actual_resolution = 1000
97
+ Artwork.load_2x_images = false
98
+ end
99
+
100
+ it 'will build an url' do
101
+ expect(instance.artwork_url(attachment_name, '320x')).to start_with '/avatar-320x.jpg'
102
+ expect(instance.artwork_url(attachment_name, '320x', {1000 => '1x@2'})).to start_with '/avatar-640x.jpg'
103
+
104
+ Artwork.load_2x_images = true
105
+ expect(instance.artwork_url(attachment_name, '320x')).to start_with '/avatar-320x_2x.jpg'
106
+ end
107
+ end
108
+ end
109
+
110
+ describe '#artwork_thumb_for' do
111
+ before :each do
112
+ Artwork.base_resolution = 1000
113
+ Artwork.current_resolution = 1000
114
+ Artwork.actual_resolution = 1000
115
+ Artwork.load_2x_images = false
116
+ end
117
+
118
+ def expect_thumb(size, expected)
119
+ expect(instance.artwork_thumb_for(attachment_name, *Array(size))).to eq expected
120
+ end
121
+
122
+ it 'picks the exact requested size if it exists' do
123
+ expect_thumb '2000x', :'2000x'
124
+ end
125
+
126
+ it 'accepts sizes passed as both a symbol or a string' do
127
+ expect_thumb '2000x', :'2000x'
128
+ expect_thumb :'2000x', :'2000x'
129
+ end
130
+
131
+ it 'scales the required size according to current_resolution' do
132
+ Artwork.base_resolution = 1000
133
+ Artwork.current_resolution = 2000
134
+
135
+ expect_thumb '1000x', :'2000x'
136
+ expect_thumb '640x', :'1280x'
137
+ end
138
+
139
+ it 'ignores the retina thumbs when looking for a given size' do
140
+ expect_thumb '1500x', :'2000x'
141
+ end
142
+
143
+ it 'uses the _2x thumb for retina screens' do
144
+ Artwork.load_2x_images = true
145
+ expect_thumb '640x', :'640x_2x'
146
+ expect_thumb '640x', :'640x_2x'
147
+ end
148
+
149
+ it 'uses the non-retina thumb for retina screens if no _2x thumb is available' do
150
+ Artwork.load_2x_images = true
151
+ expect_thumb '2000x', :'2000x'
152
+ end
153
+
154
+ it 'passes through unsupported thumb names' do
155
+ expect_thumb 'unsupported', :unsupported
156
+
157
+ Artwork.load_2x_images = true
158
+ Artwork.base_resolution = 1000
159
+ Artwork.current_resolution = 5000
160
+
161
+ expect_thumb 'unsupported', :unsupported
162
+ end
163
+
164
+ it 'picks the nearest non-retina size to our desizred size' do
165
+ expect_thumb '390x', :'400x500'
166
+ expect_thumb '420x', :'640x'
167
+ end
168
+
169
+ it 'picks the largest available size if requesting a too large thumb' do
170
+ expect_thumb '5000x', :'2000x'
171
+ end
172
+
173
+ it 'picks the smallest available size if requesting a too small thumb' do
174
+ expect_thumb '100x', :'320x'
175
+ end
176
+
177
+ it 'distinguishes thumbs by the supplied text label' do
178
+ expect_thumb '320x', :'320x'
179
+ expect_thumb '320x_some_label', :'320x_some_label'
180
+ expect_thumb '200x_some_label', :'320x_some_label'
181
+ end
182
+
183
+ it 'allows changing the base resolution per request' do
184
+ Artwork.current_resolution = 2000
185
+
186
+ expect_thumb '320x@320', :'2000x'
187
+ expect_thumb '320x@640', :'1280x'
188
+
189
+ Artwork.current_resolution = 320
190
+ expect_thumb '320x@320', :'320x'
191
+ expect_thumb '320x_some_label@320', :'320x_some_label'
192
+ expect_thumb '200x_some_label@320', :'320x_some_label'
193
+ end
194
+
195
+ it 'considers the aspect ratio of the desired thumb' do
196
+ expect_thumb '320x499', :'320x500'
197
+ expect_thumb '319x498', :'320x500'
198
+ Artwork.load_2x_images = true
199
+ expect_thumb '319x498', :'320x500_2x'
200
+ expect_thumb '319x498_crop', :'320x500_crop_2x'
201
+ Artwork.load_2x_images = false
202
+ expect_thumb '319x498_crop', :'320x500_crop'
203
+ end
204
+
205
+ it 'returns the largest thumb with the requested label if no other suitable sizes are found' do
206
+ expect_thumb '20000x_crop', :'320x500_crop'
207
+ Artwork.load_2x_images = true
208
+ expect_thumb '20000x_crop', :'320x500_crop_2x'
209
+ end
210
+
211
+ it 'returns the largest thumb with the requested aspect ratio if no other suitable sizes are found' do
212
+ expect_thumb '8000x10000', :'400x500'
213
+ Artwork.load_2x_images = true
214
+ expect_thumb '8000x10000', :'400x500_2x'
215
+ end
216
+
217
+ it 'returns nil if no thumbnail matches the requested aspect ratio' do
218
+ expect_thumb '319x200', nil
219
+ end
220
+
221
+ it 'returns nil if no thumbnail matches the requested label' do
222
+ expect_thumb '319x_nonexistant_label', nil
223
+ end
224
+
225
+ context 'with an alternative sizes definition' do
226
+ it 'ignores alternative sizes if current resolution is above all the max resolutions given' do
227
+ Artwork.current_resolution = 1000
228
+
229
+ expect_thumb ['320x', {700 => '100x@100'}], :'320x'
230
+ end
231
+
232
+ it 'picks the first alternative size if current resolution is smaller than all max resolutions' do
233
+ Artwork.current_resolution = 799
234
+
235
+ expect_thumb ['320x', {1280 => '500x', 800 => '1000x'}], :'1280x'
236
+ end
237
+
238
+ it 'picks the first alternative size if current resolution is smaller than all max resolutions and supports custom base resolutions' do
239
+ Artwork.current_resolution = 799
240
+
241
+ expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100'}], :'1280x'
242
+ end
243
+
244
+ it 'compares resolutions with <=' do
245
+ Artwork.current_resolution = 800
246
+
247
+ expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100'}], :'1280x'
248
+ end
249
+
250
+ it 'picks the largest alternative size if current resolution is smaller only than the max resolution given' do
251
+ Artwork.current_resolution = 1200
252
+
253
+ expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100'}], :'640x'
254
+ end
255
+
256
+ it 'ignores non-numeric keys' do
257
+ Artwork.current_resolution = 1200
258
+
259
+ expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100', :foo => 'bar'}], :'640x'
260
+ end
261
+ end
262
+ end
263
+ end
@@ -1,2 +1,3 @@
1
1
  require 'rspec'
2
2
  require 'artwork'
3
+ require 'shared/model'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artwork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitar Dimitrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-17 00:00:00.000000000 Z
11
+ date: 2015-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,13 +53,13 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rails
56
+ name: paperclip
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2.3'
62
- type: :runtime
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
@@ -67,7 +67,21 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.3'
69
69
  - !ruby/object:Gem::Dependency
70
- name: paperclip
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -121,10 +135,12 @@ files:
121
135
  - lib/assets/javascripts/artwork.js
122
136
  - spec/artwork/configuration_spec.rb
123
137
  - spec/artwork/controller_spec.rb
124
- - spec/artwork/model_spec.rb
125
138
  - spec/artwork/thumbnail_spec.rb
126
139
  - spec/artwork/view_spec.rb
127
140
  - spec/artwork_spec.rb
141
+ - spec/examples/model_with_paperclip_spec.rb
142
+ - spec/examples/model_without_paperclip_spec.rb
143
+ - spec/shared/model.rb
128
144
  - spec/spec_helper.rb
129
145
  homepage: https://github.com/mitio/artwork
130
146
  licenses:
@@ -146,15 +162,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
162
  version: '0'
147
163
  requirements: []
148
164
  rubyforge_project:
149
- rubygems_version: 2.2.2
165
+ rubygems_version: 2.4.5.1
150
166
  signing_key:
151
167
  specification_version: 4
152
168
  summary: Automated user resolution based image size choosing for Rails.
153
169
  test_files:
154
170
  - spec/artwork/configuration_spec.rb
155
171
  - spec/artwork/controller_spec.rb
156
- - spec/artwork/model_spec.rb
157
172
  - spec/artwork/thumbnail_spec.rb
158
173
  - spec/artwork/view_spec.rb
159
174
  - spec/artwork_spec.rb
175
+ - spec/examples/model_with_paperclip_spec.rb
176
+ - spec/examples/model_without_paperclip_spec.rb
177
+ - spec/shared/model.rb
160
178
  - spec/spec_helper.rb
@@ -1,246 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Artwork
4
- describe Model do
5
- let(:model) { Class.new { include Artwork::Model } }
6
- let(:instance) { model.new }
7
-
8
- before :each do
9
- allow(model).to receive(:attachment_definitions).and_return({
10
- :image => {
11
- :styles => {
12
- :'320x' => '320x>',
13
- :'320x_2x' => '640x>',
14
- :'640x_2x' => '1280x>',
15
- :'640x' => '640x>',
16
- :'1280x' => '1280x>',
17
- :'1280x_2x' => '2560x>',
18
- :'2000x' => '2000x>',
19
- :'1500x_2x' => '3000x>',
20
- :'320x_some_label' => '320x>',
21
- :'320x_some_label_2x' => '640x>',
22
- :'320x500' => '320x500>',
23
- :'320x500_2x' => '640x1000>',
24
- :'320x500_crop' => '320x500#',
25
- :'320x500_crop_2x' => '640x1000#',
26
- :'400x500' => '400x500>',
27
- :'400x500_2x' => '800x1000>',
28
- :'320x_' => '320x>',
29
- :unsupported => '100x100>'
30
- },
31
- },
32
- })
33
- end
34
-
35
- describe '#attachment_styles_for' do
36
- it 'returns the list of available thumbnails' do
37
- expect(instance.attachment_styles_for(:image)).to match_array [
38
- :'320x',
39
- :'320x_2x',
40
- :'640x_2x',
41
- :'640x',
42
- :'1280x',
43
- :'1280x_2x',
44
- :'2000x',
45
- :'1500x_2x',
46
- :'320x_some_label',
47
- :'320x_some_label_2x',
48
- :'320x500',
49
- :'320x500_2x',
50
- :'320x500_crop',
51
- :'320x500_crop_2x',
52
- :'400x500',
53
- :'400x500_2x',
54
- :'320x_',
55
- :unsupported,
56
- ]
57
- end
58
- end
59
-
60
- describe '#artwork_url' do
61
- it 'returns the computed url of an attachment by delegating to artwork_thumb_for' do
62
- expect(instance).to receive(:artwork_thumb_for).with(:photo, :size, 'options').and_return(:computed_size)
63
-
64
- attachment = double
65
- expect(attachment).to receive(:url).with(:computed_size, 'options').and_return 'some/url'
66
- expect(instance).to receive(:photo).and_return(attachment)
67
-
68
- expect(instance.artwork_url(:photo, :size, 'options')).to eq 'some/url'
69
- end
70
-
71
- it 'works with two arguments and a hash options' do
72
- expect(instance).to receive(:artwork_thumb_for).with(:photo, :size, :some => 'options').and_return(:computed_size)
73
-
74
- attachment = double
75
- expect(attachment).to receive(:url).with(:computed_size, :some => 'options').and_return 'some/url'
76
- expect(instance).to receive(:photo).and_return(attachment)
77
-
78
- expect(instance.artwork_url(:photo, :size, :some => 'options')).to eq 'some/url'
79
- end
80
-
81
- it 'works with two arguments only without any options hash' do
82
- expect(instance).to receive(:artwork_thumb_for).with(:photo, :size, {}).and_return(:computed_size)
83
-
84
- attachment = double
85
- expect(attachment).to receive(:url).with(:computed_size, {}).and_return 'some/url'
86
- expect(instance).to receive(:photo).and_return(attachment)
87
-
88
- expect(instance.artwork_url(:photo, :size)).to eq 'some/url'
89
- end
90
- end
91
-
92
- describe '#artwork_thumb_for' do
93
- before :each do
94
- Artwork.base_resolution = 1000
95
- Artwork.current_resolution = 1000
96
- Artwork.actual_resolution = 1000
97
- Artwork.load_2x_images = false
98
- end
99
-
100
- def expect_thumb(size, expected)
101
- expect(instance.artwork_thumb_for(:image, *Array(size))).to eq expected
102
- end
103
-
104
- it 'picks the exact requested size if it exists' do
105
- expect_thumb '2000x', :'2000x'
106
- end
107
-
108
- it 'accepts sizes passed as both a symbol or a string' do
109
- expect_thumb '2000x', :'2000x'
110
- expect_thumb :'2000x', :'2000x'
111
- end
112
-
113
- it 'scales the required size according to current_resolution' do
114
- Artwork.base_resolution = 1000
115
- Artwork.current_resolution = 2000
116
-
117
- expect_thumb '1000x', :'2000x'
118
- expect_thumb '640x', :'1280x'
119
- end
120
-
121
- it 'ignores the retina thumbs when looking for a given size' do
122
- expect_thumb '1500x', :'2000x'
123
- end
124
-
125
- it 'uses the _2x thumb for retina screens' do
126
- Artwork.load_2x_images = true
127
- expect_thumb '640x', :'640x_2x'
128
- expect_thumb '640x', :'640x_2x'
129
- end
130
-
131
- it 'uses the non-retina thumb for retina screens if no _2x thumb is available' do
132
- Artwork.load_2x_images = true
133
- expect_thumb '2000x', :'2000x'
134
- end
135
-
136
- it 'passes through unsupported thumb names' do
137
- expect_thumb 'unsupported', :unsupported
138
-
139
- Artwork.load_2x_images = true
140
- Artwork.base_resolution = 1000
141
- Artwork.current_resolution = 5000
142
-
143
- expect_thumb 'unsupported', :unsupported
144
- end
145
-
146
- it 'picks the nearest non-retina size to our desizred size' do
147
- expect_thumb '390x', :'400x500'
148
- expect_thumb '420x', :'640x'
149
- end
150
-
151
- it 'picks the largest available size if requesting a too large thumb' do
152
- expect_thumb '5000x', :'2000x'
153
- end
154
-
155
- it 'picks the smallest available size if requesting a too small thumb' do
156
- expect_thumb '100x', :'320x'
157
- end
158
-
159
- it 'distinguishes thumbs by the supplied text label' do
160
- expect_thumb '320x', :'320x'
161
- expect_thumb '320x_some_label', :'320x_some_label'
162
- expect_thumb '200x_some_label', :'320x_some_label'
163
- end
164
-
165
- it 'allows changing the base resolution per request' do
166
- Artwork.current_resolution = 2000
167
-
168
- expect_thumb '320x@320', :'2000x'
169
- expect_thumb '320x@640', :'1280x'
170
-
171
- Artwork.current_resolution = 320
172
- expect_thumb '320x@320', :'320x'
173
- expect_thumb '320x_some_label@320', :'320x_some_label'
174
- expect_thumb '200x_some_label@320', :'320x_some_label'
175
- end
176
-
177
- it 'considers the aspect ratio of the desired thumb' do
178
- expect_thumb '320x499', :'320x500'
179
- expect_thumb '319x498', :'320x500'
180
- Artwork.load_2x_images = true
181
- expect_thumb '319x498', :'320x500_2x'
182
- expect_thumb '319x498_crop', :'320x500_crop_2x'
183
- Artwork.load_2x_images = false
184
- expect_thumb '319x498_crop', :'320x500_crop'
185
- end
186
-
187
- it 'returns the largest thumb with the requested label if no other suitable sizes are found' do
188
- expect_thumb '20000x_crop', :'320x500_crop'
189
- Artwork.load_2x_images = true
190
- expect_thumb '20000x_crop', :'320x500_crop_2x'
191
- end
192
-
193
- it 'returns the largest thumb with the requested aspect ratio if no other suitable sizes are found' do
194
- expect_thumb '8000x10000', :'400x500'
195
- Artwork.load_2x_images = true
196
- expect_thumb '8000x10000', :'400x500_2x'
197
- end
198
-
199
- it 'returns nil if no thumbnail matches the requested aspect ratio' do
200
- expect_thumb '319x200', nil
201
- end
202
-
203
- it 'returns nil if no thumbnail matches the requested label' do
204
- expect_thumb '319x_nonexistant_label', nil
205
- end
206
-
207
- context 'with an alternative sizes definition' do
208
- it 'ignores alternative sizes if current resolution is above all the max resolutions given' do
209
- Artwork.current_resolution = 1000
210
-
211
- expect_thumb ['320x', {700 => '100x@100'}], :'320x'
212
- end
213
-
214
- it 'picks the first alternative size if current resolution is smaller than all max resolutions' do
215
- Artwork.current_resolution = 799
216
-
217
- expect_thumb ['320x', {1280 => '500x', 800 => '1000x'}], :'1280x'
218
- end
219
-
220
- it 'picks the first alternative size if current resolution is smaller than all max resolutions and supports custom base resolutions' do
221
- Artwork.current_resolution = 799
222
-
223
- expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100'}], :'1280x'
224
- end
225
-
226
- it 'compares resolutions with <=' do
227
- Artwork.current_resolution = 800
228
-
229
- expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100'}], :'1280x'
230
- end
231
-
232
- it 'picks the largest alternative size if current resolution is smaller only than the max resolution given' do
233
- Artwork.current_resolution = 1200
234
-
235
- expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100'}], :'640x'
236
- end
237
-
238
- it 'ignores non-numeric keys' do
239
- Artwork.current_resolution = 1200
240
-
241
- expect_thumb ['320x', {1280 => '50x@100', 800 => '100x@100', :foo => 'bar'}], :'640x'
242
- end
243
- end
244
- end
245
- end
246
- end