2012-08-31 38 views
0

有沒有一種方法可以在回形針中定義樣式別名(相同的轉換,相同的文件路徑)?回形針:樣式別名

# in the model 
has_attached_file :image, { 
    :styles => { 
    :thumb => "90x90>", 
    :small => :thumb 
    } 
    [...] 
} 

# in the application 
model.image.url(:thumb) 
=> 'path/to/image/thumb.jpg' 

model.image.url(:small) 
=> 'path/to/image/thumb.jpg' 

我目前正在重構一個應用程序,其中有很多重複樣式。我希望將其定義一次,而不要打破界面。

+2

我想通過在主持人 – apneadiving

+0

我會loik的覆蓋方法做到這一點稍後更好的解決方案:) – apneadiving

+0

相同。我想知道我是否錯過了回形針文檔中的配置字段,但似乎這只是不支持的功能。主持人似乎是最合乎邏輯的解決方案:) – MrRuru

回答

3

這裏有一個補丁,在初始化添加:

module Paperclip 
    Options.class_eval do 
    attr_accessor :aliases 

    def initialize_with_alias(attachment, hash) 
     @aliases = hash[:aliases] || {} 
     initialize_without_alias(attachment, hash) 
    end 

    alias_method_chain :initialize, :alias 
    end 

    Attachment.class_eval do 
    def url_with_patch(style_name = default_style, use_timestamp = @options.use_timestamp) 
     style_name = @options.aliases[style_name.to_sym] if @options.aliases[style_name.to_sym] 
     url_without_patch(style_name, use_timestamp) 
    end 

    alias_method_chain :url, :patch 
    end 
end 

使用這種方式:

has_attached_file :image, { 
    :styles => { 
    :thumb => "90x90>" 
    } 
    :aliases => { :small => :thumb } 
} 
+0

工作像一個魅力! – MrRuru