class Attach::Backends::Database

Public Instance Methods

delete(attachment) click to toggle source
# File lib/attach/backends/database.rb, line 27
def delete(attachment)
  AttachmentBinary.where(:attachment_id => attachment.id).destroy_all
end
read(attachment) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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