Module: Grip::HasAttachment::ClassMethods

Defined in:
lib/grip/has_attachment.rb

Instance Method Summary collapse

Instance Method Details

#has_grid_attachment(name, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grip/has_attachment.rb', line 16

def has_grid_attachment name, opts={}
  set_callbacks_once

  write_inheritable_attribute(:uploaded_file_options, {}) if uploaded_file_options.nil?
  uploaded_file_options[name] = opts
  self.send(:include, InstanceMethods)

  define_method(name) do
    attachments.find(:first, :conditions=>{:name => name.to_s})
  end

  define_method("#{name}=") do |new_file|
					return if new_file.nil? || (new_file.is_a?(String) && new_file.blank?)
    raise InvalidFile unless (new_file.is_a?(File) || new_file.is_a?(Tempfile))
    uploaded_files[name] ||= {}
    uploaded_files[name][:file] = new_file
    self['_id'] = Mongo::ObjectID.new if _id.blank?
    new_attachment = attachments.find_or_create_by_name(name.to_s)
    update_attachment_attributes!(new_attachment, new_file, opts)
  end

end

#set_callbacks_onceObject



39
40
41
# File 'lib/grip/has_attachment.rb', line 39

def set_callbacks_once
  after_save :save_attachments unless after_save.collect(&:method).include?(:save_attachments)
end

#uploaded_file_optionsObject



43
44
45
# File 'lib/grip/has_attachment.rb', line 43

def uploaded_file_options
  read_inheritable_attribute(:uploaded_file_options)
end