简体   繁体   English

回形针-投寄箱。 如何设置条件has_attached_file?

[英]Paperclip - dropbox. How can I setup a conditional has_attached_file?

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. 为此,我想利用回形针保管箱gem进行存储。 It allows paperclip to directly upload to dropbox: https://github.com/janko-m/paperclip-dropbox . 它允许回形针直接上传到保管箱: https : //github.com/janko-m/paperclip-dropbox

Here is the code that sets the authorization information for the paperclip-dropbox gem. 这是设置回形针宝盒gem的授权信息的代码。 NOTE : current_user does not work at the moment. 注意 :current_user目前无法使用。 I'm just putting that there to outline what would need to happen for the current setup to work. 我只是在这里概述要使当前设置正常工作将需要发生的事情。

Image.rb 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. 请注意,保管箱身份验证要求current_user获得该组特定的凭据。

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? 我知道不应该从模型中访问current_user,我想保持这种方式,所以有人可以帮助我找出如何使用当前设置进行操作吗? Or suggest a better alternative? 还是提出更好的选择?

Basically, I need to conditionally change the access_token, access_secret, & user_id on a per user basis. 基本上,我需要根据每个用户有条件地更改access_token,access_secret和user_id。

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). 要动态地更改has_attached_file ,必须在附件模型中有一个user_id列,以便不调用current_user (没有丑陋的hacks就不可能)。 Then you need a belongs_to as well to complete the user association. 然后,您还需要一个belongs_to来完成用户关联。 Let's assume I'm attaching an audio file to a Song model for this example. 假设在此示例中,我将音频文件附加到Song模型中。

The key to getting the dynamically set variables is to initialize the attachment with the after_initialize callback. 获得动态设置的变量的关键是使用after_initialize回调初始化附件。

Song.rb 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. 当然,您可以自由地以不同的方式设置关联,但这是所提出问题的有效代码示例。

I think first thing you should do is to set association Image.belongs_to :user - you could then use simply user.token etc. instead of referencing current_user . 我认为您应该做的第一件事是设置关联Image.belongs_to :user然后可以简单地使用user.token等,而不是引用current_user

Now the hard part. 现在是困难的部分。 You can't simply type: 您不能简单地输入:

access_token: user.token

because self is Image class which simply doesn't respond to user method (it's instance method). 因为selfImage类,它根本不响应user方法(它是实例方法)。 My idea is to modify this gem so it could accept lambdas as arguments with attachment instance (for example) passed to this lambda on call. 我的想法是修改此gem,以便它可以接受lambda作为参数,例如在调用时将附件实例传递给此lambda。 The problem is I don't know if it's hard to modify this gem that way yet. 问题是我还不知道以这种方式修改这个gem是否很困难。

Just found this resource you might gain benefit from: Ruby on Rails - Paperclip and dynamic parameters 刚刚找到了您可能会从中受益的资源: Ruby on Rails-回形针和动态参数

More specifically for you, I thought this might shed some light onto what you're doing: 更具体地说,对于您来说,我认为这可能可以使您了解正在做的事情:

# AssetsController
def create
  @project = Project.find(params[:project_id])
  @asset = @project.assets.build(params[:asset])
  @asset.uploaded_by = current_user

  respond_to do |format|
    # all this is unrelated and can stay the same
  end
end

Notice the "@asset.uploaded_by" is set in the controller? 注意控制器中设置了“ @ asset.uploaded_by”吗? Maybe you could pass similar variables to your model? 也许您可以将类似的变量传递给模型? I don't know how I would do it specifically, but you'd basically be able to set the save options before you try and save the file, giving you the ability to dynamically set the options 我不知道该怎么做,但是您基本上可以在尝试保存文件之前设置保存选项,从而可以动态设置选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM