Class: Attach::Backends::Database
Instance Method Summary
collapse
Methods inherited from Abstract
#bytesize, #digest, #initialize, #url
Instance Method Details
#delete(attachment) ⇒ Object
27
28
29
|
# File 'lib/attach/backends/database.rb', line 27
def delete(attachment)
AttachmentBinary.where(:attachment_id => attachment.id).destroy_all
end
|
#read(attachment) ⇒ Object
7
8
9
10
11
12
13
|
# File 'lib/attach/backends/database.rb', line 7
def read(attachment)
if binary = AttachmentBinary.find_by_attachment_id(attachment.id)
binary.data
else
nil
end
end
|
#read_multi(attachments) ⇒ Object
31
32
33
34
35
|
# File 'lib/attach/backends/database.rb', line 31
def read_multi(attachments)
AttachmentBinary.where(:attachment_id => attachments.map(&:id)).each_with_object({}) do |binary, hash|
hash[binary.attachment_id] = binary.data
end
end
|
#write(attachment, binary) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/attach/backends/database.rb', line 15
def write(attachment, binary)
binary_object = AttachmentBinary.where(:attachment_id => attachment.id).first_or_initialize
if binary.respond_to?(:path)
binary.rewind
binary_object.data = binary.read
else
binary_object.data = binary
end
binary_object.save!
binary_object
end
|