module Paperclip::Storage::Database

Public Class Methods

extended(base) click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 4
def self.extended(base)
  base.instance_eval do
    override_default_options base
  end
end

Public Instance Methods

exists?(style = default_style) click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 16
def exists?(style = default_style)
  return !get_attachment(style).nil?
end
flush_deletes() click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 75
def flush_deletes
  @queued_for_delete.each do |style|
    attachment = get_attachment(style)
    attachment.destroy if !attachment.nil?
  end
  @queued_for_delete = []
end
flush_writes() click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 57
def flush_writes
  attachment_definitions = get_attachment_definitions

  @queued_for_write.each do |style, file|
    PaperclipDatabaseStorage::Attachment.new do |a|
      a.attached_type = self.instance.class.name
      a.attached_id = self.instance.id
      a.style = style
      a.content_type = file.content_type
      a.attachment_name = attachment_definitions.keys.first
      a.file_size = file.size
      a.file_data = Base64.encode64(file.read)
      a.base64_encoded = true
    end.save
  end
  @queued_for_write = {}
end
get_attachment(style) click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 24
def get_attachment(style)
  return PaperclipDatabaseStorage::Attachment.find(:first, :conditions => {
    :style => style,
    :attached_type => self.instance.class.name,
    :attached_id => self.instance.id,
    :attachment_name => self.get_attachment_definitions.keys.first
  })
end
get_attachment_definitions() click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 33
def get_attachment_definitions
  attachment_definitions = self.instance.class.attachment_definitions

  if attachment_definitions.select { |k,v| v[:storage] == :database }.count > 1
    raise Exception.new('paperclip-database-storage does not support more than one attachment per model')
  end

  return attachment_definitions
end
path(style = default_style) click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 20
def path(style = default_style)
  return style
end
to_file(style = default_style) click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 44
def to_file style = default_style
  if @queued_for_write[style]
    @queued_for_write[style]
  elsif exists?(style)
    attachment = get_attachment(style)
    tempfile = Tempfile.new attachment.base_name
  tempfile.write attachment.file_data
  tempfile
  else
    nil
  end
end

Private Instance Methods

override_default_options(base) click to toggle source
# File lib/paperclip_database_storage/storage/database.rb, line 10
def override_default_options(base)
  @path = @url
end