Adding default configuration options to Paperclip
July 23rd, 2011
Using paperclip, you’ll often put something like this in your models:
has_attached_file :article_text,
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AS3_BUCKET'],
:access_key_id => ENV['AS3_ACCESS_KEY_ID'],
:secret_access_key => ENV['AS3_SECRET_ACCESS_KEY']
}
:s3_permissions => :private,
:processors => [:manuscript_converter],
:path => "manuscript/:id/:uploaded_manuscript_filename"
Many of these settings will be repeated in every paperclip’d model. You can easily remove that duplication by adding an initializer. In config/initializers/paperclip_defaults.rb:
Paperclip::Attachment.default_options.merge!(
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AS3_BUCKET'],
:access_key_id => ENV['AS3_ACCESS_KEY_ID'],
:secret_access_key => ENV['AS3_SECRET_ACCESS_KEY']
}
)
And now you can remove those options from all your models – separating the parts that change from the parts that stay the same.
I had trouble finding this online and had to dig through the source code to figure it out. So maybe it will help someone else save a little time.
Tags: paperclip, Ruby-on-Rails