I am creating an application that lets users store data in their personal dropbox in a protected folder that the application controls. So each user needs to store and access files in their own personal dropbox account.
To do this I'd like to leverage the paperclip-dropbox gem for storage. It allows paperclip to directly upload to dropbox: https://github.com/janko-m/paperclip-dropbox.
Here is the code that sets the authorization information for the paperclip-dropbox gem. NOTE: current_user does not work at the moment. I'm just putting that there to outline what would need to happen for the current setup to work.
Image.rb
has_attached_file :avatar,
:storage => :dropbox,
:dropbox_credentials => {app_key: DROPBOX_KEY,
app_secret: DROPBOX_SECRET,
access_token: current_user.token,
access_secret: current_user.secret,
user_id: current_user.uid,
access_type: "app_folder"}
Notice the dropbox authentication requires the current_user to get that particular set of credentials.
I know that the current_user is not supposed to be accessed from the model and I'd like to keep it that way so could anyone help me figure out how to do that with this current setup? Or suggest a better alternative?
Basically, I need to conditionally change the access_token, access_secret, & user_id on a per user basis.
Thanks!
I'm going to answer my own question because the other answers were too vague to accept - although they were on the right path. I think the community would prefer an answer with more code to back it up.
So here goes. To change the has_attached_file
on a dynamic basis, you have to have a user_id
column in the attachment model so that you're not calling current_user
(which isn't possible without ugly hacks). Then you need a belongs_to
as well to complete the user association. Let's assume I'm attaching an audio file to a Song
model for this example.
The key to getting the dynamically set variables is to initialize the attachment with the after_initialize
callback.
Song.rb
belongs_to :user
has_attached_file :audio
after_initialize :init_attachment
def init_attachment
self.class.has_attached_file :audio,
:storage => :dropbox,
:dropbox_credentials => {app_key: DROPBOX_KEY,
app_secret: DROPBOX_SECRET,
access_token: self.user.token,
access_token_secret: self.user.secret,
user_id: self.user.id
access_type: "app_folder"},
:dropbox_options => {}
end
You are, of course, free to setup your association differently, but this is a working code example for the question posed.