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
|