2017-08-23 68 views
0

我有一個使用回形針的附加文件模型。它與多個參數進行描述:導軌,回形針選項。將多個選項作爲一個參數傳遞給回形針

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

我可以以某種方式合併所有這些參數,一個變量(拉姆達或類似的東西),這樣我可以重用他們到其他附件? 我想可能是這樣的(這一個不讀通過的選項)代碼:

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 
} 

回答

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