Controlling content_type in attachment_fu

I’m working on an app where a model can have different kinds of audio
attachments, in this case mp3 and m4a files. I want to distiguish
between them by content type. So I want mp3 files to have a content
type of audio/mpeg and m4a files a content type of audio/mp4, at first
I tried this:

class AudioFile < ActiveRecord::Base
has_attachment :storage => :file_system,
:max_size => 10.megabytes,
:processor => :none

def self.create_mp3!(attributes)
create!(attributes.merge(:content_type => “audio/mpeg”))
end

def self.create_mp3!(attributes)
create!(attributes.merge(:content_type => “audio/mpeg”))
end
end

where the attributes coming from a form included an uploaded_data value.

This DIDN’T work, the content type was ignored, I eventually tracked
this down to attachment_fu setting the content type from content_type
returned by the UploadedTempFile value of uploaded data which was
returning nil.

So right now, I’ve got this:

module MP3File
def content_type
‘audio/mpeg’
end
end

module AACFile
def content_type
‘audio/mp4’
end
end

def self.create_mp3!(attributes)
attributes[:uploaded_data].extend MP3File
create!(attributes.merge(:content_type => ‘audio/mpeg’))
end

def self.create_aac!(attributes)
attributes[:uploaded_data].extend AACFile
create!(attributes.merge(:content_type => ‘audio/mp4’))
end

Which ‘duck punches’ the UploadedTempFile to return the right value.

But this feels to me somehow wrong. Am I missing something in
attachment_fu?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/