Skip to content

Instantly share code, notes, and snippets.

@vrinek
Created June 25, 2009 12:02
Show Gist options
  • Save vrinek/135810 to your computer and use it in GitHub Desktop.
Save vrinek/135810 to your computer and use it in GitHub Desktop.
attachment_fu custom error messages
class Avatar < ActiveRecord::Base
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
:resize_to => 'c128x128',
:thumbnails => {:tiny => 'c24x24', :small => "c48x48"}
validates_as_attachment
after_validation :greeklize_attachment_errors
def greeklize_attachment_errors
add_attachment_errors({
:content_type => "Το αρχείο δεν είναι εικόνα",
:size => "Το αρχείο είναι πολύ μεγάλο (μέγιστο 500KB)"
})
end
def add_attachment_errors(e)
unless errors.empty?
previous = self.errors.clone
self.errors.clear
e.stringify_keys!
e.keys.each{|k|
self.errors.add k.to_s, e[k].to_s if e[k] and previous.on(k)
}
previous.each{|a,m|
self.errors.add a, m unless e.keys.include?(a)
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment