paperclip 2.4.2 → 2.4.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of paperclip might be problematic. Click here for more details.

@@ -32,12 +32,12 @@ module Paperclip
32
32
  # by default we behave as before, though.
33
33
  # if a proc has been supplied, we call it here
34
34
  def processors
35
- @processors.respond_to?(:call) ? @processors.call(attachment.instance) : (@processors || attachment.processors)
35
+ @processors.respond_to?(:call) ? @processors.call(attachment.instance) : (@processors || attachment.options.processors)
36
36
  end
37
37
 
38
38
  # retrieves from the attachment the whiny setting
39
39
  def whiny
40
- attachment.whiny
40
+ attachment.options.whiny
41
41
  end
42
42
 
43
43
  # returns true if we're inclined to grumble
@@ -74,7 +74,7 @@ module Paperclip
74
74
  end
75
75
 
76
76
  # Supports getting and setting style properties with hash notation to ensure backwards-compatibility
77
- # eg. @attachment.styles[:large][:geometry]@ will still work
77
+ # eg. @attachment.options.styles[:large][:geometry]@ will still work
78
78
  def [](key)
79
79
  if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated, :source_file_options].include?(key)
80
80
  send(key)
@@ -1,3 +1,3 @@
1
1
  module Paperclip
2
- VERSION = "2.4.2" unless defined? Paperclip::VERSION
2
+ VERSION = "2.4.3" unless defined? Paperclip::VERSION
3
3
  end
@@ -85,7 +85,7 @@ class AttachmentTest < Test::Unit::TestCase
85
85
  Paperclip::Attachment.default_options.keys.each do |key|
86
86
  should "be the default_options for #{key}" do
87
87
  assert_equal @old_default_options[key],
88
- @attachment.instance_variable_get("@#{key}"),
88
+ @attachment.options.send(key),
89
89
  key
90
90
  end
91
91
  end
@@ -100,24 +100,11 @@ class AttachmentTest < Test::Unit::TestCase
100
100
  Paperclip::Attachment.default_options.keys.each do |key|
101
101
  should "be the new default_options for #{key}" do
102
102
  assert_equal @new_default_options[key],
103
- @attachment.instance_variable_get("@#{key}"),
103
+ @attachment.options.send(key),
104
104
  key
105
105
  end
106
106
  end
107
107
  end
108
-
109
- context "with nested hash default" do
110
- setup do
111
- @nested_hash = {:thumb => {:first => "second" }}
112
- Paperclip::Attachment.default_options[:styles] = @nested_hash
113
- @dummy = Dummy.new
114
- @attachment = @dummy.avatar
115
- end
116
-
117
- should "correctly clone the nested hash" do
118
- assert_equal(@nested_hash, @attachment.instance_variable_get(:@styles))
119
- end
120
- end
121
108
  end
122
109
  end
123
110
 
@@ -197,12 +184,12 @@ class AttachmentTest < Test::Unit::TestCase
197
184
  end
198
185
 
199
186
  should "interpolate the hash data" do
200
- @attachment.expects(:interpolate).with(@attachment.options[:hash_data],anything).returns("interpolated_stuff")
187
+ @attachment.expects(:interpolate).with(@attachment.options.hash_data,anything).returns("interpolated_stuff")
201
188
  @attachment.hash
202
189
  end
203
190
 
204
191
  should "result in the correct interpolation" do
205
- assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options[:hash_data])
192
+ assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options.hash_data)
206
193
  end
207
194
 
208
195
  should "result in a correct hash" do
@@ -285,7 +272,7 @@ class AttachmentTest < Test::Unit::TestCase
285
272
  end
286
273
 
287
274
  should "report the correct options when sent #extra_source_file_options_for(:thumb)" do
288
- assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.source_file_options.inspect
275
+ assert_equal "-depth 8 -density 400", @dummy.avatar.send(:extra_source_file_options_for, :thumb), @dummy.avatar.options.source_file_options.inspect
289
276
  end
290
277
 
291
278
  should "report the correct options when sent #extra_source_file_options_for(:large)" do
@@ -367,7 +354,7 @@ class AttachmentTest < Test::Unit::TestCase
367
354
  end
368
355
 
369
356
  should "have the correct geometry" do
370
- assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
357
+ assert_equal "50x50#", @attachment.options.styles[:thumb][:geometry]
371
358
  end
372
359
  end
373
360
 
@@ -379,13 +366,13 @@ class AttachmentTest < Test::Unit::TestCase
379
366
  end
380
367
 
381
368
  should "have the correct styles for the assigned instance values" do
382
- assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
383
- assert_nil @dummy.avatar.styles[:large]
369
+ assert_equal "50x50#", @dummy.avatar.options.styles[:thumb][:geometry]
370
+ assert_nil @dummy.avatar.options.styles[:large]
384
371
 
385
372
  @dummy.other = 'b'
386
373
 
387
- assert_equal "400x400", @dummy.avatar.styles[:large][:geometry]
388
- assert_nil @dummy.avatar.styles[:thumb]
374
+ assert_equal "400x400", @dummy.avatar.options.styles[:large][:geometry]
375
+ assert_nil @dummy.avatar.options.styles[:thumb]
389
376
  end
390
377
  end
391
378
 
@@ -429,7 +416,7 @@ class AttachmentTest < Test::Unit::TestCase
429
416
  end
430
417
 
431
418
  should "have the correct geometry" do
432
- assert_equal "50x50#", @attachment.styles[:normal][:geometry]
419
+ assert_equal "50x50#", @attachment.options.styles[:normal][:geometry]
433
420
  end
434
421
  end
435
422
  end
@@ -447,7 +434,7 @@ class AttachmentTest < Test::Unit::TestCase
447
434
 
448
435
  [:processors, :whiny, :convert_options, :geometry, :format].each do |field|
449
436
  should "have the same #{field} field" do
450
- assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
437
+ assert_equal @attachment.options.styles[:normal][field], @attachment.options.styles[:hash][field]
451
438
  end
452
439
  end
453
440
  end
@@ -468,7 +455,7 @@ class AttachmentTest < Test::Unit::TestCase
468
455
  end
469
456
 
470
457
  should "have the correct processors" do
471
- assert_equal [ :test ], @attachment.styles[:normal][:processors]
458
+ assert_equal [ :test ], @attachment.options.styles[:normal][:processors]
472
459
  end
473
460
  end
474
461
  end
@@ -881,7 +868,7 @@ class AttachmentTest < Test::Unit::TestCase
881
868
 
882
869
  context "and trying to delete" do
883
870
  setup do
884
- @existing_names = @attachment.styles.keys.collect do |style|
871
+ @existing_names = @attachment.options.styles.keys.collect do |style|
885
872
  @attachment.path(style)
886
873
  end
887
874
  end
@@ -1121,12 +1108,12 @@ class AttachmentTest < Test::Unit::TestCase
1121
1108
  @dummy.destroy
1122
1109
  end
1123
1110
 
1124
- assert File.exists?(@path)
1111
+ assert File.exists?(@path), "#{@path} does not exist."
1125
1112
  end
1126
1113
 
1127
1114
  should "be deleted when the model is destroyed" do
1128
1115
  @dummy.destroy
1129
- assert ! File.exists?(@path)
1116
+ assert ! File.exists?(@path), "#{@path} does not exist."
1130
1117
  end
1131
1118
  end
1132
1119
 
@@ -18,7 +18,7 @@ class FogTest < Test::Unit::TestCase
18
18
  end
19
19
 
20
20
  should "have the proper information loading credentials from a file" do
21
- assert_equal @dummy.avatar.instance_variable_get("@fog_credentials")[:provider], 'AWS'
21
+ assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
22
22
  end
23
23
  end
24
24
 
@@ -34,7 +34,7 @@ class FogTest < Test::Unit::TestCase
34
34
  end
35
35
 
36
36
  should "have the proper information loading credentials from a file" do
37
- assert_equal @dummy.avatar.instance_variable_get("@fog_credentials")[:provider], 'AWS'
37
+ assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
38
38
  end
39
39
  end
40
40
 
@@ -61,7 +61,7 @@ class FogTest < Test::Unit::TestCase
61
61
  File.stubs(:exist?).returns(true)
62
62
  Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
63
63
  Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
64
-
64
+
65
65
  @dummy.save!
66
66
  end
67
67
  end
@@ -181,7 +181,7 @@ class FogTest < Test::Unit::TestCase
181
181
  end
182
182
 
183
183
  should 'set the @fog_public instance variable to false' do
184
- assert_equal false, @dummy.avatar.instance_variable_get('@fog_public')
184
+ assert_equal false, @dummy.avatar.options.fog_public
185
185
  end
186
186
  end
187
187
 
@@ -132,7 +132,7 @@ class GeometryTest < Test::Unit::TestCase
132
132
 
133
133
  should "not generate from a file with no path" do
134
134
  file = mock("file", :path => "")
135
- file.stubs(:respond_to?).with("path").returns(true)
135
+ file.stubs(:respond_to?).with(:path).returns(true)
136
136
  assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
137
137
  end
138
138
 
@@ -9,6 +9,7 @@ require 'active_record'
9
9
  require 'active_record/version'
10
10
  require 'active_support'
11
11
  require 'mime/types'
12
+ require 'pry'
12
13
 
13
14
  puts "Testing against version #{ActiveRecord::VERSION::STRING}"
14
15
 
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+ require './test/helper'
3
+
4
+ class MockAttachment < Struct.new(:one, :two)
5
+ def instance
6
+ self
7
+ end
8
+ end
9
+
10
+ class OptionsTest < Test::Unit::TestCase
11
+ context "#styles with a plain hash" do
12
+ setup do
13
+ @attachment = MockAttachment.new(nil, nil)
14
+ @options = Paperclip::Options.new(@attachment,
15
+ :styles => {
16
+ :something => ["400x400", :png]
17
+ })
18
+ end
19
+
20
+ should "return the right data for the style's geometry" do
21
+ assert_equal "400x400", @options.styles[:something][:geometry]
22
+ end
23
+
24
+ should "return the right data for the style's format" do
25
+ assert_equal :png, @options.styles[:something][:format]
26
+ end
27
+ end
28
+
29
+ context "#styles is a proc" do
30
+ setup do
31
+ @attachment = MockAttachment.new("123x456", :doc)
32
+ @options = Paperclip::Options.new(@attachment,
33
+ :styles => lambda {|att|
34
+ {:something => {:geometry => att.one, :format => att.two}}
35
+ })
36
+ end
37
+
38
+ should "return the right data for the style's geometry" do
39
+ assert_equal "123x456", @options.styles[:something][:geometry]
40
+ end
41
+
42
+ should "return the right data for the style's format" do
43
+ assert_equal :doc, @options.styles[:something][:format]
44
+ end
45
+
46
+ should "run the proc each time, giving dynamic results" do
47
+ assert_equal :doc, @options.styles[:something][:format]
48
+ @attachment.two = :pdf
49
+ assert_equal :pdf, @options.styles[:something][:format]
50
+ end
51
+ end
52
+
53
+ context "#processors" do
54
+ setup do
55
+ @attachment = MockAttachment.new(nil, nil)
56
+ end
57
+ should "return processors if not a proc" do
58
+ @options = Paperclip::Options.new(@attachment, :processors => [:one])
59
+ assert_equal [:one], @options.processors
60
+ end
61
+ should "return processors if it is a proc" do
62
+ @options = Paperclip::Options.new(@attachment, :processors => lambda{|att| [att.one]})
63
+ assert_equal [nil], @options.processors
64
+ @attachment.one = :other
65
+ assert_equal [:other], @options.processors
66
+ end
67
+ end
68
+ end
@@ -15,7 +15,7 @@ class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
15
15
  assert_kind_of Set, Paperclip.classes_with_attachments
16
16
  assert Paperclip.classes_with_attachments.empty?, 'list should be empty'
17
17
  rebuild_model
18
- assert_equal [Dummy].to_set, Paperclip.classes_with_attachments
18
+ assert_equal ['Dummy'].to_set, Paperclip.classes_with_attachments
19
19
  end
20
20
 
21
21
  should "enable to get and set path to registered styles file" do
@@ -173,15 +173,23 @@ class S3Test < Test::Unit::TestCase
173
173
  AWS::S3::Base.stubs(:establish_connection!)
174
174
  rebuild_model :storage => :s3,
175
175
  :s3_credentials => { :bucket => "prod_bucket" },
176
- :s3_host_alias => Proc.new { |image| "cdn#{image.size.to_i % 4}.example.com" },
176
+ :s3_host_alias => Proc.new{|atch| "cdn#{atch.instance.counter % 4}.example.com"},
177
177
  :path => ":attachment/:basename.:extension",
178
178
  :url => ":s3_alias_url"
179
+ Dummy.class_eval do
180
+ def counter
181
+ @counter ||= 0
182
+ @counter += 1
183
+ @counter
184
+ end
185
+ end
179
186
  @dummy = Dummy.new
180
187
  @dummy.avatar = StringIO.new(".")
181
188
  end
182
189
 
183
190
  should "return a url based on the host_alias" do
184
- assert_match %r{^http://cdn0.example.com/avatars/stringio.txt}, @dummy.avatar.url
191
+ assert_match %r{^http://cdn1.example.com/avatars/stringio.txt}, @dummy.avatar.url
192
+ assert_match %r{^http://cdn2.example.com/avatars/stringio.txt}, @dummy.avatar.url
185
193
  end
186
194
 
187
195
  should "still return the bucket name" do
@@ -203,7 +211,7 @@ class S3Test < Test::Unit::TestCase
203
211
  end
204
212
 
205
213
  should "return a relative URL for Rails to calculate assets host" do
206
- assert_match %r{^avatars/stringio.txt}, @dummy.avatar.url
214
+ assert_match %r{^avatars/stringio\.txt}, @dummy.avatar.url
207
215
  end
208
216
  end
209
217
 
@@ -292,7 +300,7 @@ class S3Test < Test::Unit::TestCase
292
300
  AWS::S3::Base.stubs(:establish_connection!)
293
301
  rebuild_model :storage => :s3,
294
302
  :s3_credentials => {
295
- :production => {:s3_host_name => "s3-world-end.amazonaws.com"},
303
+ :production => { :s3_host_name => "s3-world-end.amazonaws.com" },
296
304
  :development => { :s3_host_name => "s3-ap-northeast-1.amazonaws.com" }
297
305
  }
298
306
  @dummy = Dummy.new
@@ -6,8 +6,9 @@ class StyleTest < Test::Unit::TestCase
6
6
  context "A style rule" do
7
7
  setup do
8
8
  @attachment = attachment :path => ":basename.:extension",
9
- :styles => { :foo => {:geometry => "100x100#", :format => :png} }
10
- @style = @attachment.styles[:foo]
9
+ :styles => { :foo => {:geometry => "100x100#", :format => :png} },
10
+ :whiny => true
11
+ @style = @attachment.options.styles[:foo]
11
12
  end
12
13
 
13
14
  should "be held as a Style object" do
@@ -23,7 +24,6 @@ class StyleTest < Test::Unit::TestCase
23
24
  end
24
25
 
25
26
  should "be whiny if the attachment is" do
26
- @attachment.expects(:whiny).returns(true)
27
27
  assert @style.whiny?
28
28
  end
29
29
 
@@ -46,17 +46,11 @@ class StyleTest < Test::Unit::TestCase
46
46
  }
47
47
  end
48
48
 
49
- should "defer processing of procs until they are needed" do
50
- assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@geometry")
51
- assert_kind_of Proc, @attachment.styles[:bar].instance_variable_get("@geometry")
52
- assert_kind_of Proc, @attachment.instance_variable_get("@processors")
53
- end
54
-
55
49
  should "call procs when they are needed" do
56
- assert_equal "300x300#", @attachment.styles[:foo].geometry
57
- assert_equal "300x300#", @attachment.styles[:bar].geometry
58
- assert_equal [:test], @attachment.styles[:foo].processors
59
- assert_equal [:test], @attachment.styles[:bar].processors
50
+ assert_equal "300x300#", @attachment.options.styles[:foo].geometry
51
+ assert_equal "300x300#", @attachment.options.styles[:bar].geometry
52
+ assert_equal [:test], @attachment.options.styles[:foo].processors
53
+ assert_equal [:test], @attachment.options.styles[:bar].processors
60
54
  end
61
55
  end
62
56
 
@@ -70,30 +64,30 @@ class StyleTest < Test::Unit::TestCase
70
64
  :styles => styles
71
65
  end
72
66
  should "have the right number of styles" do
73
- assert_kind_of Hash, @attachment.styles
74
- assert_equal 3, @attachment.styles.size
67
+ assert_kind_of Hash, @attachment.options.styles
68
+ assert_equal 3, @attachment.options.styles.size
75
69
  end
76
70
 
77
71
  should "have styles as Style objects" do
78
72
  [:aslist, :ashash, :aslist].each do |s|
79
- assert_kind_of Paperclip::Style, @attachment.styles[s]
73
+ assert_kind_of Paperclip::Style, @attachment.options.styles[s]
80
74
  end
81
75
  end
82
76
 
83
77
  should "have the right geometries" do
84
78
  [:aslist, :ashash, :aslist].each do |s|
85
- assert_equal @attachment.styles[s].geometry, "100x100"
79
+ assert_equal @attachment.options.styles[s].geometry, "100x100"
86
80
  end
87
81
  end
88
82
 
89
83
  should "have the right formats" do
90
- assert_equal @attachment.styles[:aslist].format, :png
91
- assert_equal @attachment.styles[:ashash].format, :png
92
- assert_nil @attachment.styles[:asstring].format
84
+ assert_equal @attachment.options.styles[:aslist].format, :png
85
+ assert_equal @attachment.options.styles[:ashash].format, :png
86
+ assert_nil @attachment.options.styles[:asstring].format
93
87
  end
94
88
 
95
89
  should "retain order" do
96
- assert_equal [:aslist, :ashash, :asstring], @attachment.styles.keys
90
+ assert_equal [:aslist, :ashash, :asstring], @attachment.options.styles.keys
97
91
  end
98
92
  end
99
93
 
@@ -102,7 +96,7 @@ class StyleTest < Test::Unit::TestCase
102
96
  @attachment = attachment :path => ":basename.:extension",
103
97
  :styles => {:thumb => "100x100", :large => "400x400"},
104
98
  :convert_options => {:all => "-do_stuff", :thumb => "-thumbnailize"}
105
- @style = @attachment.styles[:thumb]
99
+ @style = @attachment.options.styles[:thumb]
106
100
  @file = StringIO.new("...")
107
101
  @file.stubs(:original_filename).returns("file.jpg")
108
102
  end
@@ -113,7 +107,7 @@ class StyleTest < Test::Unit::TestCase
113
107
 
114
108
  should "call extra_options_for(:thumb/:large) when convert options are requested" do
115
109
  @attachment.expects(:extra_options_for).with(:thumb)
116
- @attachment.styles[:thumb].convert_options
110
+ @attachment.options.styles[:thumb].convert_options
117
111
  end
118
112
  end
119
113
 
@@ -122,7 +116,7 @@ class StyleTest < Test::Unit::TestCase
122
116
  @attachment = attachment :path => ":basename.:extension",
123
117
  :styles => {:thumb => "100x100", :large => "400x400"},
124
118
  :source_file_options => {:all => "-density 400", :thumb => "-depth 8"}
125
- @style = @attachment.styles[:thumb]
119
+ @style = @attachment.options.styles[:thumb]
126
120
  @file = StringIO.new("...")
127
121
  @file.stubs(:original_filename).returns("file.jpg")
128
122
  end
@@ -133,7 +127,7 @@ class StyleTest < Test::Unit::TestCase
133
127
 
134
128
  should "call extra_options_for(:thumb/:large) when convert options are requested" do
135
129
  @attachment.expects(:extra_source_file_options_for).with(:thumb)
136
- @attachment.styles[:thumb].source_file_options
130
+ @attachment.options.styles[:thumb].source_file_options
137
131
  end
138
132
  end
139
133
 
@@ -148,7 +142,7 @@ class StyleTest < Test::Unit::TestCase
148
142
  }
149
143
  },
150
144
  :processors => [:thumbnail]
151
- @style = @attachment.styles[:foo]
145
+ @style = @attachment.options.styles[:foo]
152
146
  end
153
147
 
154
148
  should "not get processors from the attachment" do
@@ -176,11 +170,11 @@ class StyleTest < Test::Unit::TestCase
176
170
  end
177
171
 
178
172
  should "defer processing of procs until they are needed" do
179
- assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@processors")
173
+ assert_kind_of Proc, @attachment.options.styles[:foo].instance_variable_get("@processors")
180
174
  end
181
175
 
182
176
  should "call procs when they are needed" do
183
- assert_equal [:test], @attachment.styles[:foo].processors
177
+ assert_equal [:test], @attachment.options.styles[:foo].processors
184
178
  end
185
179
  end
186
180
  end