Skip to content

Instantly share code, notes, and snippets.

@rodrigotassinari
Created August 4, 2009 20:25
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 rodrigotassinari/161521 to your computer and use it in GitHub Desktop.
Save rodrigotassinari/161521 to your computer and use it in GitHub Desktop.
class Image < ActiveRecord::Base
COMMON_ATTACHMENT_OPTIONS = {
:content_type => :image,
:max_size => 1.megabyte,
:thumbnails => {
:thumbnail => "75x75!",
:small => "100x",
:medium => "240x",
:large => "500x",
}
}
has_attachment({
:storage => :s3,
:s3_access => :private
}.merge(COMMON_ATTACHMENT_OPTIONS)
)
validates_as_attachment
def self.new(args={})
raise "use TempImage.new or TempImage.build instead" if self == Image
super(args)
end
def public_filename(thumbnail = nil, expiration = 1.hour)
authenticated_s3_url(thumbnail, :expires_in => expiration)
end
def proxy_public_filename(thumbnail = nil, expiration = 1.hour)
unless self.thumbnail? # self é img original
if thumbnail # quer algum thumb, outro registro
img = TempImage.first(
:conditions => {:parent_id => self.id, :thumbnail => thumbnail.to_s}
) # img é um thumbnail
img.temporary? ?
img.public_filename(nil) :
self.public_filename(thumbnail, expiration)
else # quer original
self.temporary? ?
TempImage.find(self.id).public_filename(thumbnail) :
self.public_filename(thumbnail, expiration)
end
else # self é thumbnail
self.temporary? ?
TempImage.find(self.id).public_filename(nil) :
self.public_filename(nil, expiration)
end
end
def proxy
temporary? ? TempImage.find(id) : self
end
def all_temporary?
if thumbnail?
([parent.temporary?] + parent.thumbnails.map(&:temporary?)).uniq == [true]
else
([temporary?] + thumbnails.map(&:temporary?)).uniq == [true]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment