Skip to content

Instantly share code, notes, and snippets.

@tibra
Created February 8, 2011 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tibra/815765 to your computer and use it in GitHub Desktop.
Save tibra/815765 to your computer and use it in GitHub Desktop.
class Upload < ActiveRecord::Base
belongs_to :uploadable, :polymorphic => true
attr_accessible :uploadable_id, :uploadable_type, :attachment, :shape, :description
# Make :parent_type available for Paperclip :path & :url options.
# Path on S3 should contain the parent class (e.g. "member") for
# better readability.
Paperclip.interpolates :parent_type do |attachment, style|
attachment.instance.uploadable_type.downcase
end
# Paperclip settings, basic settings are in Rails.root/config/initializers/paperclip.rb
has_attached_file :attachment,
:path => ":class/:parent_type/:id/:style.:extension",
:styles => lambda { |a|
# Don't apply set_shape (= do not set style) when content_type == picture
if ['image/jpeg','image/png','image/gif'].include?(a.instance_read(:content_type))
a.instance.assign_shape
else
{}
end
}
# Defining image sizes (shapes) depending on @shape parameter
# Adding a new @shape: add a new 'when "name"' section, then pass
# params with upload helper.
# Also, don't forget to add a new img.class_shape section in shared.sass
# for correct image rendering.
def assign_shape
case shape
when "gallery"
{ :preview => "700x467>", :mid => "350x234>", :small => "230x153>" , :tiny => "100x67>" }
when "photo"
{ :preview => "270x346>", :small => "100x128>" , :tiny => "78x100>" }
when "logo"
{ :preview => "250x250>", :small => "150x150>" , :tiny => "70x70>" }
when "sponsor"
{ :preview => "250x250>", :super => "728x90>" , :halfsize => "234x60!" }
when "generic_file"
{}
else
{ :standard => "500x500>", :small => "250x250>", :tiny => "125x125>" }
end
end
scope :graphics, where("attachment_content_type IN ('image/jpeg','image/png','image/gif')")
scope :documents, all - graphics
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment