Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After playing around with it for a while, I came up with a module that does what I want.</p> <p>Inside <code>app/models/shared/attachment_helper.rb</code>:</p> <pre><code>module Shared module AttachmentHelper def self.included(base) base.extend ClassMethods end module ClassMethods def has_attachment(name, options = {}) # generates a string containing the singular model name and the pluralized attachment name. # Examples: "user_avatars" or "asset_uploads" or "message_previews" attachment_owner = self.table_name.singularize attachment_folder = "#{attachment_owner}_#{name.to_s.pluralize}" # we want to create a path for the upload that looks like: # message_previews/00/11/22/001122deadbeef/thumbnail.png attachment_path = "#{attachment_folder}/:uuid_partition/:uuid/:style.:extension" if Rails.env.production? options[:path] ||= attachment_path options[:storage] ||= :s3 options[:url] ||= ':s3_authenticated_url' options[:s3_credentials] ||= File.join(Rails.root, 'config', 's3.yml') options[:s3_permissions] ||= 'private' options[:s3_protocol] ||= 'https' else # For local Dev/Test envs, use the default filesystem, but separate the environments # into different folders, so you can delete test files without breaking dev files. options[:path] ||= ":rails_root/public/system/attachments/#{Rails.env}/#{attachment_path}" options[:url] ||= "/system/attachments/#{Rails.env}/#{attachment_path}" end # pass things off to paperclip. has_attached_file name, options end end end end </code></pre> <p><em>(Note: I'm using some custom paperclip interpolations above, like <code>:uuid_partition</code>, <code>:uuid</code> and <code>:s3_authenticated_url</code>. You'll need to modify things as needed for your particular application)</em></p> <p>Now, for every model that has paperclip attachments, you just have to include this shared module, and call the <code>has_attachment</code> method (instead of paperclip's <code>has_attached_file</code>)</p> <p>An example model file: <code>app/models/user.rb</code>:</p> <pre><code>class User &lt; ActiveRecord::Base include Shared::AttachmentHelper has_attachment :avatar, :styles =&gt; { :thumbnail =&gt; "100x100&gt;" } end </code></pre> <p>With this in place, you'll have files saved to the following locations, depending on your environment:</p> <p><strong>Development:</strong> </p> <p><code>RAILS_ROOT + public/attachments/development/user_avatars/aa/bb/cc/aabbccddeeff/thumbnail.jpg</code></p> <p><strong>Test:</strong></p> <p><code>RAILS_ROOT + public/attachments/test/user_avatars/aa/bb/cc/aabbccddeeff/thumbnail.jpg</code></p> <p><strong>Production:</strong></p> <p><code>https://s3.amazonaws.com/your-bucket-name/user_avatars/aa/bb/cc/aabbccddeeff/thumbnail.jpg</code></p> <p>This does exactly what I'm looking for, hopefully it'll prove useful to someone else too. :)</p> <p>-John</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload