Rails,Paperclip选项。将多个选项作为一个参数传递给回形针

时间:2017-08-23 14:13:26

标签: ruby-on-rails ruby-on-rails-4 paperclip ruby-on-rails-5

我有一个使用Paperclip附带文件的模型。它用多个参数描述:

has_attached_file :attachment,
  styles: {
    regular: Proc.new { |instance| instance.detect_regular_style },
    thumb: '144x144>'
  },
  storage: :s3,
  s3_protocol: :https
  ...

我可以以某种方式将所有参数合并到一个变量(lambda或类似的东西),以便我可以将它们重用到其他附件吗? 我想象的代码看起来像这样(这个代码不会读取传递的选项):

has_attached_file :attachment, {options: -> {attached_file_options} }
has_attached_file :picture, {options: -> {attached_file_options} }

def attached_file_options
{
  styles: {
    regular: Proc.new { |instance| instance.detect_regular_style },
    thumb: '144x144>'
  },
  storage: :s3,
  s3_protocol: :https
}

1 个答案:

答案 0 :(得分:0)

答案很简单:只是一个带参数哈希的变量。请记住在访问变量之前将数据分配给变量:

ATTACHED_FILE_OPTIONS =
  {
    styles: {
      regular: Proc.new { |instance| instance.detect_regular_style },
      thumb: '144x144>'
    },
    storage: :s3,
    s3_protocol: :https
    ...
  }

has_attached_file :attachment, ATTACHED_FILE_OPTIONS
has_attached_file :picture, ATTACHED_FILE_OPTIONS