Skip to content

Instantly share code, notes, and snippets.

@pbraswell
Created January 13, 2011 16:04
Show Gist options
  • Save pbraswell/778095 to your computer and use it in GitHub Desktop.
Save pbraswell/778095 to your computer and use it in GitHub Desktop.
module Helpers
# Conventient way to setup filesystem attachment uploading when in dev/testing
# and use S3 otherwise
module AssetStorage
def self.included(within)
within.class_eval do
extend ClassMethods
end
end
module ClassMethods
def stores_file_as(asset_type, opts = {})
filename_interpolation = opts[:filename_interpolation] || ':style.:extension'
options = use_local_storage? ?
filesystem_default_options(filename_interpolation) :
s3_default_options(filename_interpolation)
options = options.merge(opts)
has_attached_file asset_type, options
end
private
def use_local_storage?
@use_local_storage ||= !(Rails.env.production? || Rails.env.staging?)
end
def s3_default_options(filename_interpolation)
# Akamai URL -> origin URL -> Rails env
# N/A -> origin.staticassets.development.theslap.digitaltoniq.com -> development
# staticassets.theslap.digitaltoniq.com -> origin.staticassets.theslap.digitaltoniq.com -> staging
# staticassets.beta.theslap.com -> origin.staticassets.beta.theslap.com -> beta
# staticassets.theslap.com -> origin.staticassets.theslap.com -> production
{
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/:style/:filename"
}
end
def filesystem_default_options(filename_interpolation)
{
## Add any local file system options you may want here ...
# :url => "/assets/:class/:slug/#{filename_interpolation}",
# :path => ":rails_root/public/assets/:class/:slug/#{filename_interpolation}"
}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment