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 !
add bad ass readme doc [fowlduck]

git-svn-id: 
http://svn.techno-weenie.net/projects/plugins/attachment_fu@2923 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Tue Jun 26 18:14:35 -0700 2007
commit  1b498f4ea0b4d372e0418e7652c82efcf497e224
tree    239920f385a2091721b994997815384e35da77c5
parent  460d5782c32f6fa6ee3b4265999f1a41bdae143b
0
...
1
2
3
4
 
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1,6 +1,162 @@
0
 attachment-fu
0
 =====================
0
 
0
-It works! Mike Clark writes a mean tutorial:
0
+attachment_fu is a plugin by Rick Olson (aka technoweenie <http://techno-weenie.net>) and is the successor to acts_as_attachment. To get a basic run-through of its capabilities, check out Mike Clark's tutorial <http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu>.
0
 
0
-http://clarkware.com/cgi/blosxom/2007/02/24#FileUploadFu
0
\ No newline at end of file
0
+
0
+attachment_fu functionality
0
+===========================
0
+
0
+attachment_fu facilitates file uploads in Ruby on Rails. There are a few storage options for the actual file data, but the plugin always at a minimum stores metadata for each file in the database.
0
+
0
+There are three storage options for files uploaded through attachment_fu:
0
+ File system
0
+ Database file
0
+ Amazon S3
0
+
0
+Each method of storage many options associated with it that will be covered in the following section. Something to note, however, is that the Amazon S3 storage requires you to modify config/amazon_s3.yml and the Database file storage requires an extra table.
0
+
0
+
0
+attachment_fu models
0
+====================
0
+
0
+For all three of these storage options a table of metadata is required. This table will contain information about the file (hence the 'meta') and its location. This table has no restrictions on naming, unlike the extra table required for database storage, which must have a table name of db_files (and by convention a model of DbFile).
0
+
0
+In the model there are two methods made available by this plugins: has_attachment and validates_as_attachment.
0
+
0
+has_attachment(options = {})
0
+ This method accepts the options in a hash:
0
+ :content_type # Allowed content types.
0
+ # Allows all by default. Use :image to allow all standard image types.
0
+ :min_size # Minimum size allowed.
0
+ # 1 byte is the default.
0
+ :max_size # Maximum size allowed.
0
+ # 1.megabyte is the default.
0
+ :size # Range of sizes allowed.
0
+ # (1..1.megabyte) is the default. This overrides the :min_size and :max_size options.
0
+ :resize_to # Used by RMagick to resize images.
0
+ # Pass either an array of width/height, or a geometry string.
0
+ :thumbnails # Specifies a set of thumbnails to generate.
0
+ # This accepts a hash of filename suffixes and RMagick resizing options.
0
+ # This option need only be included if you want thumbnailing.
0
+ :thumbnail_class # Set which model class to use for thumbnails.
0
+ # This current attachment class is used by default.
0
+ :path_prefix # path to store the uploaded files.
0
+ # Uses public/#{table_name} by default for the filesystem, and just #{table_name} for the S3 backend.
0
+ # Setting this sets the :storage to :file_system.
0
+ :storage # Specifies the storage system to use..
0
+ # Defaults to :db_system. Options are :file_system, :db_file, and :s3.
0
+ :processor # Sets the image processor to use for resizing of the attached image.
0
+ # Options include ImageScience, Rmagick, and MiniMagick. Default is whatever is installed.
0
+
0
+
0
+ Examples:
0
+ has_attachment :max_size => 1.kilobyte
0
+ has_attachment :size => 1.megabyte..2.megabytes
0
+ has_attachment :content_type => 'application/pdf'
0
+ has_attachment :content_type => ['application/pdf', 'application/msword', 'text/plain']
0
+ has_attachment :content_type => :image, :resize_to => [50,50]
0
+ has_attachment :content_type => ['application/pdf', :image], :resize_to => 'x50'
0
+ has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
0
+ has_attachment :storage => :file_system, :path_prefix => 'public/files'
0
+ has_attachment :storage => :file_system, :path_prefix => 'public/files',
0
+ :content_type => :image, :resize_to => [50,50]
0
+ has_attachment :storage => :file_system, :path_prefix => 'public/files',
0
+ :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
0
+ has_attachment :storage => :s3
0
+
0
+validates_as_attachment
0
+ This method prevents files outside of the valid range (:min_size to :max_size, or the :size range) from being saved. It does not however, halt the upload of such files. They will be uploaded into memory regardless of size before validation.
0
+
0
+ Example:
0
+ validates_as_attachment
0
+
0
+
0
+attachment_fu migrations
0
+========================
0
+
0
+Fields for attachment_fu metadata tables...
0
+ in general:
0
+ size, :integer # file size in bytes
0
+ content_type, :string # mime type, ex: application/mp3
0
+ filename, :string # sanitized filename
0
+ that reference images:
0
+ height, :integer # in pixels
0
+ width, :integer # in pixels
0
+ that reference images that will be thumbnailed:
0
+ parent_id, :integer # id of parent image (on the same table, a self-referencing foreign-key).
0
+ # Only populated if the current object is a thumbnail.
0
+ thumbnail, :string # the 'type' of thumbnail this attachment record describes.
0
+ # Only populated if the current object is a thumbnail.
0
+ # Usage:
0
+ # [ In Model 'Avatar' ]
0
+ # has_attachment :content_type => :image,
0
+ # :storage => :file_system,
0
+ # :max_size => 500.kilobytes,
0
+ # :resize_to => '320x200>',
0
+ # :thumbnails => { :small => '10x10>',
0
+ # :thumb => '100x100>' }
0
+ # [ Elsewhere ]
0
+ # @user.avatar.thumbnails.first.thumbnail #=> 'small'
0
+ that reference files stored in the database (:db_file):
0
+ db_file_id, :integer # id of the file in the database (foreign key)
0
+
0
+Field for attachment_fu db_files table:
0
+ data, :binary # binary file data, for use in database file storage
0
+
0
+
0
+attachment_fu views
0
+===================
0
+
0
+There are two main views tasks that will be directly affected by attachment_fu: upload forms and displaying uploaded images.
0
+
0
+There are two parts of the upload form that differ from typical usage.
0
+ 1. Include ':multipart => true' in the html options of the form_for tag.
0
+ Example:
0
+ <% form_for(:attachment_metadata, :url => { :action => "create" }, :html => { :multipart => true }) do |form| %>
0
+
0
+ 2. Use the file_field helper with :uploaded_data as the field name.
0
+ Example:
0
+ <%= form.file_field :uploaded_data %>
0
+
0
+Displaying uploaded images is made easy by the public_filename method of the ActiveRecord attachment objects using file system and s3 storage.
0
+
0
+public_filename(thumbnail = nil)
0
+ Returns the public path to the file. If a thumbnail prefix is specified it will return the public file path to the corresponding thumbnail.
0
+ Examples:
0
+ attachment_obj.public_filename #=> /attachments/2/file.jpg
0
+ attachment_obj.public_filename(:thumb) #=> /attachments/2/file_thumb.jpg
0
+ attachment_obj.public_filename(:small) #=> /attachments/2/file_small.jpg
0
+
0
+When serving files from database storage, doing more than simply downloading the file is beyond the scope of this document.
0
+
0
+
0
+attachment_fu controllers
0
+=========================
0
+
0
+There are two considerations to take into account when using attachment_fu in controllers.
0
+
0
+The first is when the files have no publicly accessible path and need to be downloaded through an action.
0
+
0
+Example:
0
+ def readme
0
+ send_file '/path/to/readme.txt', :type => 'plain/text', :disposition => 'inline'
0
+ end
0
+
0
+See the possible values for send_file for reference.
0
+
0
+
0
+The second is when saving the file when submitted from a form.
0
+Example in view:
0
+ <%= form.file_field :attachable, :uploaded_data %>
0
+
0
+Example in controller:
0
+ def create
0
+ @attachable_file = AttachmentMetadataModel.new(params[:attachable])
0
+ if @attachable_file.save
0
+ flash[:notice] = 'Attachment was successfully created.'
0
+ redirect_to attachable_url(@attachable_file)
0
+ else
0
+ render :action => :new
0
+ end
0
+ end

Comments

    No one has commented yet.