导轨、回形针选项.将多个选项作为一个参数传递给回形针



我有一个使用回形针的附加文件的模型。它用多个参数描述:

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
}

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

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