Class: Attach::Backends::Database

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

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #url

Constructor Details

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

Instance Method Details

#delete(attachment) ⇒ Object



21
22
23
# File 'lib/attach/backends/database.rb', line 21

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



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

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, data) ⇒ Object



15
16
17
18
19
# File 'lib/attach/backends/database.rb', line 15

def write(attachment, data)
  binary = AttachmentBinary.where(:attachment_id => attachment.id).first_or_initialize
  binary.data = data
  binary.save!
end