Skip to content

Instantly share code, notes, and snippets.

@mariochavez
Created May 13, 2010 22:21
Show Gist options
  • Save mariochavez/400554 to your computer and use it in GitHub Desktop.
Save mariochavez/400554 to your computer and use it in GitHub Desktop.
class Task < ActiveRecord::Base
has_many :assets, :as => :attachable, :dependent => :destroy
validate :validate_attachments
Max_Attachments = 5
Max_Attachment_Size = 1.megabyte
def validate_attachments
errors.add_to_base("You should attach at least one file") if assets.nil?
return if assets.nil?
errors.add_to_base("Too many attachments - maximum is #{Max_Attachments}") if assets.length > Max_Attachments
assets.each {|a| errors.add_to_base("#{a.name} is over #{Max_Attachment_Size/1.megabyte}MB") if a.file_size > Max_Attachment_Size}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment