1 | # This module contains helper methods for displaying and uploading files |
---|
2 | # for attributes created by +FileColumn+'s +file_column+ method. It will be |
---|
3 | # automatically included into ActionView::Base, thereby making this module's |
---|
4 | # methods available in all your views. |
---|
5 | module FileColumnHelper |
---|
6 | |
---|
7 | # Use this helper to create an upload field for a file_column attribute. This will generate |
---|
8 | # an additional hidden field to keep uploaded files during form-redisplays. For example, |
---|
9 | # when called with |
---|
10 | # |
---|
11 | # <%= file_column_field("entry", "image") %> |
---|
12 | # |
---|
13 | # the following HTML will be generated (assuming the form is redisplayed and something has |
---|
14 | # already been uploaded): |
---|
15 | # |
---|
16 | # <input type="hidden" name="entry[image_temp]" value="..." /> |
---|
17 | # <input type="file" name="entry[image]" /> |
---|
18 | # |
---|
19 | # You can use the +option+ argument to pass additional options to the file-field tag. |
---|
20 | # |
---|
21 | # Be sure to set the enclosing form's encoding to 'multipart/form-data', by |
---|
22 | # using something like this: |
---|
23 | # |
---|
24 | # <%= form_tag {:action => "create", ...}, :multipart => true %> |
---|
25 | def file_column_field(object, method, options={}) |
---|
26 | result = ActionView::Helpers::InstanceTag.new(object.dup, method.to_s+"_temp", self).to_input_field_tag("hidden", {}) |
---|
27 | result << ActionView::Helpers::InstanceTag.new(object.dup, method, self).to_input_field_tag("file", options) |
---|
28 | end |
---|
29 | |
---|
30 | # Creates an URL where an uploaded file can be accessed. When called for an Entry object with |
---|
31 | # id 42 (stored in <tt>@entry</tt>) like this |
---|
32 | # |
---|
33 | # <%= url_for_file_column("entry", "image") |
---|
34 | # |
---|
35 | # the following URL will be produced, assuming the file "test.png" has been stored in |
---|
36 | # the "image"-column of an Entry object stored in <tt>@entry</tt>: |
---|
37 | # |
---|
38 | # /entry/image/42/test.png |
---|
39 | # |
---|
40 | # This will produce a valid URL even for temporary uploaded files, e.g. files where the object |
---|
41 | # they are belonging to has not been saved in the database yet. |
---|
42 | # |
---|
43 | # If there is currently no uploaded file stored in the object's column this method will |
---|
44 | # return +nil+. |
---|
45 | # |
---|
46 | # If the optional +suffix+ parameter is given, it will be inserted into the filename |
---|
47 | # before the extension. So if the uploaded file is name "vancouver.jpg" and you have |
---|
48 | # the following code in your view |
---|
49 | # |
---|
50 | # <%= url_for_file_column("entry", "image", "thumb") %> |
---|
51 | # |
---|
52 | # the resulting URL's would point to a file "vancouver-thumb.jpg". This can be used to |
---|
53 | # access different versions of a file as produced by FileColumn::Magick, for example. |
---|
54 | # |
---|
55 | # Note that the object has to be stored in an instance variable. So if +object_name+ is |
---|
56 | # "entry" your object has to be stored in <tt>@entry</tt>. |
---|
57 | def url_for_file_column(object_name, method, suffix=nil) |
---|
58 | object = instance_variable_get("@#{object_name.to_s}") |
---|
59 | relative_path = object.send("#{method}_relative_path", suffix) |
---|
60 | return nil unless relative_path |
---|
61 | url = "" |
---|
62 | url << request.relative_url_root.to_s << "/" |
---|
63 | url << object.send("#{method}_options")[:base_url] << "/" |
---|
64 | url << relative_path |
---|
65 | end |
---|
66 | end |
---|