How can I set paperclip's storage mechanism based on the current Rails environment?

Posted by John Reilly on Stack Overflow See other posts from Stack Overflow or by John Reilly
Published on 2010-04-01T17:18:08Z Indexed on 2010/04/01 17:23 UTC
Read the original article Hit count: 153

I have a rails application that has multiple models with paperclip attachments that are all uploaded to S3. This app also has a large test suite that is run quite often. The downside with this is that a ton of files are uploaded to our S3 account on every test run, making the test suite run slowly. It also slows down development a bit, and requires you to have an internet connection in order to work on the code.

Is there a reasonable way to set the paperclip storage mechanism based on the Rails environment? Ideally, our test and development environments would use the local filesystem storage, and the production environment would use S3 storage.

I'd also like to extract this logic into a shared module of some kind, since we have several models that will need this behavior. I'd like to avoid a solution like this inside of every model:

### We don't want to do this in our models...
if Rails.env.production?
  has_attached_file :image, :styles => {...},
                    :storage => :s3,
                    # ...etc...
else
  has_attached_file :image, :styles => {...},
                    :storage => :filesystem,
                    # ...etc...
end

Any advice or suggestions would be greatly appreciated! :-)

© Stack Overflow or respective owner

Related posts about paperclip

Related posts about ruby-on-rails