merged in from code from the other master
[selectricity] / vendor / plugins / attachment_fu / test / fixtures / attachment.rb
1 class Attachment < ActiveRecord::Base
2   @@saves = 0
3   cattr_accessor :saves
4   has_attachment :processor => :rmagick
5   validates_as_attachment
6   after_attachment_saved do |record|
7     self.saves += 1
8   end
9 end
10
11 class SmallAttachment < Attachment
12   has_attachment :max_size => 1.kilobyte
13 end
14
15 class BigAttachment < Attachment
16   has_attachment :size => 1.megabyte..2.megabytes
17 end
18
19 class PdfAttachment < Attachment
20   has_attachment :content_type => 'pdf'
21 end
22
23 class DocAttachment < Attachment
24   has_attachment :content_type => %w(pdf doc txt)
25 end
26
27 class ImageAttachment < Attachment
28   has_attachment :content_type => :image, :resize_to => [50,50]
29 end
30
31 class ImageOrPdfAttachment < Attachment
32   has_attachment :content_type => ['pdf', :image], :resize_to => 'x50'
33 end
34
35 class ImageWithThumbsAttachment < Attachment
36   has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
37   after_resize do |record, img|
38    # record.aspect_ratio = img.columns.to_f / img.rows.to_f
39   end
40 end
41
42 class FileAttachment < ActiveRecord::Base
43   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files', :processor => :rmagick
44   validates_as_attachment
45 end
46
47 class FileAttachmentWithStringId < ActiveRecord::Base
48   set_table_name 'file_attachments_with_string_id'
49   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files', :processor => :rmagick
50   validates_as_attachment
51   
52   before_validation :auto_generate_id
53   before_save :auto_generate_id
54   @@last_id = 0
55   
56   private
57     def auto_generate_id
58       @@last_id += 1
59       self.id = "id_#{@@last_id}"
60     end
61 end
62
63 class FileAttachmentWithUuid < ActiveRecord::Base
64   set_table_name 'file_attachments_with_string_id'
65   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files', :processor => :rmagick, :uuid_primary_key => true
66   validates_as_attachment
67   
68   before_validation :auto_generate_id
69   before_save :auto_generate_id
70   @@last_id = 0
71   
72   private
73     def auto_generate_id
74       @@last_id += 1
75       self.id = "%0127dx" % @@last_id
76     end
77 end
78
79 class ImageFileAttachment < FileAttachment
80   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
81     :content_type => :image, :resize_to => [50,50]
82 end
83
84 class ImageWithThumbsFileAttachment < FileAttachment
85   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
86     :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
87   after_resize do |record, img|
88   #  record.aspect_ratio = img.columns.to_f / img.rows.to_f
89   end
90 end
91
92 class ImageWithThumbsClassFileAttachment < FileAttachment
93   # use file_system_path to test backwards compatibility
94   has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
95     :thumbnails => { :thumb => [50, 50] }, :resize_to => [55,55],
96     :thumbnail_class => 'ImageThumbnail'
97 end
98
99 class ImageThumbnail < FileAttachment
100   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files/thumbnails'
101 end
102
103 # no parent
104 class OrphanAttachment < ActiveRecord::Base
105   has_attachment :processor => :rmagick
106   validates_as_attachment
107 end
108
109 # no filename, no size, no content_type
110 class MinimalAttachment < ActiveRecord::Base
111   has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files', :processor => :rmagick
112   validates_as_attachment
113   
114   def filename
115     "#{id}.file"
116   end
117 end
118
119 begin
120   class ImageScienceAttachment < ActiveRecord::Base
121     has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
122       :processor => :image_science, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
123   end
124 rescue MissingSourceFile
125   puts $!.message
126   puts "no ImageScience"
127 end
128
129 begin
130   class CoreImageAttachment < ActiveRecord::Base
131     has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
132       :processor => :core_image, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
133   end
134 rescue MissingSourceFile
135   puts $!.message
136   puts "no CoreImage"
137 end
138
139 begin
140   class MiniMagickAttachment < ActiveRecord::Base
141     has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
142       :processor => :mini_magick, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
143   end
144 rescue MissingSourceFile
145   puts $!.message
146   puts "no Mini Magick"
147 end
148
149 begin
150   class GD2Attachment < ActiveRecord::Base
151     has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
152       :processor => :gd2, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
153   end
154 rescue MissingSourceFile
155   puts $!.message
156   puts "no GD2"
157 end
158
159
160 begin
161   class MiniMagickAttachment < ActiveRecord::Base
162     has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
163       :processor => :mini_magick, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
164   end
165   class ImageThumbnailCrop < MiniMagickAttachment
166     has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
167     :thumbnails => { :square => "50x50c", :vertical => "30x60c", :horizontal => "60x30c"}
168     
169     # TODO this is a bad duplication, this method is in the MiniMagick Processor
170     def self.calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_height,thumb_aspect)
171     # only crop if image is not smaller in both dimensions
172
173       # special cases, image smaller in one dimension then thumbsize
174       if image_width < thumb_width
175         offset = (image_height / 2) - (thumb_height / 2)
176         command = "#{image_width}x#{thumb_height}+0+#{offset}"
177       elsif image_height < thumb_height
178         offset = (image_width / 2) - (thumb_width / 2)
179         command = "#{thumb_width}x#{image_height}+#{offset}+0"
180
181       # normal thumbnail generation
182       # calculate height and offset y, width is fixed                 
183       elsif (image_aspect <= thumb_aspect or image_width < thumb_width) and image_height > thumb_height
184         height = image_width / thumb_aspect
185         offset = (image_height / 2) - (height / 2)
186         command = "#{image_width}x#{height}+0+#{offset}"
187       # calculate width and offset x, height is fixed
188       else
189         width = image_height * thumb_aspect
190         offset = (image_width / 2) - (width / 2)
191         command = "#{width}x#{image_height}+#{offset}+0"
192       end
193       # crop image
194       command
195     end
196   end
197
198 rescue MissingSourceFile
199 end
200
201
202
203 begin
204   class S3Attachment < ActiveRecord::Base
205     has_attachment :storage => :s3, :processor => :rmagick, :s3_config_path => File.join(File.dirname(__FILE__), '../amazon_s3.yml')
206     validates_as_attachment
207   end
208   
209   class CloudFilesAttachment < ActiveRecord::Base
210     has_attachment :storage => :cloud_files, :processor => :rmagick, :cloudfiles_config_path => File.join(File.dirname(__FILE__), '../rackspace_cloudfiles.yml')
211     validates_as_attachment
212   end
213
214   class S3WithPathPrefixAttachment < S3Attachment
215     has_attachment :storage => :s3, :path_prefix => 'some/custom/path/prefix', :processor => :rmagick
216     validates_as_attachment
217   end
218   
219   class CloudFilesWithPathPrefixAttachment < CloudFilesAttachment
220     has_attachment :storage => :cloud_files, :path_prefix => 'some/custom/path/prefix', :processor => :rmagick
221     validates_as_attachment
222   end
223   
224 rescue
225   puts "S3 error: #{$!}"
226 end

Benjamin Mako Hill || Want to submit a patch?