PaperClip 没有执行 validates_attachment

PaperClip is not executing validates_attachment

当行:

时,PaperClip 未执行 validates_attachment
after_post_process :extract_text

有效。如果我将其注释掉 validates_attachment 则按需要执行。文档指出验证是在 post_process.

之前完成的

我正在使用 Rails 4.2.3,Ruby 2.0,PaperClip 4.3.0。

class Document < ActiveRecord::Base

  validates :title, presence: true, length: { minimum: 5 }

  has_attached_file :fdoc,
                    :default_url => "/images/:style/missing.png"

  validates_attachment :fdoc,
                       :presence => true,
                       :content_type => { :content_type => "application/pdf" },
                       :file_name => { :matches => [/pdf\Z/] }

  after_post_process :extract_text

  def extract_text

    reader = PDF::Reader.new(fdoc.queued_for_write[:original].path)

    full_text = ""
    reader.pages.each do |page|
      full_text << page.text
    end    

    self.text = full_text
  end

end

更新:

我添加了一个 begin/rescue 块,以防止我的 extract_text 函数在上传的不是 PDF 且正确执行验证时崩溃。

因此文档不正确。验证在 post_process 事件后执行。

根据回形针 README.md 如果 self.valid after_post_process 不会触发?是假的。但是我发现无论如何都会调用 after_post_process 所以我添加了:

after_post_process :my_method

def my_method
  if self.valid?
     # execute code
  end
end