dm-paperclip-r3 2.4.1
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.
- data/LICENSE +26 -0
- data/README.rdoc +118 -0
- data/Rakefile +104 -0
- data/init.rb +1 -0
- data/lib/dm-paperclip/attachment.rb +416 -0
- data/lib/dm-paperclip/callback_compatability.rb +33 -0
- data/lib/dm-paperclip/geometry.rb +117 -0
- data/lib/dm-paperclip/interpolations.rb +123 -0
- data/lib/dm-paperclip/iostream.rb +46 -0
- data/lib/dm-paperclip/processor.rb +49 -0
- data/lib/dm-paperclip/storage.rb +257 -0
- data/lib/dm-paperclip/thumbnail.rb +70 -0
- data/lib/dm-paperclip/upfile.rb +47 -0
- data/lib/dm-paperclip/validations.rb +124 -0
- data/lib/dm-paperclip.rb +369 -0
- data/tasks/paperclip_tasks.rake +38 -0
- data/test/attachment_test.rb +361 -0
- data/test/fixtures/12k.png +0 -0
- data/test/fixtures/50x50.png +0 -0
- data/test/fixtures/5k.png +0 -0
- data/test/fixtures/bad.png +1 -0
- data/test/fixtures/text.txt +0 -0
- data/test/geometry_test.rb +142 -0
- data/test/helper.rb +72 -0
- data/test/integration_test.rb +396 -0
- data/test/iostream_test.rb +78 -0
- data/test/paperclip_test.rb +163 -0
- data/test/storage_test.rb +324 -0
- data/test/thumbnail_test.rb +147 -0
- metadata +181 -0
@@ -0,0 +1,396 @@
|
|
1
|
+
require 'test/helper.rb'
|
2
|
+
|
3
|
+
class IntegrationTest < Test::Unit::TestCase
|
4
|
+
context "Many models at once" do
|
5
|
+
setup do
|
6
|
+
rebuild_model
|
7
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"))
|
8
|
+
300.times do |i|
|
9
|
+
Dummy.create! :avatar => @file
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should "not exceed the open file limit" do
|
14
|
+
assert_nothing_raised do
|
15
|
+
dummies = Dummy.all
|
16
|
+
dummies.each { |dummy| dummy.avatar }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "An attachment" do
|
22
|
+
setup do
|
23
|
+
rebuild_model :styles => { :thumb => "50x50#" }
|
24
|
+
@dummy = Dummy.new
|
25
|
+
@dummy.id = 1
|
26
|
+
@file = File.new(File.join(File.dirname(__FILE__),
|
27
|
+
"fixtures",
|
28
|
+
"5k.png"))
|
29
|
+
@dummy.avatar = @file
|
30
|
+
assert @dummy.save
|
31
|
+
end
|
32
|
+
|
33
|
+
should "create its thumbnails properly" do
|
34
|
+
assert_match /\b50x50\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
|
35
|
+
end
|
36
|
+
|
37
|
+
context "redefining its attachment styles" do
|
38
|
+
setup do
|
39
|
+
Dummy.class_eval do
|
40
|
+
has_attached_file :avatar, :styles => { :thumb => "150x25#" }
|
41
|
+
end
|
42
|
+
@d2 = Dummy.get(@dummy.id)
|
43
|
+
@d2.avatar.reprocess!
|
44
|
+
@d2.save
|
45
|
+
end
|
46
|
+
|
47
|
+
should "create its thumbnails properly" do
|
48
|
+
assert_match /\b150x25\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "A model with no attachment validation" do
|
54
|
+
setup do
|
55
|
+
rebuild_model :styles => { :large => "300x300>",
|
56
|
+
:medium => "100x100",
|
57
|
+
:thumb => ["32x32#", :gif] },
|
58
|
+
:default_style => :medium,
|
59
|
+
:url => "/:attachment/:class/:style/:id/:basename.:extension",
|
60
|
+
:path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
|
61
|
+
@dummy = Dummy.new
|
62
|
+
end
|
63
|
+
|
64
|
+
should "have its definition return false when asked about whiny_thumbnails" do
|
65
|
+
assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when validates_attachment_thumbnails is called" do
|
69
|
+
setup do
|
70
|
+
Dummy.validates_attachment_thumbnails :avatar
|
71
|
+
end
|
72
|
+
|
73
|
+
should "have its definition return true when asked about whiny_thumbnails" do
|
74
|
+
assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "redefined to have attachment validations" do
|
79
|
+
setup do
|
80
|
+
rebuild_model :styles => { :large => "300x300>",
|
81
|
+
:medium => "100x100",
|
82
|
+
:thumb => ["32x32#", :gif] },
|
83
|
+
:whiny_thumbnails => true,
|
84
|
+
:default_style => :medium,
|
85
|
+
:url => "/:attachment/:class/:style/:id/:basename.:extension",
|
86
|
+
:path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
|
87
|
+
end
|
88
|
+
|
89
|
+
should "have its definition return true when asked about whiny_thumbnails" do
|
90
|
+
assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "A model with no thumbnail_convert_options setting" do
|
96
|
+
setup do
|
97
|
+
rebuild_model :styles => { :large => "300x300>",
|
98
|
+
:medium => "100x100",
|
99
|
+
:thumb => ["32x32#", :gif] },
|
100
|
+
:default_style => :medium,
|
101
|
+
:url => "/:attachment/:class/:style/:id/:basename.:extension",
|
102
|
+
:path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
|
103
|
+
@dummy = Dummy.new
|
104
|
+
end
|
105
|
+
|
106
|
+
should "have its definition return nil when asked about convert_options" do
|
107
|
+
assert ! Dummy.attachment_definitions[:avatar][:thumbnail_convert_options]
|
108
|
+
end
|
109
|
+
|
110
|
+
context "redefined to have convert_options setting" do
|
111
|
+
setup do
|
112
|
+
rebuild_model :styles => { :large => "300x300>",
|
113
|
+
:medium => "100x100",
|
114
|
+
:thumb => ["32x32#", :gif] },
|
115
|
+
:thumbnail_convert_options => "-strip -depth 8",
|
116
|
+
:default_style => :medium,
|
117
|
+
:url => "/:attachment/:class/:style/:id/:basename.:extension",
|
118
|
+
:path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
|
119
|
+
end
|
120
|
+
|
121
|
+
should "have its definition return convert_options value when asked about convert_options" do
|
122
|
+
assert_equal "-strip -depth 8", Dummy.attachment_definitions[:avatar][:thumbnail_convert_options]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "A model with a filesystem attachment" do
|
128
|
+
setup do
|
129
|
+
rebuild_model :styles => { :large => "300x300>",
|
130
|
+
:medium => "100x100",
|
131
|
+
:thumb => ["32x32#", :gif] },
|
132
|
+
:whiny_thumbnails => true,
|
133
|
+
:default_style => :medium,
|
134
|
+
:url => "/:attachment/:class/:style/:id/:basename.:extension",
|
135
|
+
:path => ":merb_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
|
136
|
+
@dummy = Dummy.new
|
137
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"))
|
138
|
+
@bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
|
139
|
+
|
140
|
+
@dummy.id = 1
|
141
|
+
assert @dummy.avatar = @file
|
142
|
+
assert @dummy.valid?
|
143
|
+
assert @dummy.save
|
144
|
+
end
|
145
|
+
|
146
|
+
should "write and delete its files" do
|
147
|
+
[["434x66", :original],
|
148
|
+
["300x46", :large],
|
149
|
+
["100x15", :medium],
|
150
|
+
["32x32", :thumb]].each do |geo, style|
|
151
|
+
cmd = %Q[identify -format "%wx%h" #{@dummy.avatar.to_file(style).path}]
|
152
|
+
assert_equal geo, `#{cmd}`.chomp, cmd
|
153
|
+
end
|
154
|
+
|
155
|
+
saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
|
156
|
+
|
157
|
+
@d2 = Dummy.get(@dummy.id)
|
158
|
+
assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file.path}`.chomp
|
159
|
+
assert_equal "434x66", `identify -format "%wx%h" #{@d2.avatar.to_file(:original).path}`.chomp
|
160
|
+
assert_equal "300x46", `identify -format "%wx%h" #{@d2.avatar.to_file(:large).path}`.chomp
|
161
|
+
assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_file(:medium).path}`.chomp
|
162
|
+
assert_equal "32x32", `identify -format "%wx%h" #{@d2.avatar.to_file(:thumb).path}`.chomp
|
163
|
+
|
164
|
+
@dummy.avatar = "not a valid file but not nil"
|
165
|
+
assert_equal File.basename(@file.path), @dummy.avatar_file_name
|
166
|
+
assert @dummy.valid?
|
167
|
+
@dummy.other = 'dirty' # must have a change in order to save
|
168
|
+
assert @dummy.save
|
169
|
+
|
170
|
+
saved_paths.each do |p|
|
171
|
+
assert File.exists?(p)
|
172
|
+
end
|
173
|
+
|
174
|
+
@dummy.avatar = nil
|
175
|
+
assert_nil @dummy.avatar_file_name
|
176
|
+
assert @dummy.valid?
|
177
|
+
assert @dummy.save
|
178
|
+
|
179
|
+
saved_paths.each do |p|
|
180
|
+
assert ! File.exists?(p)
|
181
|
+
end
|
182
|
+
|
183
|
+
@d2 = Dummy.get(@dummy.id)
|
184
|
+
assert_nil @d2.avatar_file_name
|
185
|
+
end
|
186
|
+
|
187
|
+
should "work exactly the same when new as when reloaded" do
|
188
|
+
@d2 = Dummy.get(@dummy.id)
|
189
|
+
|
190
|
+
assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
|
191
|
+
[:thumb, :medium, :large, :original].each do |style|
|
192
|
+
assert_equal @dummy.avatar.to_file(style).path, @d2.avatar.to_file(style).path
|
193
|
+
end
|
194
|
+
|
195
|
+
saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s).path }
|
196
|
+
|
197
|
+
@d2.avatar = nil
|
198
|
+
assert @d2.save
|
199
|
+
|
200
|
+
saved_paths.each do |p|
|
201
|
+
assert ! File.exists?(p)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
should "know the difference between good files, bad files, not files, and nil" do
|
206
|
+
expected = @dummy.avatar.to_file
|
207
|
+
@dummy.avatar = "not a file"
|
208
|
+
assert @dummy.valid?
|
209
|
+
assert_equal expected.path, @dummy.avatar.to_file.path
|
210
|
+
|
211
|
+
@dummy.avatar = @bad_file
|
212
|
+
assert ! @dummy.valid?
|
213
|
+
@dummy.avatar = nil
|
214
|
+
assert @dummy.valid?
|
215
|
+
end
|
216
|
+
|
217
|
+
should "know the difference between good files, bad files, not files, and nil when validating" do
|
218
|
+
Dummy.validates_attachment_presence :avatar
|
219
|
+
@d2 = Dummy.get(@dummy.id)
|
220
|
+
@d2.avatar = @file
|
221
|
+
assert @d2.valid?
|
222
|
+
@d2.avatar = @bad_file
|
223
|
+
assert ! @d2.valid?
|
224
|
+
@d2.avatar = nil
|
225
|
+
assert ! @d2.valid?
|
226
|
+
end
|
227
|
+
|
228
|
+
should "be able to reload without saving an not have the file disappear" do
|
229
|
+
@dummy.avatar = @file
|
230
|
+
@dummy.save
|
231
|
+
@dummy.avatar = nil
|
232
|
+
assert_nil @dummy.avatar_file_name
|
233
|
+
@dummy.reload
|
234
|
+
assert_equal "5k.png", @dummy.avatar_file_name
|
235
|
+
end
|
236
|
+
|
237
|
+
context "that is assigned its file from another Paperclip attachment" do
|
238
|
+
setup do
|
239
|
+
@dummy2 = Dummy.new
|
240
|
+
@file2 = File.new(File.join(FIXTURES_DIR, "12k.png"))
|
241
|
+
assert @dummy2.avatar = @file2
|
242
|
+
@dummy2.save
|
243
|
+
end
|
244
|
+
|
245
|
+
should "work when assigned a file" do
|
246
|
+
assert_not_equal `identify -format "%wx%h" #{@dummy.avatar.to_file(:original).path}`,
|
247
|
+
`identify -format "%wx%h" #{@dummy2.avatar.to_file(:original).path}`
|
248
|
+
|
249
|
+
assert @dummy.avatar = @dummy2.avatar
|
250
|
+
@dummy.save
|
251
|
+
assert_equal `identify -format "%wx%h" #{@dummy.avatar.to_file(:original).path}`,
|
252
|
+
`identify -format "%wx%h" #{@dummy2.avatar.to_file(:original).path}`
|
253
|
+
end
|
254
|
+
|
255
|
+
should "work when assigned a nil file" do
|
256
|
+
@dummy2.avatar = nil
|
257
|
+
@dummy2.save
|
258
|
+
|
259
|
+
@dummy.avatar = @dummy2.avatar
|
260
|
+
@dummy.save
|
261
|
+
|
262
|
+
assert !@dummy.avatar?
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
end
|
267
|
+
|
268
|
+
if ENV['S3_TEST_BUCKET']
|
269
|
+
def s3_files_for attachment
|
270
|
+
[:thumb, :medium, :large, :original].inject({}) do |files, style|
|
271
|
+
data = `curl '#{attachment.url(style)}' 2>/dev/null`.chomp
|
272
|
+
t = Tempfile.new("paperclip-test")
|
273
|
+
t.write(data)
|
274
|
+
t.rewind
|
275
|
+
files[style] = t
|
276
|
+
files
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
context "A model with an S3 attachment" do
|
281
|
+
setup do
|
282
|
+
rebuild_model :styles => { :large => "300x300>",
|
283
|
+
:medium => "100x100",
|
284
|
+
:thumb => ["32x32#", :gif] },
|
285
|
+
:storage => :s3,
|
286
|
+
:whiny_thumbnails => true,
|
287
|
+
# :s3_options => {:logger => Logger.new(StringIO.new)},
|
288
|
+
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
|
289
|
+
:default_style => :medium,
|
290
|
+
:bucket => ENV['S3_TEST_BUCKET'],
|
291
|
+
:path => ":class/:attachment/:id/:style/:basename.:extension"
|
292
|
+
@dummy = Dummy.new
|
293
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"))
|
294
|
+
@bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"))
|
295
|
+
|
296
|
+
assert @dummy.avatar = @file
|
297
|
+
assert @dummy.valid?
|
298
|
+
assert @dummy.save
|
299
|
+
|
300
|
+
@files_on_s3 = s3_files_for @dummy.avatar
|
301
|
+
end
|
302
|
+
|
303
|
+
should "write and delete its files" do
|
304
|
+
[["434x66", :original],
|
305
|
+
["300x46", :large],
|
306
|
+
["100x15", :medium],
|
307
|
+
["32x32", :thumb]].each do |geo, style|
|
308
|
+
cmd = %Q[identify -format "%wx%h" #{@files_on_s3[style].path}]
|
309
|
+
assert_equal geo, `#{cmd}`.chomp, cmd
|
310
|
+
end
|
311
|
+
|
312
|
+
@d2 = Dummy.get(@dummy.id)
|
313
|
+
@d2_files = s3_files_for @d2.avatar
|
314
|
+
[["434x66", :original],
|
315
|
+
["300x46", :large],
|
316
|
+
["100x15", :medium],
|
317
|
+
["32x32", :thumb]].each do |geo, style|
|
318
|
+
cmd = %Q[identify -format "%wx%h" #{@d2_files[style].path}]
|
319
|
+
assert_equal geo, `#{cmd}`.chomp, cmd
|
320
|
+
end
|
321
|
+
|
322
|
+
@dummy.avatar = "not a valid file but not nil"
|
323
|
+
assert_equal File.basename(@file.path), @dummy.avatar_file_name
|
324
|
+
assert @dummy.valid?
|
325
|
+
assert @dummy.save
|
326
|
+
|
327
|
+
saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
|
328
|
+
|
329
|
+
saved_keys.each do |key|
|
330
|
+
assert key.exists?
|
331
|
+
end
|
332
|
+
|
333
|
+
@dummy.avatar = nil
|
334
|
+
assert_nil @dummy.avatar_file_name
|
335
|
+
assert @dummy.valid?
|
336
|
+
assert @dummy.save
|
337
|
+
|
338
|
+
saved_keys.each do |key|
|
339
|
+
assert ! key.exists?
|
340
|
+
end
|
341
|
+
|
342
|
+
@d2 = Dummy.get(@dummy.id)
|
343
|
+
assert_nil @d2.avatar_file_name
|
344
|
+
end
|
345
|
+
|
346
|
+
should "work exactly the same when new as when reloaded" do
|
347
|
+
@d2 = Dummy.get(@dummy.id)
|
348
|
+
|
349
|
+
assert_equal @dummy.avatar_file_name, @d2.avatar_file_name
|
350
|
+
[:thumb, :medium, :large, :original].each do |style|
|
351
|
+
assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s
|
352
|
+
end
|
353
|
+
|
354
|
+
saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) }
|
355
|
+
|
356
|
+
@d2.avatar = nil
|
357
|
+
assert @d2.save
|
358
|
+
|
359
|
+
saved_keys.each do |key|
|
360
|
+
assert ! key.exists?
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
should "know the difference between good files, bad files, not files, and nil" do
|
365
|
+
expected = @dummy.avatar.to_file
|
366
|
+
@dummy.avatar = "not a file"
|
367
|
+
assert @dummy.valid?
|
368
|
+
assert_equal expected.full_name, @dummy.avatar.to_file.full_name
|
369
|
+
|
370
|
+
@dummy.avatar = @bad_file
|
371
|
+
assert ! @dummy.valid?
|
372
|
+
@dummy.avatar = nil
|
373
|
+
assert @dummy.valid?
|
374
|
+
|
375
|
+
Dummy.validates_attachment_presence :avatar
|
376
|
+
@d2 = Dummy.get(@dummy.id)
|
377
|
+
@d2.avatar = @file
|
378
|
+
assert @d2.valid?
|
379
|
+
@d2.avatar = @bad_file
|
380
|
+
assert ! @d2.valid?
|
381
|
+
@d2.avatar = nil
|
382
|
+
assert ! @d2.valid?
|
383
|
+
end
|
384
|
+
|
385
|
+
should "be able to reload without saving and not have the file disappear" do
|
386
|
+
@dummy.avatar = @file
|
387
|
+
assert @dummy.save
|
388
|
+
@dummy.avatar = nil
|
389
|
+
assert_nil @dummy.avatar_file_name
|
390
|
+
@dummy.reload
|
391
|
+
assert_equal "5k.png", @dummy.avatar_file_name
|
392
|
+
end
|
393
|
+
end
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test/helper'
|
2
|
+
|
3
|
+
class IOStreamTest < Test::Unit::TestCase
|
4
|
+
context "IOStream" do
|
5
|
+
should "be included in IO, File, Tempfile, and StringIO" do
|
6
|
+
[IO, File, Tempfile, StringIO].each do |klass|
|
7
|
+
assert klass.included_modules.include?(IOStream), "Not in #{klass}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "A file" do
|
13
|
+
setup do
|
14
|
+
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
teardown { @file.close }
|
18
|
+
|
19
|
+
context "that is sent #stream_to" do
|
20
|
+
|
21
|
+
context "and given a String" do
|
22
|
+
setup do
|
23
|
+
FileUtils.mkdir_p(File.join(ROOT, 'tmp'))
|
24
|
+
assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test'))
|
25
|
+
end
|
26
|
+
|
27
|
+
should "return a File" do
|
28
|
+
assert @result.is_a?(File)
|
29
|
+
end
|
30
|
+
|
31
|
+
should "contain the same data as the original file" do
|
32
|
+
@file.rewind; @result.rewind
|
33
|
+
assert_equal @file.read, @result.read
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "and given a Tempfile" do
|
38
|
+
setup do
|
39
|
+
tempfile = Tempfile.new('iostream.test')
|
40
|
+
tempfile.binmode
|
41
|
+
assert @result = @file.stream_to(tempfile)
|
42
|
+
end
|
43
|
+
|
44
|
+
should "return a Tempfile" do
|
45
|
+
assert @result.is_a?(Tempfile)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "contain the same data as the original file" do
|
49
|
+
@file.rewind; @result.rewind
|
50
|
+
assert_equal @file.read, @result.read
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "that is sent #to_tempfile" do
|
57
|
+
setup do
|
58
|
+
assert @tempfile = @file.to_tempfile
|
59
|
+
end
|
60
|
+
|
61
|
+
should "convert it to a Paperclip Tempfile" do
|
62
|
+
assert @tempfile.is_a?(Paperclip::Tempfile)
|
63
|
+
end
|
64
|
+
|
65
|
+
should "have the name be based on the original_filename" do
|
66
|
+
name = File.basename(@file.path)
|
67
|
+
extension = File.extname(name)
|
68
|
+
basename = File.basename(name, extension)
|
69
|
+
assert_match %r[^#{Regexp.quote(basename)}.*?#{Regexp.quote(extension)}], File.basename(@tempfile.path)
|
70
|
+
end
|
71
|
+
|
72
|
+
should "have the Tempfile contain the same data as the file" do
|
73
|
+
@file.rewind; @tempfile.rewind
|
74
|
+
assert_equal @file.read, @tempfile.read
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'test/helper.rb'
|
2
|
+
|
3
|
+
class PaperclipTest < Test::Unit::TestCase
|
4
|
+
context "A DataMapper model with an 'avatar' attachment" do
|
5
|
+
setup do
|
6
|
+
rebuild_model :path => "tmp/:class/omg/:style.:extension"
|
7
|
+
@file = File.new(File.join(FIXTURES_DIR, "5k.png"))
|
8
|
+
end
|
9
|
+
|
10
|
+
should "not error when trying to also create a 'blah' attachment" do
|
11
|
+
assert_nothing_raised do
|
12
|
+
Dummy.class_eval do
|
13
|
+
has_attached_file :blah
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
should "handle multiple classes using attachments" do
|
19
|
+
Object.const_set("DummyTwo", Class.new())
|
20
|
+
DummyTwo.class_eval do
|
21
|
+
include DataMapper::Resource
|
22
|
+
include DataMapper::Validate
|
23
|
+
include Paperclip::Resource
|
24
|
+
property :id, DataMapper::Types::Serial
|
25
|
+
property :other, String
|
26
|
+
has_attached_file :file
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_equal [:file], DummyTwo.attachment_definitions.keys
|
30
|
+
assert_equal [:avatar], Dummy.attachment_definitions.keys
|
31
|
+
|
32
|
+
Object.send(:remove_const, "DummyTwo") rescue nil
|
33
|
+
|
34
|
+
assert_equal [:avatar], Dummy.attachment_definitions.keys
|
35
|
+
end
|
36
|
+
|
37
|
+
context "that is write protected" do
|
38
|
+
setup do
|
39
|
+
Dummy.class_eval do
|
40
|
+
has_attached_file :image, { :protected => true }
|
41
|
+
end
|
42
|
+
@dummy = Dummy.new
|
43
|
+
end
|
44
|
+
|
45
|
+
should "not assign the avatar on mass-set" do
|
46
|
+
@dummy.attributes = { :other => "I'm set!" } #,
|
47
|
+
#:image => @file }
|
48
|
+
|
49
|
+
assert_equal "I'm set!", @dummy.other
|
50
|
+
assert ! @dummy.image?
|
51
|
+
end
|
52
|
+
|
53
|
+
should "still allow assigment on normal set" do
|
54
|
+
@dummy.other = "I'm set!"
|
55
|
+
@dummy.image = @file
|
56
|
+
|
57
|
+
assert_equal "I'm set!", @dummy.other
|
58
|
+
assert @dummy.image?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with a subclass" do
|
63
|
+
setup do
|
64
|
+
class ::SubDummy < Dummy; end
|
65
|
+
end
|
66
|
+
|
67
|
+
should "be able to use the attachment from the subclass" do
|
68
|
+
assert_nothing_raised do
|
69
|
+
@subdummy = SubDummy.create(:avatar => @file)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
should "be able to see the attachment definition from the subclass's class" do
|
74
|
+
assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path]
|
75
|
+
end
|
76
|
+
|
77
|
+
teardown do
|
78
|
+
Object.send(:remove_const, "SubDummy") rescue nil
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
should "have an #avatar method" do
|
83
|
+
assert Dummy.new.respond_to?(:avatar)
|
84
|
+
end
|
85
|
+
|
86
|
+
should "have an #avatar= method" do
|
87
|
+
assert Dummy.new.respond_to?(:avatar=)
|
88
|
+
end
|
89
|
+
|
90
|
+
context "that is valid" do
|
91
|
+
setup do
|
92
|
+
@dummy = Dummy.new
|
93
|
+
@dummy.avatar = @file
|
94
|
+
end
|
95
|
+
|
96
|
+
should "be valid" do
|
97
|
+
assert @dummy.valid?
|
98
|
+
end
|
99
|
+
|
100
|
+
context "then has a validation added that makes it invalid" do
|
101
|
+
setup do
|
102
|
+
assert @dummy.save
|
103
|
+
Dummy.class_eval do
|
104
|
+
validates_attachment_content_type :avatar, :content_type => ["text/plain"]
|
105
|
+
end
|
106
|
+
@dummy2 = Dummy.get(@dummy.id)
|
107
|
+
end
|
108
|
+
|
109
|
+
should "be invalid when reloaded" do
|
110
|
+
assert ! @dummy2.valid?, @dummy2.errors.inspect
|
111
|
+
end
|
112
|
+
|
113
|
+
should "be able to call #valid? twice without having duplicate errors" do
|
114
|
+
@dummy2.avatar.valid?
|
115
|
+
first_errors = @dummy2.avatar.errors
|
116
|
+
@dummy2.avatar.valid?
|
117
|
+
assert_equal first_errors, @dummy2.avatar.errors
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
[[:presence, nil, "5k.png", nil],
|
123
|
+
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
|
124
|
+
[:size2, {:in => 1..10240}, nil, "12k.png"],
|
125
|
+
[:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
|
126
|
+
[:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
|
127
|
+
[:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
|
128
|
+
[:content_type4, {:content_type => "image/png"}, nil, "text.txt"]].each do |args|
|
129
|
+
context "with #{args[0]} validations" do
|
130
|
+
setup do
|
131
|
+
Dummy.class_eval do
|
132
|
+
send(*[:"validates_attachment_#{args[0].to_s[/[a-z_]*/]}", :avatar, args[1]].compact)
|
133
|
+
end
|
134
|
+
@dummy = Dummy.new
|
135
|
+
end
|
136
|
+
|
137
|
+
context "and a valid file" do
|
138
|
+
setup do
|
139
|
+
@file = args[2] && File.new(File.join(FIXTURES_DIR, args[2]))
|
140
|
+
end
|
141
|
+
|
142
|
+
should "not have any errors" do
|
143
|
+
@dummy.avatar = @file
|
144
|
+
assert @dummy.valid?
|
145
|
+
assert_equal 0, @dummy.errors.length
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "and an invalid file" do
|
150
|
+
setup do
|
151
|
+
@file = args[3] && File.new(File.join(FIXTURES_DIR, args[3]))
|
152
|
+
end
|
153
|
+
|
154
|
+
should "have errors" do
|
155
|
+
@dummy.avatar = @file
|
156
|
+
assert ! @dummy.valid?
|
157
|
+
assert_equal 1, @dummy.errors.length
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|