Skip to content

Instantly share code, notes, and snippets.

@adamaig
Created February 9, 2011 02:22
Show Gist options
  • Select an option

  • Save adamaig/817768 to your computer and use it in GitHub Desktop.

Select an option

Save adamaig/817768 to your computer and use it in GitHub Desktop.
Paperclip.interpolates :attachable_type do |attachment, style|
attachment.instance.attachable_type
end
Paperclip.interpolates :attachable_id do |attachment, style|
attachment.instance.attachable_id
end
class Upload < PlayableStory
belongs_to :attachable, :polymorphic => true
# maybe will set this at the application level. This is also used by the uploadify plugin
MAX_FILE_SIZE = 5.megabytes
# paperclip setup
has_attached_file :attached_upload,
:styles => lambda {|x| x.instance.determine_styles x },
:url => "/uploads/:attachable_type/:attachable_id/:class/:id/:style.:extension",
:default_url => "/images/cancel.png",
:path => ":rails_root/public/uploads/:attachable_type/:attachable_id/:class/:id/:style.:extension",
:whiny => false
validates_attachment_size :attached_upload, :in => 1..MAX_FILE_SIZE
validates_attachment_presence :attached_upload
def upload_file=(data)
data.content_type = MIME::Types.type_for(data.original_filename).first.to_s
self.attached_upload = data
end
delegate :url, :path, :original_filename, :content_type, :default_style, :to => :attached_upload
def determine_styles(attachment)
if is_image?(attachment.content_type)
{ :inventory => "64x64>", :state => "186x206>" }
else
{}
end
end
# determines if the file-type is an image file
def is_image?(content_type)
content_type =~ /image\/(png|jpeg|jpg|gif|pjpeg)/i ? true : false
end
def verify_image
raise "Your upload must be an image -- png, jpg or gif files are the only acceptable formats" if !is_image?
end
# determines if the file-type is an audio file
def is_audio?(content_type)
content_type =~ /audio/i ? true : false
end
# determines if the file-type is a video file
def is_video?(content_type)
content_type =~ /video/i ? true : false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment