Class: Attach::Backends::Database

Inherits:
Abstract
  • Object
show all
Defined in:
lib/attach/backends/database.rb

Instance Method Summary collapse

Methods inherited from Abstract

#bytesize, #digest, #initialize, #url

Constructor Details

This class inherits a constructor from Attach::Backends::Abstract

Instance Method Details

#delete(attachment) ⇒ Object



26
27
28
# File 'lib/attach/backends/database.rb', line 26

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



30
31
32
33
34
# File 'lib/attach/backends/database.rb', line 30

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
# File 'lib/attach/backends/database.rb', line 15

def write(attachment, binary)
  binary = AttachmentBinary.where(:attachment_id => attachment.id).first_or_initialize
  if binary.respond_to?(:path)
    binary.rewind
    binary.data = binary.read
  else
    binary.data = binary
  end
  binary.save!
end