public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
technoweenie (author)
Wed Dec 13 16:59:38 -0800 2006
attachment_fu / test / fixtures / attachment.rb
100644 84 lines (69 sloc) 2.366 kb
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
class Attachment < ActiveRecord::Base
  @@saves = 0
  cattr_accessor :saves
  has_attachment
  validates_as_attachment
  after_attachment_saved do |record|
    self.saves += 1
  end
end
 
class SmallAttachment < Attachment
  has_attachment :max_size => 1.kilobyte
end
 
class BigAttachment < Attachment
  has_attachment :size => 1.megabyte..2.megabytes
end
 
class PdfAttachment < Attachment
  has_attachment :content_type => 'pdf'
end
 
class DocAttachment < Attachment
  has_attachment :content_type => %w(pdf doc txt)
end
 
class ImageAttachment < Attachment
  has_attachment :content_type => :image, :resize_to => [50,50]
end
 
class ImageOrPdfAttachment < Attachment
  has_attachment :content_type => ['pdf', :image], :resize_to => 'x50'
end
 
class ImageWithThumbsAttachment < Attachment
  has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
  after_resize do |record, img|
    record.aspect_ratio = img.columns.to_f / img.rows.to_f
  end
end
 
class FileAttachment < ActiveRecord::Base
  has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
  validates_as_attachment
end
 
class ImageFileAttachment < FileAttachment
  has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
    :content_type => :image, :resize_to => [50,50]
end
 
class ImageWithThumbsFileAttachment < FileAttachment
  has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
    :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
  after_resize do |record, img|
    record.aspect_ratio = img.columns.to_f / img.rows.to_f
  end
end
 
class ImageWithThumbsClassFileAttachment < FileAttachment
  has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
    :thumbnails => { :thumb => [50, 50] }, :resize_to => [55,55],
    :thumbnail_class => 'ImageThumbnail'
end
 
class ImageThumbnail < FileAttachment
  has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files/thumbnails'
end
 
# no parent
class OrphanAttachment < ActiveRecord::Base
  has_attachment
  validates_as_attachment
end
 
# no filename, no size, no content_type
class MinimalAttachment < ActiveRecord::Base
  has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
  validates_as_attachment
  
  def filename
    "#{id}.file"
  end
end