Module: Polypaperclip::ClassMethods

Defined in:
lib/polypaperclip.rb

Instance Method Summary collapse

Instance Method Details

#has_attachment(name, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/polypaperclip.rb', line 15

def has_attachment(name, options = {})
  initialize_polypaperclip
  
  polypaperclip_definitions[name] = {:validations => []}.merge(options)
  
  has_one "#{name}_attachment",
    :as => :attachable,
    :dependent => :destroy,
    :class_name => "Polypaperclip::PersistedAttachment",
    :conditions => "attachment_type = '#{name}'"
  
  define_method name do |*args|
    a = paperclip_attachment_for(name)
    (args.length > 0) ? a.to_s(args.first) : a
  end

  define_method "#{name}=" do |file|
    paperclip_attachment_for(name).assign(file)
  end

  define_method "#{name}?" do
    paperclip_attachment_for(name).file?
  end
  
  validates_each(name) do |record, attr, value|
    attachment = record.paperclip_attachment_for(name)
    attachment.send(:flush_errors)
  end
end

#polypaperclip_definitionsObject



11
12
13
# File 'lib/polypaperclip.rb', line 11

def polypaperclip_definitions
  read_inheritable_attribute(:polypaperclip_definitions)
end