Created
October 25, 2009 10:46
-
-
Save thirdreplicator/217997 to your computer and use it in GitHub Desktop.
how to configure the Paperclip plugin to handle general file uploading and image thumbnail generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Clip < ActiveRecord::Base | |
belongs_to :clippable, :polymorphic => true | |
has_attached_file :document, | |
:styles => { :thumbnail => "50x50#", | |
:medium => "200x200", | |
:large => "450x450>", | |
:jumbo => "1024x1024>" | |
}, | |
:processors => [:general], | |
:url => "/assets/clips/:id/:style/:basename.:extension", | |
:path => ":rails_root/public/assets/clips/:id/:style/:basename.:extension" | |
validates_attachment_size :document, :less_than => 5.megabytes | |
def parent | |
self.clippable_type.constantize.find(self.clippable_id) | |
end | |
end | |
# RAILS_ROOT/lib/paperclip_processors/general_processor.rb | |
module Paperclip | |
class General < Processor | |
def initialize file, options = {}, attachment = nil | |
super | |
@format = options[:format] | |
@current_format = File.extname(@file.path) | |
@basename = File.basename(@file.path, @current_format) | |
end | |
def make | |
if @attachment.instance.document_content_type.to_s =~ /image/i | |
Thumbnail.make( @file, @options, @attachment ) | |
else | |
Tempfile.new([@basename, @format].compact.join(".")) | |
end | |
end | |
end # class | |
end # module | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment