In attachment_fu you can have attachment file pathes partitioned: uploaded file.jpg goes to files/0000/0167 directory, where 00000167 is internal ID of the file. With this fork you have some control over it:
Now you can specify partitioning strategy as lambda in has_attachment
statement, like this:
class Attachment < ActiveRecord::Base
has_attachment :storage => :file_system, :path_prefix => 'files/issues',
:partition => lambda {|a| (a.thumbnail ? a.parent : a).document_id}
belongs_to :document, :polymorphic => true
...
Attachment model object is passed to lambda. In this example we have Issue
model having many Attachments via polymorphic association, and we want
attachment files pathes to be like ‘files/issues/67/my_file.doc’, where 67 is
attachment’s issue id. The lambda can also return array of strings, then
['a', 'b'] will become path ‘files/issues/a/b’ in this case. This lambda will fit
if you use thumbnails; if you don’t it will be just lambda {|a| a.document_id}.
Note that partitioning is relevant only to :file_system storage.
Ok this works great but I’m having a problem with the thumbnails not being stored in the proper directory.
So for example my photos get stored in:
/photos/ /my_photo.jpg
But my thumbnails get stored here:
/photos/my_photo_thumb.jpg
I’m trying to get the thumbnails and photos to be stored in the same directory like they were before I used the lambda partition thing.
I hope this made sense.. any help would be really great!
Thanks,
Kirk
/photos/ID_HERE/my_photo.jpg << — for above comment
Hi Kirk!
Thanks for the bugreport. I don’t use attachment_fu’s thumbnails yet so I didn’t see it. I tried to make a fix and published it, please check. (sorry no time currently to check it myself – need to install image processor and throw some code :)
Best regards,
Artem
Hi Kirk!
The problem is that thumbnail object calls partition lambda separately from its main (parent) attachment object, and thumbnail object doesn’t have link to document.
What you need is lambda correction:
:partition => lambda {|a| (a.thumbnail ? a.parent : a).document_id}
I’m going to correct example in this article too.
Best regards,
Artem
Works wonderfully now,
Thank you :)