Rails Paperclip,DRY配置

时间:2011-01-24 18:43:08

标签: ruby-on-rails paperclip dry

为了干掉附件图片的代码,我创建了一个初始化程序来覆盖Paperclip使用的@default_options变量。

这样,我不必一次又一次地指定我想要的网址,路径和存储空间。

我想更进一步,在其中加入验证,但我不能让它发挥作用......

任何想法?

编辑1:我至少要验证存在和大小。

编辑2:我当前代码的一部分

module Paperclip
 class Attachment
   def self.default_options
     if Rails.env != "production"
       @default_options = {
         :url => "/assets/:class/:attachment/:id/:style/:normalized_name",
         :path => ":rails_root/public/assets/:class/:attachment/:id/:style/:normalized_name",
         :default_style => :original,
         :storage       => :filesystem,
         :whiny         => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
         }
      else
       ...
      end
   end
 end

normalized_name是一个外部函数,专长:http://blog.wyeworks.com/2009/7/13/paperclip-file-rename

编辑3:

此博客:http://omgsean.com/2009/02/overriding-paperclip-defaults-for-your-entire-rails-app/使用default_options密钥表示validations哈希值。

所以它有可能,但尚未找到。

1 个答案:

答案 0 :(得分:1)

您将无法将验证移动到default_options哈希值(因为这些验证是在附件类之外(在回形针模块内)执行的。我的想法是,如果您对所有验证都进行了相同的验证模型,您可能需要考虑使用继承来减少代码重复。我建议不要将验证移到初始化器中。