26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/grip.rb', line 26
def has_grid_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}_size".to_sym, Integer
key "#{name}_path".to_sym, String
key "#{name}_name".to_sym, String
key "#{name}_content_type".to_sym, String
define_method(name) do
GridFS::GridStore.read(self.class.database, self["#{name}_path"])
end
define_method("#{name}=") do |file|
self['_id'] = Mongo::ObjectID.new if _id.blank?
self["#{name}_size"] = File.size(file)
self["#{name}_name"] = File.basename(file.path)
self["#{name}_path"] = "#{self.class.to_s.underscore}/#{name}/#{_id}"
self["#{name}_content_type"] = file.content_type rescue MIME::Types.type_for(self["#{name}_name"]).to_s
self.class.attachment_definitions[name] = file
end
end
|