Ensuring the file uploaded by Refile

Refile is an neat and simple file upload library for Ruby applications. It can be easily to integrated into Rails app. Be advised, I am on its git version 6a25a24059.

Recently, I ran into trouble to validate the file exists or uploaded. I went through the AR layer, the model is set at before_save hook. There is no way to check the file is uploaded.

Thanks to its simple abstraction, attacher will present when the file is uploaded.

Then the final resolution is straightforward. For example:

class UserValidator < ActiveModel::Validator
  def validate(record)
    unless record.send(:avatar).present?
      attacher = "avatar_attacher"

      record.errors.add(:avatar, :blank, options) unless record.send(attacher).present?

Only if the file is uploaded, the attcher will be present.