Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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