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
|
# File 'lib/grip.rb', line 6
def attachment(name)
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
attachment_definitions[name] = {}
after_save :save_attachments
before_destroy :destroy_attached_files
key "#{name}_id".to_sym, ObjectId
key "#{name}_name".to_sym, String
key "#{name}_size".to_sym, Integer
key "#{name}_type".to_sym, String
define_method(name) do
self.class.grid.get(self["#{name}_id"])
end
define_method("#{name}=") do |file|
self["#{name}_id"] = BSON::ObjectID.new
self["#{name}_size"] = File.size(file)
self["#{name}_type"] = Wand.wave(file.path)
self["#{name}_name"] = if file.respond_to?(:original_filename)
file.original_filename
else
File.basename(file.path)
end
self.class.attachment_definitions[name] = file
end
end
|