Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Created October 21, 2010 20:57
Show Gist options
  • Save joshnuss/639341 to your computer and use it in GitHub Desktop.
Save joshnuss/639341 to your computer and use it in GitHub Desktop.
module SecureAttachment
private
def has_secure_attached_file(name, options = {})
has_attached_file name,
options.reverse_merge(
:path => "#{name}/:id/:style/:basename.:extension",
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:s3_protocol => 'https',
:s3_permissions => 'private',
:bucket => "#{BUCKET_APP_NAME}-#{Rails.env}-private")
define_method "#{name}_time_limited_url" do |*args|
attachment = send(name)
options = args.last || {}
style = options[:style]
expire_in = options[:expire_in] || 10.minutes
attachment.s3.interface.get_link(attachment.s3_bucket.to_s,
attachment.path(style), expire_in)
end
end
end
### in your model:
class SomeModel < ActiveRecord::Base
extend SecureAttachment
has_secure_attached_file :attachment
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment