views:

332

answers:

2

Is there anyway to have the validates_attachment_size except a dynamic file size limit? Here's an example:

class Document < ActiveRecord::Base
   belongs_to :folder
   has_attached_file :document
   validates_attachment_size :document, :less_than => get_current_file_size_limit

   private

   def get_current_file_size_limit
     10.megabytes # This will dynamically change
   end
end

I've tried this but I keep getting an error saying "unknown method". Lambdas and Procs don't work either. Has anyone ever tried this? Thanks

+1  A: 

Long shot...

validates_attachment_size :document, :less_than => :get_current_file_size_limit

Usually when passing a function you have to pass the symbol and not the actual function.

Tony Fontenot
Yeah, I thought that might work but nothing. Thanks though!
CalebHC
+4  A: 

Paperclip doesn't allow to pass function as size limit parameter. So you probably need to write custom validation:

  validate :validate_image_size

  def validate_image_size
    if document.file? && document.size > get_current_file_size_limit
      errors.add_to_base(" ... Your error message")
    end
  end
Voyta
This worked great. Thanks! I was hoping there would be a little bit nicer way but I guess not for now. Maybe I'll submit a patch to Paperclip. :)
CalebHC