Acts_as_attachment - on server copy?

Hi all,

I am using aaa for uploads (Upload model) of assets to a dir below
public. I also need to save files, into the Upload model, which come
as attachments by email. Is there a way I can ask aaa to save the
attachment as if it was uploaded? TIA,

bakki

On 8/23/07, Bakki K. [email protected] wrote:

Hi all,

I am using aaa for uploads (Upload model) of assets to a dir below
public. I also need to save files, into the Upload model, which come
as attachments by email. Is there a way I can ask aaa to save the
attachment as if it was uploaded? TIA,

bakki

bakki,

you should be loop through the email’s attachments and create new
Uploads like this:

for attachment in email.attachments
Upload.create! attachment
end

note: this is untested code, but should point you in the right

direction.

Have a look at this article regarding Action Mailer’s attachments:

http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer

Thank you for your reply Sam, I don’t have my action mailer receive
part working yet and I will try this when its done. However the
Upload model has

acts_as_attachment :storage => :file_system, :file_system_path =>
‘uploads’
validates_as_attachment

I was wondering if acts_as_attachment can take an attachment passed to
the model vs. the uploaded file which it normally gets from cgi param.
I thought the validates_as_attachment call back might not work if the
attachment was passed to the model as you suggest. I’ll have to look
the aaa code over to see if this doesn’t work.

-bakki

here’s a post on how to upload files stored locally:

http://www.railsweenie.com/forums/2/topics/1013

Hello,

I am not sure if I understand correctly, but you can pass a regular
file (on disk) to the model conviently using TestUploadedFile (see
http://api.rubyonrails.org/classes/ActionController/TestUploadedFile.html#M000278)
like this:

Photo.create( :uploaded_data =>
ActionController::TestUploadedFile.new(file, ‘image/jpeg’) )

See http://pastie.caboo.se/90597 for more complete code I use for
importing whole directory of photos into application.

(You can even use this in script/console or script/runner, thus
automating the importing process.)

Cheers,

Karel

Hello,

yes, that’s it precisely. I guess acts_as_attachment has
TestUploadedFile somehow encapsulated, but I couldn’t get it to
work, don’t recall precisely why. I recently used TestUploadedFile
to import loads of legacy data into ported application with great
results.

Glad to help, cheers,

Karel

Hi Karel,

This actually should work for me. I was looking at the code in
acts_as_attachment and it has the following code which handles
uploaded files.

  # This method handles the uploaded file object.  If you set the

field name to uploaded_data, you don’t need
# any special code in your controller.
#
# <% form_for :attachment, :html => { :multipart => true } do
|f| -%>
#

<%= f.file_field :uploaded_data %>


#

<%= submit_tag :Save %>
# <% end -%>
#
# @attachment = Attachment.create! params[:attachment]
def uploaded_data=(file_data)
return nil if file_data.nil? || file_data.size == 0
self.content_type = file_data.content_type.strip
self.filename = file_data.original_filename.strip
self.attachment_data = file_data.read
end

So this should fire when, as you suggest

Photo.create( :uploaded_data =>
ActionController::TestUploadedFile.new(file, ‘image/jpeg’) )

:uploaded_data gets assigned to and the rest of aaa validations etc
should work.

Thank you for your help.

-bakki