views:

137

answers:

1

I'm working on a Rails app using attachment_ fu and Amazon S3 storage. Is it possible to make the :s3_ access (a part of the has_attachment options) conditional based on a user input when creating the object. I would like the user to be able if to select if the attachment is authenticated-read or public-read. Is this possible and how would you setup the conditional statement?

My model looks like this:

has_attachment :content_type => :image,
               :storage => :s3,
               :size => 0.kilobytes..6144.kilobytes,
               :processor => 'Rmagick',
               :resize_to => '650x500>',
               :thumbnails => { :thumb => '75x75!' },
               :s3_access => ( [[conditional]] ? 'authenticated-read' : 'public-read' )

Obviously [[conditional]] is what I'm looking to replace, but I don't know how to correctly setup a conditional for the item based off user action in the model. Maybe this is the wrong time of conditional for this type statement. Any suggestions?

A: 

James, I've done this before. I was digging through my old code, trying to find a sample I could show here, but I can't seem to find it. Old client, code is probably gone, etc...

You are on the right track though. Remember, 'has_attachment' is just a method call with a hash for the parameters, so if you treat it as such, you can adjust which hash values are ultimately sent to the method.

Edit - Here is a code sample -

production = ENV['RAILS_ENV'] == 'production'

has_attachment :content_type => :image, 
  :max_size => 1.megabyte, 
  :resize_to => '800x600>', 
  :thumbnails => { :thumb => '146x146>', :small => '75x75' }, 

  # skip s3 for local development and testing
  :storage => (production ? :s3 : :file_system),
  :path_prefix => (production ? 'comment_images' : 'public/comment_images')

In the above example, I wanted to store uploads locally while developing, but switch to S3 when in production. So you are very close to where you need to be...

Phil