vrinek (owner)

Revisions

gist: 135810 Download_button fork
public
Description:
attachment_fu custom error messages
Public Clone URL: git://gist.github.com/135810.git
Embed All Files: show embed
avatar.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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