public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
big test refactoring, no new tests/features

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2450 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Wed Nov 08 20:35:16 -0800 2006
commit  a44db7f2522d742c1f75c2c941cdbfc1ab2c4f90
tree    66c32108e526b05fdd5eeca523d6c0d312c2dd9a
parent  71fd0f8a263e3469d26f14712fa5d5b15163f500
...
46
47
48
49
 
50
51
52
...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
...
46
47
48
 
49
50
51
52
...
67
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
71
0
@@ -46,7 +46,7 @@ module Technoweenie # :nodoc:
0
 
0
         # only need to define these once on a class
0
         unless included_modules.include? InstanceMethods
0
- class_inheritable_accessor :attachment_options
0
+ class_inheritable_accessor :attachment_options, :attachment_attributes
0
 
0
           after_destroy :destroy_file
0
 
0
@@ -67,341 +67,5 @@ module Technoweenie # :nodoc:
0
         self.attachment_options = options
0
       end
0
     end
0
-
0
- module ClassMethods
0
- delegate :content_types, :to => Technoweenie::ActsAsAttachment
0
-
0
- # Performs common validations for attachment models.
0
- def validates_as_attachment
0
- validates_presence_of :size, :content_type, :filename
0
- validate :attachment_attributes_valid?
0
- end
0
-
0
- # Returns true or false if the given content type is recognized as an image.
0
- def image?(content_type)
0
- content_types.include?(content_type)
0
- end
0
-
0
- # Yields a block containing an RMagick Image for the given binary data.
0
- def with_image(binary_data, &block)
0
- binary_data = Magick::Image::from_blob(binary_data).first unless !Object.const_defined?(:Magick) || binary_data.is_a?(Magick::Image)
0
- block.call binary_data if block && binary_data
0
- rescue
0
- # Log the failure to load the image. This should match ::Magick::ImageMagickError
0
- # but that would cause acts_as_attachment to require rmagick.
0
- logger.debug("Exception working with image: #{$!}")
0
- binary_data = nil
0
- ensure
0
- !binary_data.nil?
0
- end
0
-
0
- # Callback after an image has been resized.
0
- #
0
- # class Foo < ActiveRecord::Base
0
- # acts_as_attachment
0
- # after_resize do |record, img|
0
- # record.aspect_ratio = img.columns.to_f / img.rows.to_f
0
- # end
0
- # end
0
- def after_resize(&block)
0
- write_inheritable_array(:after_resize, [block])
0
- end
0
-
0
- # Callback after an attachment has been saved either to the file system or the DB.
0
- # Only called if the file has been changed, not necessarily if the record is updated.
0
- #
0
- # class Foo < ActiveRecord::Base
0
- # acts_as_attachment
0
- # after_attachment_saved do |record|
0
- # ...
0
- # end
0
- # end
0
- def after_attachment_saved(&block)
0
- write_inheritable_array(:after_attachment_saved, [block])
0
- end
0
-
0
- # Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
0
- #
0
- # class Foo < ActiveRecord::Base
0
- # acts_as_attachment
0
- # before_thumbnail_saved do |record, thumbnail|
0
- # ...
0
- # end
0
- # end
0
- def before_thumbnail_saved(&block)
0
- write_inheritable_array(:before_thumbnail_saved, [block])
0
- end
0
-
0
- # Get the thumbnail class, which is the current attachment class by default.
0
- # Configure this with the :thumbnail_class option.
0
- def thumbnail_class
0
- attachment_options[:thumbnail_class] = attachment_options[:thumbnail_class].constantize unless attachment_options[:thumbnail_class].is_a?(Class)
0
- attachment_options[:thumbnail_class]
0
- end
0
- end
0
-
0
- module InstanceMethods
0
- # Checks whether the attachment's content type is an image content type
0
- def image?
0
- self.class.image?(content_type.to_s.strip)
0
- end
0
-
0
- def thumbnail_class
0
- self.class.thumbnail_class
0
- end
0
-
0
- # Gets the thumbnail name for a filename. 'foo.jpg' becomes 'foo_thumbnail.jpg'
0
- def thumbnail_name_for(thumbnail = nil)
0
- return filename unless thumbnail
0
- basename, ext = filename.split '.'
0
- "#{basename}_#{thumbnail}.#{ext}"
0
- end
0
-
0
- # Creates or updates the thumbnail for the current attachment.
0
- def create_or_update_thumbnail(file_name_suffix, *size)
0
- returning thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) do |thumb|
0
- resized_image = resize_image_to(size)
0
- return if resized_image.nil?
0
- thumb.attributes = {
0
- :content_type => content_type,
0
- :filename => thumbnail_name_for(file_name_suffix),
0
- :attachment_data => resized_image
0
- }
0
- callback_with_args :before_thumbnail_saved, thumb
0
- thumb.save!
0
- end
0
- end
0
-
0
- # This method handles the uploaded file object. If you set the field name to uploaded_data, you don't need
0
- # any special code in your controller.
0
- #
0
- # <% form_for :attachment, :html => { :multipart => true } do |f| -%>
0
- # <p><%= f.file_field :uploaded_data %></p>
0
- # <p><%= submit_tag :Save %>
0
- # <% end -%>
0
- #
0
- # @attachment = Attachment.create! params[:attachment]
0
- def uploaded_data=(file_data)
0
- return nil if file_data.nil? || file_data.size == 0
0
- self.content_type = file_data.content_type.strip
0
- self.filename = file_data.original_filename.strip
0
- self.attachment_data = file_data.read
0
- end
0
-
0
- # Sets the actual binary data. This is typically called by uploaded_data=, but you can call this
0
- # manually if you're creating from the console. This is also where the resizing occurs.
0
- def attachment_data=(data)
0
- if data.nil?
0
- @attachment_data = nil
0
- @save_attachment = false
0
- return nil
0
- end
0
- with_image data do |img|
0
- resized_img = (attachment_options[:resize_to] && parent_id.nil?) ?
0
- thumbnail_for_image(img, attachment_options[:resize_to]) : img
0
- data = resized_img.to_blob
0
- self.width = resized_img.columns if respond_to?(:width)
0
- self.height = resized_img.rows if respond_to?(:height)
0
- callback_with_args :after_resize, resized_img
0
- end if image?
0
- self.size = data.length
0
- @attachment_data = data
0
- @save_attachment = true
0
- end
0
-
0
- # Resizes a thumbnail.
0
- def resize_image_to(size)
0
- thumb = nil
0
- with_image do |img|
0
- thumb = thumbnail_for_image(img, size)
0
- end
0
- thumb
0
- end
0
-
0
- # Returns the width/height in a suitable format for the image_tag helper: (100x100)
0
- def image_size
0
- [width.to_s, height.to_s] * 'x'
0
- end
0
-
0
- # Allows you to work with an RMagick representation of the attachment in a block.
0
- #
0
- # @attachment.with_image do |img|
0
- # self.data = img.thumbnail(100, 100).to_blob
0
- # end
0
- #
0
- def with_image(data = self.attachment_data, &block)
0
- self.class.with_image(data, &block)
0
- end
0
-
0
- def thumbnail_class
0
- self.class.thumbnail_class
0
- end
0
-
0
- protected
0
- # Performs the actual resizing operation for a thumbnail
0
- def thumbnail_for_image(img, size)
0
- size = size.first if size.is_a?(Array) && size.length == 1 && !size.first.is_a?(Fixnum)
0
- if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
0
- size = [size, size] if size.is_a?(Fixnum)
0
- img.thumbnail(size.first, size[1])
0
- else
0
- img.change_geometry(size.to_s) { |cols, rows, image| image.resize(cols, rows) }
0
- end
0
- end
0
-
0
- def sanitize_filename
0
- return unless filename
0
- # NOTE: File.basename doesn't work right with Windows paths on Unix
0
- # get only the filename, not the whole path
0
- filename.gsub!(/^.*(\\|\/)/, '')
0
-
0
- # Finally, replace all non alphanumeric, underscore or periods with underscore
0
- filename.gsub!(/[^\w\.\-]/, '_')
0
- end
0
-
0
- # creates default thumbnails for parent attachments
0
- def create_attachment_thumbnails
0
- if image? && @save_attachment && !attachment_options[:thumbnails].blank? && parent_id.nil?
0
- attachment_options[:thumbnails].each { |suffix, size| create_or_update_thumbnail(suffix, size) }
0
- end
0
- if @save_attachment
0
- @save_attachment = nil
0
- callback :after_attachment_saved
0
- end
0
- end
0
-
0
- # validates the size and content_type attributes according to the current model's options
0
- def attachment_attributes_valid?
0
- [:size, :content_type].each do |attr_name|
0
- enum = attachment_options[attr_name]
0
- errors.add attr_name, ActiveRecord::Errors.default_error_messages[:inclusion] unless enum.nil? || enum.include?(send(attr_name))
0
- end
0
- end
0
-
0
- # Yanked from ActiveRecord::Callbacks, modified so I can pass args to the callbacks besides self.
0
- # Only accept blocks, however
0
- def callback_with_args(method, arg = self)
0
- notify(method)
0
-
0
- result = nil
0
- callbacks_for(method).each do |callback|
0
- result = callback.call(self, arg)
0
- return false if result == false
0
- end
0
-
0
- return result
0
- end
0
- end
0
-
0
- # Methods for DB backed attachments
0
- module DbFileMethods
0
- def self.included(base) #:nodoc:
0
- base.belongs_to :db_file
0
- base.before_save :save_to_storage # so the db_file_id can be set
0
- end
0
-
0
- # Gets the attachment data
0
- def attachment_data
0
- @attachment_data ||= db_file.data
0
- end
0
-
0
- protected
0
- # Destroys the file. Called in the after_destroy callback
0
- def destroy_file
0
- db_file.destroy if db_file
0
- end
0
-
0
- # Saves the data to the DbFile model
0
- def save_to_storage
0
- if @save_attachment
0
- (db_file || build_db_file).data = attachment_data
0
- db_file.save!
0
- self.db_file_id = db_file.id # needed for my own sanity, k thx
0
- end
0
- true
0
- end
0
- end
0
-
0
- # Methods for file system backed attachments
0
- module FileSystemMethods
0
- def self.included(base) #:nodoc:
0
- base.before_update :rename_file
0
- base.after_save :save_to_storage # so the id can be part of the url
0
- end
0
-
0
- # Gets the attachment data
0
- def attachment_data
0
- return @attachment_data if @attachment_data
0
-
0
- filename = full_filename
0
- File.open(filename, 'rb') do |file|
0
- @attachment_data = file.read
0
- end if File.file?(filename)
0
- @attachment_data
0
- end
0
-
0
- # Gets the full path to the filename in this format:
0
- #
0
- # # This assumes a model name like MyModel
0
- # # public/#{table_name} is the default filesystem path
0
- # RAILS_ROOT/public/my_models/5/blah.jpg
0
- #
0
- # Overwrite this method in your model to customize the filename.
0
- # The optional thumbnail argument will output the thumbnail's filename.
0
- def full_filename(thumbnail = nil)
0
- file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:file_system_path]
0
- File.join(RAILS_ROOT, file_system_path, (parent_id || id).to_s, thumbnail_name_for(thumbnail))
0
- end
0
-
0
- # Used as the base path that #public_filename strips off full_filename to create the public path
0
- def base_path
0
- @base_path ||= File.join(RAILS_ROOT, 'public')
0
- end
0
-
0
- # Gets the public path to the file
0
- # The optional thumbnail argument will output the thumbnail's filename.
0
- def public_filename(thumbnail = nil)
0
- full_filename(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), ''
0
- end
0
-
0
- def filename=(value)
0
- @old_filename = full_filename unless filename.nil? || @old_filename
0
- write_attribute :filename, value
0
- end
0
-
0
- protected
0
- # Destroys the file. Called in the after_destroy callback
0
- def destroy_file
0
- FileUtils.rm full_filename rescue nil
0
- end
0
-
0
- def rename_file
0
- return unless @old_filename && @old_filename != full_filename
0
- if @save_attachment && File.exists?(@old_filename)
0
- FileUtils.rm @old_filename
0
- elsif File.exists?(@old_filename)
0
- FileUtils.mv @old_filename, full_filename
0
- end
0
- @old_filename = nil
0
- true
0
- end
0
-
0
- # Saves the file to the file system
0
- def save_to_storage
0
- if @save_attachment
0
- # TODO: This overwrites the file if it exists, maybe have an allow_overwrite option?
0
- FileUtils.mkdir_p(File.dirname(full_filename))
0
-
0
- # TODO Convert to streaming storage to prevent excessive memory usage
0
- # FileUtils.copy_stream is very efficient in regards to copies
0
- # OR - get the tmp filename for large files and do FileUtils.cp ? *agile*
0
- File.open(full_filename, "wb") do |file|
0
- file.write(attachment_data)
0
- end
0
- end
0
- @old_filename = nil
0
- true
0
- end
0
- end
0
   end
0
 end
...
25
26
27
28
 
 
 
 
 
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
 
 
48
49
50
51
 
 
 
 
 
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
56
 
 
57
...
25
26
27
 
28
29
30
31
32
33
 
 
34
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
37
38
39
40
 
 
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 
102
103
104
105
0
@@ -25,31 +25,79 @@ class Test::Unit::TestCase #:nodoc:
0
     end
0
   end
0
 
0
- # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
0
+ def setup
0
+ FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
0
+ attachment_model self.class.attachment_model
0
+ end
0
+
0
   self.use_transactional_fixtures = true
0
-
0
- # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
0
   self.use_instantiated_fixtures = false
0
 
0
- # http://project.ioni.st/post/217#post-217
0
- #
0
- # def test_new_publication
0
- # assert_difference(Publication, :count) do
0
- # post :create, :publication => {...}
0
- # # ...
0
- # end
0
- # end
0
- #
0
- def assert_difference(object, method = nil, difference = 1)
0
- initial_value = object.send(method)
0
- yield
0
- assert_equal initial_value + difference,
0
- object.send(method)
0
+ def self.attachment_model(klass = nil)
0
+ @attachment_model = klass if klass
0
+ @attachment_model
0
   end
0
 
0
- def assert_no_difference(object, method, &block)
0
- assert_difference object, method, 0, &block
0
+ def self.test_against_class(test_method, klass, subclass = false)
0
+ define_method("#{test_method}_on_#{:sub if subclass}class") do
0
+ klass = Class.new(klass) if subclass
0
+ attachment_model klass
0
+ send test_method, klass
0
+ end
0
   end
0
+
0
+ def self.test_against_subclass(test_method, klass)
0
+ test_against_class test_method, klass, true
0
+ end
0
+
0
+ protected
0
+ def upload_file(options = {})
0
+ att = attachment_model.create :uploaded_data => fixture_file_upload(options[:filename], options[:content_type] || 'image/png')
0
+ att.reload unless att.new_record?
0
+ att
0
+ end
0
+
0
+ def assert_created(num = 1)
0
+ assert_difference attachment_model.base_class, :count, num do
0
+ if attachment_model.included_modules.include? DbFile
0
+ assert_difference DbFile, :count, num do
0
+ yield
0
+ end
0
+ else
0
+ yield
0
+ end
0
+ end
0
+ end
0
+
0
+ def assert_not_created
0
+ assert_created(0) { yield }
0
+ end
0
+
0
+ def should_reject_by_size_with(klass)
0
+ attachment_model klass
0
+ assert_not_created do
0
+ attachment = upload_file :filename => '/files/rails.png'
0
+ assert attachment.new_record?
0
+ assert attachment.errors.on(:size)
0
+ assert_nil attachment.db_file if attachment.respond_to?(:db_file)
0
+ end
0
+ end
0
+
0
+ def assert_difference(object, method = nil, difference = 1)
0
+ initial_value = object.send(method)
0
+ yield
0
+ assert_equal initial_value + difference, object.send(method)
0
+ end
0
+
0
+ def assert_no_difference(object, method, &block)
0
+ assert_difference object, method, 0, &block
0
+ end
0
+
0
+ def attachment_model(klass = nil)
0
+ @attachment_model = klass if klass
0
+ @attachment_model
0
+ end
0
 end
0
 
0
-require File.join(File.dirname(__FILE__), 'fixtures/attachment')
0
\ No newline at end of file
0
+require File.join(File.dirname(__FILE__), 'fixtures/attachment')
0
+require File.join(File.dirname(__FILE__), 'fixtures/base_attachment_tests')
0
\ No newline at end of file
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
 
 
 
434
435
436
437
438
 
 
 
 
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
 
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
 
 
495
 
 
 
 
 
 
 
 
 
 
496
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
7
 
 
 
8
9
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -1,495 +1,27 @@
0
 require File.join(File.dirname(__FILE__), 'abstract_unit')
0
 
0
-# All sizes are commented out because they vary wildly among platforms
0
-class AttachmentTest < Test::Unit::TestCase
0
- def setup
0
- FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
0
- end
0
-
0
- def test_should_create_image_from_uploaded_file
0
- assert_created Attachment do
0
- attachment = upload_file :filename => '/files/rails.png'
0
- assert !attachment.new_record?, attachment.errors.full_messages.join("\n")
0
- assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
0
- assert attachment.image?
0
- assert !attachment.size.zero?
0
- #assert_equal 1784, attachment.size
0
- assert_equal 50, attachment.width
0
- assert_equal 64, attachment.height
0
- assert_equal '50x64', attachment.image_size
0
- end
0
- end
0
-
0
- def test_should_create_file_from_uploaded_file
0
- assert_created Attachment do
0
- attachment = upload_file :filename => '/files/foo.txt'
0
- assert !attachment.new_record?, attachment.errors.full_messages.join("\n")
0
- assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
0
- assert attachment.image?
0
- assert !attachment.size.zero?
0
- #assert_equal 3, attachment.size
0
- assert_nil attachment.width
0
- assert_nil attachment.height
0
- end
0
- end
0
-
0
- def test_should_create_image_from_uploaded_file_with_custom_content_type
0
- assert_created Attachment do
0
- attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
0
- assert !attachment.new_record?, attachment.errors.full_messages.join("\n")
0
- assert !attachment.image?
0
- assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
0
- assert !attachment.size.zero?
0
- #assert_equal 1784, attachment.size
0
- assert_nil attachment.width
0
- assert_nil attachment.height
0
- assert_equal [], attachment.thumbnails
0
- end
0
- end
0
-
0
- def test_should_create_thumbnail
0
- attachment = nil
0
- assert_created Attachment do
0
- attachment = upload_file :filename => '/files/rails.png'
0
- assert !attachment.new_record?, attachment.errors.full_messages.join("\n")
0
- end
0
- assert_equal 50, attachment.width
0
- assert_equal 64, attachment.height
0
-
0
- assert_created Attachment do
0
- basename, ext = attachment.filename.split '.'
0
- thumbnail = attachment.create_or_update_thumbnail('thumb', 50, 50)
0
- assert !thumbnail.new_record?, thumbnail.errors.full_messages.join("\n")
0
- assert !thumbnail.size.zero?
0
- #assert_in_delta 4673, thumbnail.size, 2
0
- assert_equal 50, thumbnail.width
0
- assert_equal 50, thumbnail.height
0
- assert_equal [thumbnail], attachment.thumbnails
0
- assert_equal attachment, thumbnail.parent
0
- assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
0
- end
0
- end
0
-
0
- def test_should_create_thumbnail_with_geometry_string
0
- attachment = nil
0
- assert_created Attachment do
0
- attachment = upload_file :filename => '/files/rails.png'
0
- assert !attachment.new_record?, attachment.errors.full_messages.join("\n")
0
- end
0
- assert_equal 50, attachment.width
0
- assert_equal 64, attachment.height
0
-
0
- assert_created Attachment do
0
- basename, ext = attachment.filename.split '.'
0
- thumbnail = attachment.create_or_update_thumbnail('thumb', 'x50')
0
- assert !thumbnail.new_record?, thumbnail.errors.full_messages.join("\n")
0
- assert !thumbnail.size.zero?
0
- #assert_equal 3915, thumbnail.size
0
- assert_equal 39, thumbnail.width
0
- assert_equal 50, thumbnail.height
0
- assert_equal [thumbnail], attachment.thumbnails
0
- assert_equal attachment, thumbnail.parent
0
- assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
0
- end
0
- end
0
-
0
- def test_should_resize_image(klass = ImageAttachment)
0
- assert_equal [50, 50], klass.att