tdd-attachment_fu 0.9.6

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.
Files changed (49) hide show
  1. data/CHANGELOG +44 -0
  2. data/LICENSE +20 -0
  3. data/README +197 -0
  4. data/Rakefile +22 -0
  5. data/amazon_s3.yml.tpl +17 -0
  6. data/init.rb +16 -0
  7. data/install.rb +7 -0
  8. data/lib/geometry.rb +96 -0
  9. data/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb +211 -0
  10. data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
  11. data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +126 -0
  12. data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +394 -0
  13. data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +65 -0
  14. data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +62 -0
  15. data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +80 -0
  16. data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +138 -0
  17. data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +65 -0
  18. data/lib/technoweenie/attachment_fu.rb +514 -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 +70 -0
  26. data/test/database.yml +18 -0
  27. data/test/extra_attachment_test.rb +67 -0
  28. data/test/fixtures/attachment.rb +249 -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 +50 -0
  35. data/test/processors/gd2_test.rb +44 -0
  36. data/test/processors/image_science_test.rb +47 -0
  37. data/test/processors/mini_magick_test.rb +116 -0
  38. data/test/processors/rmagick_test.rb +267 -0
  39. data/test/schema.rb +134 -0
  40. data/test/test_helper.rb +153 -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 +103 -0
@@ -0,0 +1,267 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
+
3
+ class RmagickTest < Test::Unit::TestCase
4
+ attachment_model Attachment
5
+
6
+ if Object.const_defined?(:Magick)
7
+ def test_should_create_image_from_uploaded_file
8
+ assert_created do
9
+ attachment = upload_file :filename => '/files/rails.png'
10
+ assert_valid attachment
11
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
12
+ assert attachment.image?
13
+ assert !attachment.size.zero?
14
+ #assert_equal 1784, attachment.size
15
+ assert_equal 50, attachment.width
16
+ assert_equal 64, attachment.height
17
+ assert_equal '50x64', attachment.image_size
18
+ end
19
+ end
20
+
21
+ def test_should_create_image_from_uploaded_file_with_custom_content_type
22
+ assert_created do
23
+ attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
24
+ assert_valid attachment
25
+ assert !attachment.image?
26
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
27
+ assert !attachment.size.zero?
28
+ #assert_equal 1784, attachment.size
29
+ assert_nil attachment.width
30
+ assert_nil attachment.height
31
+ assert_equal [], attachment.thumbnails
32
+ end
33
+ end
34
+
35
+ def test_should_create_thumbnail
36
+ attachment = upload_file :filename => '/files/rails.png'
37
+
38
+ assert_created do
39
+ basename, ext = attachment.filename.split '.'
40
+ thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', 50, 50)
41
+ assert_valid thumbnail
42
+ assert !thumbnail.size.zero?
43
+ #assert_in_delta 4673, thumbnail.size, 2
44
+ assert_equal 50, thumbnail.width
45
+ assert_equal 50, thumbnail.height
46
+ assert_equal [thumbnail.id], attachment.thumbnails.collect(&:id)
47
+ assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
48
+ assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
49
+ end
50
+ end
51
+
52
+ def test_should_create_thumbnail_with_geometry_strings
53
+ attachment = upload_file :filename => '/files/rails.png'
54
+
55
+ assert_created do
56
+ basename, ext = attachment.filename.split '.'
57
+ { 'x50' => [39, 50], '25x25!' => [25, 25] }.each do |geo, (w, h)|
58
+ thumbnail = attachment.create_or_update_thumbnail(attachment.create_temp_file, 'thumb', geo)
59
+ assert_valid thumbnail
60
+ assert !thumbnail.size.zero?
61
+ assert_equal w, thumbnail.width
62
+ assert_equal h, thumbnail.height
63
+ assert_equal [thumbnail], attachment.thumbnails
64
+ assert_equal attachment.id, thumbnail.parent_id if thumbnail.respond_to?(:parent_id)
65
+ assert_equal "#{basename}_thumb.#{ext}", thumbnail.filename
66
+ end
67
+ end
68
+ end
69
+
70
+ def test_should_resize_image(klass = ImageAttachment)
71
+ attachment_model klass
72
+ assert_equal [50, 50], attachment_model.attachment_options[:resize_to]
73
+ attachment = upload_file :filename => '/files/rails.png'
74
+ assert_valid attachment
75
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
76
+ assert attachment.image?
77
+ assert !attachment.size.zero?
78
+ #assert_in_delta 4673, attachment.size, 2
79
+ assert_equal 50, attachment.width
80
+ assert_equal 50, attachment.height
81
+ end
82
+
83
+ test_against_subclass :test_should_resize_image, ImageAttachment
84
+
85
+ def test_should_resize_image_with_geometry(klass = ImageOrPdfAttachment)
86
+ attachment_model klass
87
+ assert_equal 'x50', attachment_model.attachment_options[:resize_to]
88
+ attachment = upload_file :filename => '/files/rails.png'
89
+ assert_valid attachment
90
+ assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
91
+ assert attachment.image?
92
+ assert !attachment.size.zero?
93
+ #assert_equal 3915, attachment.size
94
+ assert_equal 39, attachment.width
95
+ assert_equal 50, attachment.height
96
+ end
97
+
98
+ test_against_subclass :test_should_resize_image_with_geometry, ImageOrPdfAttachment
99
+
100
+ def test_should_give_correct_thumbnail_filenames(klass = ImageWithThumbsFileAttachment)
101
+ attachment_model klass
102
+ assert_created 3 do
103
+ attachment = upload_file :filename => '/files/rails.png'
104
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
105
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
106
+
107
+ [attachment, thumb, geo].each { |record| assert_valid record }
108
+
109
+ assert_match /rails\.png$/, attachment.full_filename
110
+ assert_match /rails_geometry\.png$/, attachment.full_filename(:geometry)
111
+ assert_match /rails_thumb\.png$/, attachment.full_filename(:thumb)
112
+ end
113
+ end
114
+
115
+ test_against_subclass :test_should_give_correct_thumbnail_filenames, ImageWithThumbsFileAttachment
116
+
117
+ def test_should_automatically_create_thumbnails(klass = ImageWithThumbsAttachment)
118
+ attachment_model klass
119
+ assert_created 3 do
120
+ attachment = upload_file :filename => '/files/rails.png'
121
+ assert_valid attachment
122
+ assert !attachment.size.zero?
123
+ #assert_equal 1784, attachment.size
124
+ assert_equal 55, attachment.width
125
+ assert_equal 55, attachment.height
126
+ assert_equal 2, attachment.thumbnails.length
127
+ # assert_equal 1.0, attachment.aspect_ratio
128
+
129
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
130
+ assert !thumb.new_record?, thumb.errors.full_messages.join("\n")
131
+ assert !thumb.size.zero?
132
+ #assert_in_delta 4673, thumb.size, 2
133
+ assert_equal 50, thumb.width
134
+ assert_equal 50, thumb.height
135
+ # assert_equal 1.0, thumb.aspect_ratio
136
+
137
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
138
+ assert !geo.new_record?, geo.errors.full_messages.join("\n")
139
+ assert !geo.size.zero?
140
+ #assert_equal 3915, geo.size
141
+ assert_equal 50, geo.width
142
+ assert_equal 50, geo.height
143
+ # assert_equal 1.0, geo.aspect_ratio
144
+ end
145
+ end
146
+
147
+ test_against_subclass :test_should_automatically_create_thumbnails, ImageWithThumbsAttachment
148
+
149
+ # same as above method, but test it on a file model
150
+ test_against_class :test_should_automatically_create_thumbnails, ImageWithThumbsFileAttachment
151
+ test_against_subclass :test_should_automatically_create_thumbnails_on_class, ImageWithThumbsFileAttachment
152
+
153
+ def test_should_use_thumbnail_subclass(klass = ImageWithThumbsClassFileAttachment)
154
+ attachment_model klass
155
+ attachment = nil
156
+ assert_difference ImageThumbnail, :count do
157
+ attachment = upload_file :filename => '/files/rails.png'
158
+ assert_valid attachment
159
+ end
160
+ assert_kind_of ImageThumbnail, attachment.thumbnails.first
161
+ if attachment.thumbnails.first.respond_to?(:parent)
162
+ assert_equal attachment.id, attachment.thumbnails.first.parent.id
163
+ assert_kind_of FileAttachment, attachment.thumbnails.first.parent
164
+ end
165
+ assert_equal 'rails_thumb.png', attachment.thumbnails.first.filename
166
+ assert_equal attachment.thumbnails.first.full_filename, attachment.full_filename(attachment.thumbnails.first.thumbnail),
167
+ "#full_filename does not use thumbnail class' path."
168
+ assert_equal attachment.destroy, attachment
169
+ end
170
+
171
+ test_against_subclass :test_should_use_thumbnail_subclass, ImageWithThumbsClassFileAttachment
172
+
173
+ def test_should_remove_old_thumbnail_files_when_updating(klass = ImageWithThumbsFileAttachment)
174
+ attachment_model klass
175
+ attachment = nil
176
+ assert_created 3 do
177
+ attachment = upload_file :filename => '/files/rails.png'
178
+ end
179
+
180
+ old_filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
181
+
182
+ assert_not_created do
183
+ use_temp_file "files/rails.png" do |file|
184
+ attachment.filename = 'rails2.png'
185
+ attachment.temp_paths.unshift File.join(fixture_path, file)
186
+ attachment.save
187
+ new_filenames = [attachment.reload.full_filename] + attachment.thumbnails.collect { |t| t.reload.full_filename }
188
+ new_filenames.each { |f| assert File.exists?(f), "#{f} does not exist" }
189
+ old_filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
190
+ end
191
+ end
192
+ end
193
+
194
+ test_against_subclass :test_should_remove_old_thumbnail_files_when_updating, ImageWithThumbsFileAttachment
195
+
196
+ def test_should_delete_file_when_in_file_system_when_attachment_record_destroyed(klass = ImageWithThumbsFileAttachment)
197
+ attachment_model klass
198
+ attachment = upload_file :filename => '/files/rails.png'
199
+ filenames = [attachment.full_filename] + attachment.thumbnails.collect(&:full_filename)
200
+ filenames.each { |f| assert File.exists?(f), "#{f} never existed to delete on destroy" }
201
+ attachment.destroy
202
+ filenames.each { |f| assert !File.exists?(f), "#{f} still exists" }
203
+ end
204
+
205
+ test_against_subclass :test_should_delete_file_when_in_file_system_when_attachment_record_destroyed, ImageWithThumbsFileAttachment
206
+
207
+ def test_should_have_full_filename_method(klass = FileAttachment)
208
+ attachment_model klass
209
+ attachment = upload_file :filename => '/files/rails.png'
210
+ assert_respond_to attachment, :full_filename
211
+ end
212
+
213
+ test_against_subclass :test_should_have_full_filename_method, FileAttachment
214
+
215
+ def test_should_overwrite_old_thumbnail_records_when_updating(klass = ImageWithThumbsAttachment)
216
+ attachment_model klass
217
+ attachment = nil
218
+ assert_created 3 do
219
+ attachment = upload_file :filename => '/files/rails.png'
220
+ end
221
+ assert_not_created do # no new db_file records
222
+ use_temp_file "files/rails.png" do |file|
223
+ attachment.filename = 'rails2.png'
224
+ # The above test (#test_should_have_full_filename_method) to pass before be can set the temp_path below --
225
+ # #temp_path calls #full_filename, which is not getting mixed into the attachment. Maybe we don't need to
226
+ # set temp_path at all?
227
+ #
228
+ # attachment.temp_paths.unshift File.join(fixture_path, file)
229
+ attachment.save!
230
+ end
231
+ end
232
+ end
233
+
234
+ test_against_subclass :test_should_overwrite_old_thumbnail_records_when_updating, ImageWithThumbsAttachment
235
+
236
+ def test_should_overwrite_old_thumbnail_records_when_renaming(klass = ImageWithThumbsAttachment)
237
+ attachment_model klass
238
+ attachment = nil
239
+ assert_created 3 do
240
+ attachment = upload_file :class => klass, :filename => '/files/rails.png'
241
+ end
242
+ assert_not_created do # no new db_file records
243
+ attachment.filename = 'rails2.png'
244
+ attachment.save
245
+ assert !attachment.reload.size.zero?
246
+ assert_equal 'rails2.png', attachment.filename
247
+ end
248
+ end
249
+
250
+ test_against_subclass :test_should_overwrite_old_thumbnail_records_when_renaming, ImageWithThumbsAttachment
251
+
252
+ def test_should_handle_jpeg_quality
253
+ attachment_model ImageWithThumbsAttachment
254
+ attachment = upload_file :filename => '/files/rails.jpg'
255
+ full_size = attachment.size
256
+ attachment_model LowerQualityAttachment
257
+ attachment = upload_file :filename => '/files/rails.jpg'
258
+ lq_size = attachment.size
259
+ puts "FULL: #{full_size} - LQ: #{lq_size}"
260
+ assert lq_size <= full_size * 0.9, 'Lower-quality JPEG filesize should be congruently smaller'
261
+ end
262
+ else
263
+ def test_flunk
264
+ puts "RMagick not installed, no tests running"
265
+ end
266
+ end
267
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,134 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :attachments, :force => true do |t|
3
+ t.column :db_file_id, :integer
4
+ t.column :parent_id, :integer
5
+ t.column :thumbnail, :string
6
+ t.column :filename, :string, :limit => 255
7
+ t.column :content_type, :string, :limit => 255
8
+ t.column :size, :integer
9
+ t.column :width, :integer
10
+ t.column :height, :integer
11
+ t.column :aspect_ratio, :float
12
+ end
13
+
14
+ create_table :file_attachments, :force => true do |t|
15
+ t.column :parent_id, :integer
16
+ t.column :thumbnail, :string
17
+ t.column :filename, :string, :limit => 255
18
+ t.column :content_type, :string, :limit => 255
19
+ t.column :size, :integer
20
+ t.column :width, :integer
21
+ t.column :height, :integer
22
+ t.column :type, :string
23
+ t.column :aspect_ratio, :float
24
+ end
25
+
26
+ create_table :file_attachments_with_string_id, :id => false, :force => true do |t|
27
+ t.column :id, :string
28
+ t.column :parent_id, :string
29
+ t.column :thumbnail, :string
30
+ t.column :filename, :string, :limit => 255
31
+ t.column :content_type, :string, :limit => 255
32
+ t.column :size, :integer
33
+ t.column :width, :integer
34
+ t.column :height, :integer
35
+ t.column :type, :string
36
+ t.column :aspect_ratio, :float
37
+ end
38
+
39
+ create_table :gd2_attachments, :force => true do |t|
40
+ t.column :parent_id, :integer
41
+ t.column :thumbnail, :string
42
+ t.column :filename, :string, :limit => 255
43
+ t.column :content_type, :string, :limit => 255
44
+ t.column :size, :integer
45
+ t.column :width, :integer
46
+ t.column :height, :integer
47
+ t.column :type, :string
48
+ end
49
+
50
+ create_table :image_science_attachments, :force => true do |t|
51
+ t.column :parent_id, :integer
52
+ t.column :thumbnail, :string
53
+ t.column :filename, :string, :limit => 255
54
+ t.column :content_type, :string, :limit => 255
55
+ t.column :size, :integer
56
+ t.column :width, :integer
57
+ t.column :height, :integer
58
+ t.column :type, :string
59
+ end
60
+
61
+ create_table :core_image_attachments, :force => true do |t|
62
+ t.column :parent_id, :integer
63
+ t.column :thumbnail, :string
64
+ t.column :filename, :string, :limit => 255
65
+ t.column :content_type, :string, :limit => 255
66
+ t.column :size, :integer
67
+ t.column :width, :integer
68
+ t.column :height, :integer
69
+ t.column :type, :string
70
+ end
71
+
72
+ create_table :mini_magick_attachments, :force => true do |t|
73
+ t.column :parent_id, :integer
74
+ t.column :thumbnail, :string
75
+ t.column :filename, :string, :limit => 255
76
+ t.column :content_type, :string, :limit => 255
77
+ t.column :size, :integer
78
+ t.column :width, :integer
79
+ t.column :height, :integer
80
+ t.column :type, :string
81
+ end
82
+
83
+ create_table :mini_magick_attachments, :force => true do |t|
84
+ t.column :parent_id, :integer
85
+ t.column :thumbnail, :string
86
+ t.column :filename, :string, :limit => 255
87
+ t.column :content_type, :string, :limit => 255
88
+ t.column :size, :integer
89
+ t.column :width, :integer
90
+ t.column :height, :integer
91
+ t.column :type, :string
92
+ end
93
+
94
+ create_table :orphan_attachments, :force => true do |t|
95
+ t.column :db_file_id, :integer
96
+ t.column :filename, :string, :limit => 255
97
+ t.column :content_type, :string, :limit => 255
98
+ t.column :size, :integer
99
+ end
100
+
101
+ create_table :minimal_attachments, :force => true do |t|
102
+ t.column :size, :integer
103
+ t.column :content_type, :string, :limit => 255
104
+ end
105
+
106
+ create_table :db_files, :force => true do |t|
107
+ t.column :data, :binary
108
+ end
109
+
110
+ create_table :s3_attachments, :force => true do |t|
111
+ t.column :parent_id, :integer
112
+ t.column :thumbnail, :string
113
+ t.column :filename, :string, :limit => 255
114
+ t.column :content_type, :string, :limit => 255
115
+ t.column :size, :integer
116
+ t.column :width, :integer
117
+ t.column :height, :integer
118
+ t.column :type, :string
119
+ t.column :aspect_ratio, :float
120
+ end
121
+
122
+ create_table :cloud_files_attachments, :force => true do |t|
123
+ t.column :parent_id, :integer
124
+ t.column :thumbnail, :string
125
+ t.column :filename, :string, :limit => 255
126
+ t.column :content_type, :string, :limit => 255
127
+ t.column :size, :integer
128
+ t.column :width, :integer
129
+ t.column :height, :integer
130
+ t.column :type, :string
131
+ t.column :aspect_ratio, :float
132
+ end
133
+
134
+ end
@@ -0,0 +1,153 @@
1
+
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ ENV['RAILS_ENV'] = 'test'
5
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
6
+
7
+ require 'test/unit'
8
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
9
+ require 'active_record/fixtures'
10
+ require 'action_controller/test_process'
11
+
12
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
13
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
14
+
15
+ db_adapter = ENV['DB']
16
+
17
+ # no db passed, try one of these fine config-free DBs before bombing.
18
+ db_adapter ||=
19
+ begin
20
+ require 'rubygems'
21
+ require 'sqlite'
22
+ 'sqlite'
23
+ rescue MissingSourceFile
24
+ begin
25
+ require 'sqlite3'
26
+ 'sqlite3'
27
+ rescue MissingSourceFile
28
+ end
29
+ end
30
+
31
+ if db_adapter.nil?
32
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
33
+ end
34
+
35
+ ActiveRecord::Base.establish_connection(config[db_adapter])
36
+
37
+ load(File.dirname(__FILE__) + "/schema.rb")
38
+
39
+ Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
40
+ $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
41
+
42
+ class Test::Unit::TestCase #:nodoc:
43
+ include ActionController::TestProcess
44
+ def create_fixtures(*table_names)
45
+ if block_given?
46
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
47
+ else
48
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
49
+ end
50
+ end
51
+
52
+ def setup
53
+ Attachment.saves = 0
54
+ DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
55
+ attachment_model self.class.attachment_model
56
+ end
57
+
58
+ def teardown
59
+ FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
60
+ # Files generated by random_tempfile_filename
61
+ FileUtils.rm_rf Dir['[0-9]*.{png,jpg}']
62
+ end
63
+
64
+ self.use_transactional_fixtures = true
65
+ self.use_instantiated_fixtures = false
66
+
67
+ def self.attachment_model(klass = nil)
68
+ @attachment_model = klass if klass
69
+ @attachment_model
70
+ end
71
+
72
+ def self.test_against_class(test_method, klass, subclass = false)
73
+ define_method("#{test_method}_on_#{:sub if subclass}class") do
74
+ klass = Class.new(klass) if subclass
75
+ attachment_model klass
76
+ send test_method, klass
77
+ end
78
+ end
79
+
80
+ def self.test_against_subclass(test_method, klass)
81
+ test_against_class test_method, klass, true
82
+ end
83
+
84
+ protected
85
+ def upload_file(options = {})
86
+ use_temp_file options[:filename] do |file|
87
+ att = attachment_model.create :uploaded_data => fixture_file_upload(file, options[:content_type] || 'image/png')
88
+ att.reload unless att.new_record?
89
+ return att
90
+ end
91
+ end
92
+
93
+ def upload_merb_file(options = {})
94
+ use_temp_file options[:filename] do |file|
95
+ att = attachment_model.create :uploaded_data => {"size" => file.size, "content_type" => options[:content_type] || 'image/png', "filename" => file, 'tempfile' => fixture_file_upload(file, options[:content_type] || 'image/png')}
96
+ att.reload unless att.new_record?
97
+ return att
98
+ end
99
+ end
100
+
101
+ def use_temp_file(fixture_filename)
102
+ temp_path = File.join('/tmp', File.basename(fixture_filename))
103
+ FileUtils.mkdir_p File.join(fixture_path, 'tmp')
104
+ FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
105
+ yield temp_path
106
+ ensure
107
+ FileUtils.rm_rf File.join(fixture_path, 'tmp')
108
+ end
109
+
110
+ def assert_created(num = 1)
111
+ assert_difference attachment_model.base_class, :count, num do
112
+ if attachment_model.included_modules.include? DbFile
113
+ assert_difference DbFile, :count, num do
114
+ yield
115
+ end
116
+ else
117
+ yield
118
+ end
119
+ end
120
+ end
121
+
122
+ def assert_not_created
123
+ assert_created(0) { yield }
124
+ end
125
+
126
+ def should_reject_by_size_with(klass)
127
+ attachment_model klass
128
+ assert_not_created do
129
+ attachment = upload_file :filename => '/files/rails.png'
130
+ assert attachment.new_record?
131
+ assert attachment.errors.on(:size)
132
+ assert_nil attachment.db_file if attachment.respond_to?(:db_file)
133
+ end
134
+ end
135
+
136
+ def assert_difference(object, method = nil, difference = 1)
137
+ initial_value = object.send(method)
138
+ yield
139
+ assert_equal initial_value + difference, object.send(method)
140
+ end
141
+
142
+ def assert_no_difference(object, method, &block)
143
+ assert_difference object, method, 0, &block
144
+ end
145
+
146
+ def attachment_model(klass = nil)
147
+ @attachment_model = klass if klass
148
+ @attachment_model
149
+ end
150
+ end
151
+
152
+ require File.join(File.dirname(__FILE__), 'fixtures/attachment')
153
+ require File.join(File.dirname(__FILE__), 'base_attachment_tests')
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
+
3
+ class ValidationTest < Test::Unit::TestCase
4
+ def test_should_invalidate_big_files
5
+ @attachment = SmallAttachment.new
6
+ assert !@attachment.valid?
7
+ assert @attachment.errors.on(:size)
8
+
9
+ @attachment.size = 2000
10
+ assert !@attachment.valid?
11
+ assert @attachment.errors.on(:size), @attachment.errors.full_messages.to_sentence
12
+
13
+ @attachment.size = 1000
14
+ assert !@attachment.valid?
15
+ assert_nil @attachment.errors.on(:size)
16
+ end
17
+
18
+ def test_should_invalidate_small_files
19
+ @attachment = BigAttachment.new
20
+ assert !@attachment.valid?
21
+ assert @attachment.errors.on(:size)
22
+
23
+ @attachment.size = 2000
24
+ assert !@attachment.valid?
25
+ assert @attachment.errors.on(:size), @attachment.errors.full_messages.to_sentence
26
+
27
+ @attachment.size = 1.megabyte
28
+ assert !@attachment.valid?
29
+ assert_nil @attachment.errors.on(:size)
30
+ end
31
+
32
+ def test_should_validate_content_type
33
+ @attachment = PdfAttachment.new
34
+ assert !@attachment.valid?
35
+ assert @attachment.errors.on(:content_type)
36
+
37
+ @attachment.content_type = 'foo'
38
+ assert !@attachment.valid?
39
+ assert @attachment.errors.on(:content_type)
40
+
41
+ @attachment.content_type = 'pdf'
42
+ assert !@attachment.valid?
43
+ assert_nil @attachment.errors.on(:content_type)
44
+ end
45
+
46
+ def test_should_require_filename
47
+ @attachment = Attachment.new
48
+ assert !@attachment.valid?
49
+ assert @attachment.errors.on(:filename)
50
+
51
+ @attachment.filename = 'foo'
52
+ assert !@attachment.valid?
53
+ assert_nil @attachment.errors.on(:filename)
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Color
5
+
6
+ def greyscale(color = nil, intensity = 1.00)
7
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
8
+
9
+ color = OSX::CIColor.colorWithString("1.0 1.0 1.0 1.0") unless color
10
+
11
+ @original.color_monochrome :inputColor => color, :inputIntensity => intensity do |greyscale|
12
+ @target = greyscale
13
+ end
14
+ end
15
+
16
+ def sepia(intensity = 1.00)
17
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
18
+
19
+ @original.sepia_tone :inputIntensity => intensity do |sepia|
20
+ @target = sepia
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Effects
5
+
6
+ def spotlight(position, points_at, brightness, concentration, color)
7
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
8
+
9
+ @original.spot_light :inputLightPosition => vector3(*position), :inputLightPointsAt => vector3(*points_at),
10
+ :inputBrightness => brightness, :inputConcentration => concentration, :inputColor => color do |spot|
11
+ @target = spot
12
+ end
13
+ end
14
+
15
+ def edges(intensity = 1.00)
16
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
17
+
18
+ @original.edges :inputIntensity => intensity do |edged|
19
+ @target = edged
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def vector3(x, y, w)
26
+ OSX::CIVector.vectorWithX_Y_Z(x, y, w)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Perspective
5
+
6
+ def perspective(top_left, top_right, bottom_left, bottom_right)
7
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
8
+
9
+ @original.perspective_transform :inputTopLeft => top_left, :inputTopRight => top_right, :inputBottomLeft => bottom_left, :inputBottomRight => bottom_right do |transformed|
10
+ @target = transformed
11
+ end
12
+ end
13
+
14
+ def perspective_tiled(top_left, top_right, bottom_left, bottom_right)
15
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
16
+
17
+ @original.perspective_tile :inputTopLeft => top_left, :inputTopRight => top_right, :inputBottomLeft => bottom_left, :inputBottomRight => bottom_right do |tiled|
18
+ @target = tiled
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end