miloops-attachment_fu 3.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/CHANGELOG +63 -0
  2. data/LICENSE +20 -0
  3. data/README +253 -0
  4. data/Rakefile +22 -0
  5. data/amazon_s3.yml.tpl +17 -0
  6. data/install.rb +7 -0
  7. data/lib/geometry.rb +96 -0
  8. data/lib/pothoven-attachment_fu.rb +23 -0
  9. data/lib/technoweenie/attachment_fu.rb +578 -0
  10. data/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb +211 -0
  11. data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
  12. data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +126 -0
  13. data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +394 -0
  14. data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +66 -0
  15. data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +59 -0
  16. data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +80 -0
  17. data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +142 -0
  18. data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +66 -0
  19. data/rackspace_cloudfiles.yml.tpl +14 -0
  20. data/test/backends/db_file_test.rb +16 -0
  21. data/test/backends/file_system_test.rb +143 -0
  22. data/test/backends/remote/cloudfiles_test.rb +102 -0
  23. data/test/backends/remote/s3_test.rb +119 -0
  24. data/test/base_attachment_tests.rb +77 -0
  25. data/test/basic_test.rb +120 -0
  26. data/test/database.yml +18 -0
  27. data/test/extra_attachment_test.rb +67 -0
  28. data/test/fixtures/attachment.rb +304 -0
  29. data/test/fixtures/files/fake/rails.png +0 -0
  30. data/test/fixtures/files/foo.txt +1 -0
  31. data/test/fixtures/files/rails.jpg +0 -0
  32. data/test/fixtures/files/rails.png +0 -0
  33. data/test/geometry_test.rb +114 -0
  34. data/test/processors/core_image_test.rb +58 -0
  35. data/test/processors/gd2_test.rb +51 -0
  36. data/test/processors/image_science_test.rb +54 -0
  37. data/test/processors/mini_magick_test.rb +122 -0
  38. data/test/processors/rmagick_test.rb +272 -0
  39. data/test/schema.rb +136 -0
  40. data/test/test_helper.rb +180 -0
  41. data/test/validation_test.rb +55 -0
  42. data/vendor/red_artisan/core_image/filters/color.rb +27 -0
  43. data/vendor/red_artisan/core_image/filters/effects.rb +31 -0
  44. data/vendor/red_artisan/core_image/filters/perspective.rb +25 -0
  45. data/vendor/red_artisan/core_image/filters/quality.rb +25 -0
  46. data/vendor/red_artisan/core_image/filters/scale.rb +47 -0
  47. data/vendor/red_artisan/core_image/filters/watermark.rb +32 -0
  48. data/vendor/red_artisan/core_image/processor.rb +123 -0
  49. metadata +98 -0
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ class CoreImageTest < Test::Unit::TestCase
4
+ attachment_model CoreImageAttachment
5
+
6
+ if Object.const_defined?(:OSX)
7
+ def test_should_resize_image
8
+ attachment = upload_file :filename => '/files/rails.png'
9
+ assert_valid attachment
10
+ assert attachment.image?
11
+ # test core image thumbnail
12
+ assert_equal 42, attachment.width
13
+ assert_equal 55, attachment.height
14
+
15
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
16
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
17
+ aspect = attachment.thumbnails.detect { |t| t.filename =~ /_aspect/ }
18
+
19
+ # test exact resize dimensions
20
+ assert_equal 50, thumb.width
21
+ assert_equal 51, thumb.height
22
+
23
+ # test geometry strings
24
+ assert_equal 31, geo.width
25
+ assert_equal 41, geo.height
26
+ assert_equal 25, aspect.width
27
+ assert_equal 25, aspect.height
28
+
29
+ # This makes sure that we didn't overwrite the original file
30
+ # and will end up with a thumbnail instead of the original
31
+ assert_equal 42, attachment.width
32
+ assert_equal 55, attachment.height
33
+
34
+ end
35
+
36
+ def test_should_handle_jpeg_quality
37
+ attachment_model CoreImageAttachment
38
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
39
+ full_size = attachment.size
40
+ attachment_model LowerQualityCoreImageAttachment
41
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
42
+ lq_size = attachment.size
43
+ assert lq_size <= full_size * 0.9, 'Lower-quality JPEG filesize should be congruently smaller'
44
+
45
+ # FIXME: wait for Marcus' reply to determine whether I can get exact-quality output or need to adjust for CoreImage.
46
+ # attachment_model CoreImageWithPerThumbJpegAttachment
47
+ # attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
48
+ # assert_file_jpeg_quality attachment, :thumb, 90
49
+ # assert_file_jpeg_quality attachment, :avatar, 80
50
+ # assert_file_jpeg_quality attachment, :editorial, 75
51
+ # assert_file_jpeg_quality attachment, nil, 75
52
+ end
53
+ else
54
+ def test_flunk
55
+ puts "CoreImage not loaded, tests not running"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ class GD2Test < Test::Unit::TestCase
4
+ attachment_model GD2Attachment
5
+
6
+ if Object.const_defined?(:GD2)
7
+ def test_should_resize_image
8
+ attachment = upload_file :filename => '/files/rails.png'
9
+ assert_valid attachment
10
+ assert attachment.image?
11
+ # test gd2 thumbnail
12
+ assert_equal 43, attachment.width
13
+ assert_equal 55, attachment.height
14
+
15
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
16
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
17
+ aspect = attachment.thumbnails.detect { |t| t.filename =~ /_aspect/ }
18
+
19
+ # test exact resize dimensions
20
+ assert_equal 50, thumb.width
21
+ assert_equal 51, thumb.height
22
+
23
+ # test geometry strings
24
+ assert_equal 31, geo.width
25
+ assert_equal 40, geo.height
26
+ assert_equal 25, aspect.width
27
+ assert_equal 25, aspect.height
28
+ end
29
+
30
+ def test_should_handle_jpeg_quality
31
+ attachment_model GD2Attachment
32
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
33
+ full_size = attachment.size
34
+ attachment_model LowerQualityGD2Attachment
35
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
36
+ lq_size = attachment.size
37
+ assert lq_size <= full_size * 0.9, 'Lower-quality JPEG filesize should be congruently smaller'
38
+
39
+ attachment_model GD2WithPerThumbJpegAttachment
40
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
41
+ assert_file_jpeg_quality attachment, :thumb, 90
42
+ assert_file_jpeg_quality attachment, :avatar, 80
43
+ assert_file_jpeg_quality attachment, :editorial, 75
44
+ assert_file_jpeg_quality attachment, nil, 75
45
+ end
46
+ else
47
+ def test_flunk
48
+ puts "GD2 not loaded, tests not running"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ class ImageScienceTest < Test::Unit::TestCase
4
+ attachment_model ImageScienceAttachment
5
+
6
+ if Object.const_defined?(:ImageScience)
7
+ def test_should_resize_image
8
+ attachment = upload_file :filename => '/files/rails.png'
9
+ assert_valid attachment
10
+ assert attachment.image?
11
+ # test image science thumbnail
12
+ assert_equal 42, attachment.width
13
+ assert_equal 55, attachment.height
14
+
15
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
16
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
17
+ aspect = attachment.thumbnails.detect { |t| t.filename =~ /_aspect/ }
18
+
19
+ # test exact resize dimensions
20
+ assert_equal 50, thumb.width
21
+ assert_equal 51, thumb.height
22
+
23
+ # test geometry strings
24
+ assert_equal 31, geo.width
25
+ assert_equal 41, geo.height
26
+ assert_equal 25, aspect.width
27
+ assert_equal 25, aspect.height
28
+ end
29
+
30
+ def test_should_handle_jpeg_quality
31
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
32
+ full_size = attachment.size
33
+ attachment_model ImageScienceLowerQualityAttachment
34
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
35
+ lq_size = attachment.size
36
+ if ImageScience.instance_method(:save).arity == -2 # tdd-image_science: JPEG quality processing
37
+ assert lq_size <= full_size * 0.75, 'Lower-quality JPEG filesize should be congruently smaller'
38
+ else
39
+ assert_equal full_size, lq_size, 'Unsupported lower-quality JPEG should yield exact same file size'
40
+ end
41
+
42
+ attachment_model ImageScienceWithPerThumbJpegAttachment
43
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
44
+ assert_file_jpeg_quality attachment, :thumb, 90
45
+ assert_file_jpeg_quality attachment, :avatar, 80
46
+ assert_file_jpeg_quality attachment, :editorial, 75
47
+ assert_file_jpeg_quality attachment, nil, 75
48
+ end
49
+ else
50
+ def test_flunk
51
+ puts "ImageScience not loaded, tests not running"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,122 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ class MiniMagickTest < Test::Unit::TestCase
4
+ attachment_model MiniMagickAttachment
5
+
6
+ if Object.const_defined?(:MiniMagick)
7
+ def test_should_resize_image
8
+ attachment = upload_file :filename => '/files/rails.png'
9
+ assert_valid attachment
10
+ assert attachment.image?
11
+ # test MiniMagick thumbnail
12
+ assert_equal 43, attachment.width
13
+ assert_equal 55, attachment.height
14
+
15
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
16
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
17
+ aspect = attachment.thumbnails.detect { |t| t.filename =~ /_aspect/ }
18
+
19
+ # test exact resize dimensions
20
+ assert_equal 50, thumb.width
21
+ assert_equal 51, thumb.height
22
+
23
+ # test geometry strings
24
+ assert_equal 31, geo.width
25
+ assert_equal 40, geo.height
26
+ assert_equal 25, aspect.width
27
+ assert_equal 25, aspect.height
28
+ end
29
+
30
+ def test_should_crop_image(klass = ImageThumbnailCrop)
31
+ attachment_model klass
32
+ attachment = upload_file :filename => '/files/rails.png'
33
+ assert_valid attachment
34
+ assert attachment.image?
35
+ # has_attachment :thumbnails => { :square => "50x50c", :vertical => "30x60c", :horizontal => "60x30c"}
36
+
37
+ square = attachment.thumbnails.detect { |t| t.filename =~ /_square/ }
38
+ vertical = attachment.thumbnails.detect { |t| t.filename =~ /_vertical/ }
39
+ horizontal = attachment.thumbnails.detect { |t| t.filename =~ /_horizontal/ }
40
+
41
+ # test excat resize
42
+ assert_equal 50, square.width
43
+ assert_equal 50, square.height
44
+
45
+ assert_equal 30, vertical.width
46
+ assert_equal 60, vertical.height
47
+
48
+ assert_equal 60, horizontal.width
49
+ assert_equal 30, horizontal.height
50
+ end
51
+
52
+ # tests the first step in resize, crop the image in original size to right format
53
+ def test_should_crop_image_right(klass = ImageThumbnailCrop)
54
+ @@testcases.collect do |testcase|
55
+ image_width, image_height, thumb_width, thumb_height = testcase[:data]
56
+ image_aspect, thumb_aspect = image_width/image_height, thumb_width/thumb_height
57
+ crop_comand = klass.calculate_offset(image_width, image_height, image_aspect, thumb_width, thumb_height,thumb_aspect)
58
+ # pattern matching on crop command
59
+ if testcase.has_key?(:height)
60
+ assert crop_comand.match(/^#{image_width}x#{testcase[:height]}\+0\+#{testcase[:yoffset]}$/)
61
+ else
62
+ assert crop_comand.match(/^#{testcase[:width]}x#{image_height}\+#{testcase[:xoffset]}\+0$/)
63
+ end
64
+ end
65
+ end
66
+
67
+ def test_should_handle_jpeg_quality
68
+ attachment_model MiniMagickAttachment
69
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
70
+ full_size = attachment.size
71
+ attachment_model LowerQualityMiniMagickAttachment
72
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
73
+ lq_size = attachment.size
74
+ assert lq_size <= full_size * 0.9, 'Lower-quality JPEG filesize should be congruently smaller'
75
+
76
+ attachment_model MiniMagickWithPerThumbJpegAttachment
77
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
78
+ assert_file_jpeg_quality attachment, :thumb, 90
79
+ assert_file_jpeg_quality attachment, :avatar, 80
80
+ assert_file_jpeg_quality attachment, :editorial, 75
81
+ assert_file_jpeg_quality attachment, nil, 75
82
+ end
83
+ else
84
+ def test_flunk
85
+ puts "MiniMagick not loaded, tests not running"
86
+ end
87
+ end
88
+
89
+ @@testcases = [
90
+ # image_aspect <= 1 && thumb_aspect >= 1
91
+ {:data => [10.0,40.0,2.0,1.0], :height => 5.0, :yoffset => 17.5}, # 1b
92
+ {:data => [10.0,40.0,1.0,1.0], :height => 10.0, :yoffset => 15.0}, # 1b
93
+
94
+ # image_aspect < 1 && thumb_aspect < 1
95
+ {:data => [10.0,40.0,1.0,2.0], :height => 20.0, :yoffset => 10.0}, # 1a
96
+ {:data => [2.0,3.0,1.0,2.0], :width => 1.5, :xoffset => 0.25}, # 1a
97
+
98
+ # image_aspect = thumb_aspect
99
+ {:data => [10.0,10.0,1.0,1.0], :height => 10.0, :yoffset => 0.0}, # QUADRAT 1c
100
+
101
+ # image_aspect >= 1 && thumb_aspect > 1 && image_aspect < thumb_aspect
102
+ {:data => [6.0,3.0,4.0,1.0], :height => 1.5, :yoffset => 0.75}, # 2b
103
+ {:data => [6.0,6.0,4.0,1.0], :height => 1.5, :yoffset => 2.25}, # 2b
104
+
105
+ # image_aspect > 1 && thumb_aspect > 1 && image_aspect > thumb_aspect
106
+ {:data => [9.0,3.0,2.0,1.0], :width => 6.0, :xoffset => 1.5}, # 2a
107
+
108
+ # image_aspect > 1 && thumb_aspect < 1 && image_aspect < thumb_aspect
109
+ {:data => [10.0,5.0,0.1,2.0], :width => 0.25, :xoffset => 4.875}, # 4
110
+ {:data => [10.0,5.0,1.0,2.0], :width => 2.5, :xoffset => 3.75}, # 4
111
+
112
+ # image_aspect > 1 && thumb_aspect > 1 && image_aspect > thumb_aspect
113
+ {:data => [9.0,3.0,2.0,1.0], :width => 6.0, :xoffset => 1.5}, # 3a
114
+ # image_aspect > 1 && thumb_aspect > 1 && image_aspect < thumb_aspect
115
+ {:data => [9.0,3.0,5.0,1.0], :height => 1.8, :yoffset => 0.6} # 3a
116
+ ]
117
+
118
+
119
+
120
+
121
+
122
+ end
@@ -0,0 +1,272 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+ class RmagickTest < Test::Unit::TestCase
3
+ attachment_model Attachment
4
+
5
+ if Object.const_defined?(:Magick)
6
+ def test_should_create_image_from_uploaded_file
7
+ assert_created do
8
+ attachment = upload_file :filename => '/files/rails.png'
9
+ assert_valid attachment
10
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
11
+ assert attachment.image?
12
+ assert !attachment.size.zero?
13
+ #assert_equal 1784, attachment.size
14
+ assert_equal 50, attachment.width
15
+ assert_equal 64, attachment.height
16
+ assert_equal '50x64', attachment.image_size
17
+ end
18
+ end
19
+
20
+ def test_should_create_image_from_uploaded_file_with_custom_content_type
21
+ assert_created do
22
+ attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
23
+ assert_valid attachment
24
+ assert !attachment.image?
25
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
26
+ assert !attachment.size.zero?
27
+ #assert_equal 1784, attachment.size
28
+ assert_nil attachment.width
29
+ assert_nil attachment.height
30
+ assert_equal [], attachment.thumbnails
31
+ end
32
+ end
33
+
34
+ def test_should_create_thumbnail
35
+ attachment = upload_file :filename => '/files/rails.png'
36
+
37
+ assert_created do
38
+ basename, ext = attachment.filename.split '.'
39
+ thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', 50, 50)
40
+ assert_valid thumbnail
41
+ assert !thumbnail.size.zero?
42
+ #assert_in_delta 4673, thumbnail.size, 2
43
+ assert_equal 50, thumbnail.width
44
+ assert_equal 50, thumbnail.height
45
+ assert_equal [thumbnail.id], attachment.thumbnails.collect(&:id)
46
+ assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
47
+ assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
48
+ end
49
+ end
50
+
51
+ def test_should_create_thumbnail_with_geometry_strings
52
+ attachment = upload_file :filename => '/files/rails.png'
53
+
54
+ assert_created do
55
+ basename, ext = attachment.filename.split '.'
56
+ { 'x50' => [39, 50], '25x25!' => [25, 25] }.each do |geo, (w, h)|
57
+ thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', geo)
58
+ assert_valid thumbnail
59
+ assert !thumbnail.size.zero?
60
+ assert_equal w, thumbnail.width
61
+ assert_equal h, thumbnail.height
62
+ assert_equal [thumbnail], attachment.thumbnails
63
+ assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
64
+ assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
65
+ end
66
+ end
67
+ end
68
+
69
+ def test_should_resize_image(klass = ImageAttachment)
70
+ attachment_model klass
71
+ assert_equal [50, 50], attachment_model.attachment_options[:resize_to]
72
+ attachment = upload_file :filename => '/files/rails.png'
73
+ assert_valid attachment
74
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
75
+ assert attachment.image?
76
+ assert !attachment.size.zero?
77
+ #assert_in_delta 4673, attachment.size, 2
78
+ assert_equal 50, attachment.width
79
+ assert_equal 50, attachment.height
80
+ end
81
+
82
+ test_against_subclass :test_should_resize_image, ImageAttachment
83
+
84
+ def test_should_resize_image_with_geometry(klass = ImageOrPdfAttachment)
85
+ attachment_model klass
86
+ assert_equal 'x50', attachment_model.attachment_options[:resize_to]
87
+ attachment = upload_file :filename => '/files/rails.png'
88
+ assert_valid attachment
89
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
90
+ assert attachment.image?
91
+ assert !attachment.size.zero?
92
+ #assert_equal 3915, attachment.size
93
+ assert_equal 39, attachment.width
94
+ assert_equal 50, attachment.height
95
+ end
96
+
97
+ test_against_subclass :test_should_resize_image_with_geometry, ImageOrPdfAttachment
98
+
99
+ def test_should_give_correct_thumbnail_filenames(klass = ImageWithThumbsFileAttachment)
100
+ attachment_model klass
101
+ assert_created 3 do
102
+ attachment = upload_file :filename => '/files/rails.png'
103
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
104
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
105
+
106
+ [attachment, thumb, geo].each { |record| assert_valid record }
107
+
108
+ assert_match /rails\.png$/, attachment.full_filename
109
+ assert_match /rails_geometry\.png$/, attachment.full_filename(:geometry)
110
+ assert_match /rails_thumb\.png$/, attachment.full_filename(:thumb)
111
+ end
112
+ end
113
+
114
+ test_against_subclass :test_should_give_correct_thumbnail_filenames, ImageWithThumbsFileAttachment
115
+
116
+ def test_should_automatically_create_thumbnails(klass = ImageWithThumbsAttachment)
117
+ attachment_model klass
118
+ assert_created 3 do
119
+ attachment = upload_file :filename => '/files/rails.png'
120
+ assert_valid attachment
121
+ assert !attachment.size.zero?
122
+ #assert_equal 1784, attachment.size
123
+ assert_equal 55, attachment.width
124
+ assert_equal 55, attachment.height
125
+ assert_equal 2, attachment.thumbnails.length
126
+ # assert_equal 1.0, attachment.aspect_ratio
127
+
128
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
129
+ assert !thumb.new_record?, thumb.errors.full_messages.join("\n")
130
+ assert !thumb.size.zero?
131
+ #assert_in_delta 4673, thumb.size, 2
132
+ assert_equal 50, thumb.width
133
+ assert_equal 50, thumb.height
134
+ # assert_equal 1.0, thumb.aspect_ratio
135
+
136
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
137
+ assert !geo.new_record?, geo.errors.full_messages.join("\n")
138
+ assert !geo.size.zero?
139
+ #assert_equal 3915, geo.size
140
+ assert_equal 50, geo.width
141
+ assert_equal 50, geo.height
142
+ # assert_equal 1.0, geo.aspect_ratio
143
+ end
144
+ end
145
+
146
+ test_against_subclass :test_should_automatically_create_thumbnails, ImageWithThumbsAttachment
147
+
148
+ # same as above method, but test it on a file model
149
+ test_against_class :test_should_automatically_create_thumbnails, ImageWithThumbsFileAttachment
150
+ test_against_subclass :test_should_automatically_create_thumbnails_on_class, ImageWithThumbsFileAttachment
151
+
152
+ def test_should_use_thumbnail_subclass(klass = ImageWithThumbsClassFileAttachment)
153
+ attachment_model klass
154
+ attachment = nil
155
+ assert_difference ImageThumbnail, :count do
156
+ attachment = upload_file :filename => '/files/rails.png'
157
+ assert_valid attachment
158
+ end
159
+ assert_kind_of ImageThumbnail, attachment.thumbnails.first
160
+ if attachment.thumbnails.first.respond_to?(:parent)
161
+ assert_equal attachment.id, attachment.thumbnails.first.parent.id
162
+ assert_kind_of FileAttachment, attachment.thumbnails.first.parent
163
+ end
164
+ assert_equal 'rails_thumb.png', attachment.thumbnails.first.filename
165
+ assert_equal attachment.thumbnails.first.full_filename, attachment.full_filename(attachment.thumbnails.first.thumbnail),
166
+ "#full_filename does not use thumbnail class' path."
167
+ assert_equal attachment.destroy, attachment
168
+ end
169
+
170
+ test_against_subclass :test_should_use_thumbnail_subclass, ImageWithThumbsClassFileAttachment
171
+
172
+ def test_should_remove_old_thumbnail_files_when_updating(klass = ImageWithThumbsFileAttachment)
173
+ attachment_model klass
174
+ attachment = nil
175
+ assert_created 3 do
176
+ attachment = upload_file :filename => '/files/rails.png'
177
+ end
178
+
179
+ old_filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
180
+
181
+ assert_not_created do
182
+ use_temp_file "files/rails.png" do |file|
183
+ attachment.filename = 'rails2.png'
184
+ attachment.temp_paths.unshift File.join(FIXTURE_PATH, file)
185
+ attachment.save
186
+ new_filenames = [attachment.reload.full_filename] + attachment.thumbnails.collect { |t| t.reload.full_filename }
187
+ new_filenames.each { |f| assert File.exists?(f), "#{f} does not exist" }
188
+ old_filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
189
+ end
190
+ end
191
+ end
192
+
193
+ test_against_subclass :test_should_remove_old_thumbnail_files_when_updating, ImageWithThumbsFileAttachment
194
+
195
+ def test_should_delete_file_when_in_file_system_when_attachment_record_destroyed(klass = ImageWithThumbsFileAttachment)
196
+ attachment_model klass
197
+ attachment = upload_file :filename => '/files/rails.png'
198
+ filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
199
+ filenames.each { |f| assert File.exists?(f), "#{f} never existed to delete on destroy" }
200
+ attachment.destroy
201
+ filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
202
+ end
203
+
204
+ test_against_subclass :test_should_delete_file_when_in_file_system_when_attachment_record_destroyed, ImageWithThumbsFileAttachment
205
+
206
+ def test_should_have_full_filename_method(klass = FileAttachment)
207
+ attachment_model klass
208
+ attachment = upload_file :filename => '/files/rails.png'
209
+ assert_respond_to attachment, :full_filename
210
+ end
211
+
212
+ test_against_subclass :test_should_have_full_filename_method, FileAttachment
213
+
214
+ def test_should_overwrite_old_thumbnail_records_when_updating(klass = ImageWithThumbsAttachment)
215
+ attachment_model klass
216
+ attachment = nil
217
+ assert_created 3 do
218
+ attachment = upload_file :filename => '/files/rails.png'
219
+ end
220
+ assert_not_created do # no new db_file records
221
+ use_temp_file "files/rails.png" do |file|
222
+ attachment.filename = 'rails2.png'
223
+ # The above test (#test_should_have_full_filename_method) to pass before be can set the temp_path below --
224
+ # #temp_path calls #full_filename, which is not getting mixed into the attachment. Maybe we don't need to
225
+ # set temp_path at all?
226
+ #
227
+ # attachment.temp_paths.unshift File.join(FIXTURE_PATH, file)
228
+ attachment.save!
229
+ end
230
+ end
231
+ end
232
+
233
+ test_against_subclass :test_should_overwrite_old_thumbnail_records_when_updating, ImageWithThumbsAttachment
234
+
235
+ def test_should_overwrite_old_thumbnail_records_when_renaming(klass = ImageWithThumbsAttachment)
236
+ attachment_model klass
237
+ attachment = nil
238
+ assert_created 3 do
239
+ attachment = upload_file :class => klass, :filename => '/files/rails.png'
240
+ end
241
+ assert_not_created do # no new db_file records
242
+ attachment.filename = 'rails2.png'
243
+ attachment.save
244
+ assert !attachment.reload.size.zero?
245
+ assert_equal 'rails2.png', attachment.filename
246
+ end
247
+ end
248
+
249
+ test_against_subclass :test_should_overwrite_old_thumbnail_records_when_renaming, ImageWithThumbsAttachment
250
+
251
+ def test_should_handle_jpeg_quality
252
+ attachment_model ImageWithThumbsAttachment
253
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
254
+ full_size = attachment.size
255
+ attachment_model LowerQualityAttachment
256
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
257
+ lq_size = attachment.size
258
+ assert lq_size <= full_size * 0.9, 'Lower-quality JPEG filesize should be congruently smaller'
259
+
260
+ attachment_model ImageWithPerThumbJpegAttachment
261
+ attachment = upload_file :filename => '/files/rails.jpg', :content_type => 'image/jpeg'
262
+ assert_file_jpeg_quality attachment, :thumb, 90
263
+ assert_file_jpeg_quality attachment, :avatar, 85
264
+ assert_file_jpeg_quality attachment, :large, 75
265
+ assert_file_jpeg_quality attachment, nil, 75
266
+ end
267
+ else
268
+ def test_flunk
269
+ puts "RMagick not installed, no tests running"
270
+ end
271
+ end
272
+ end