Module: Paperclip::Storage::Database

Defined in:
lib/paperclipdb/storage.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



5
6
7
8
9
# File 'lib/paperclipdb/storage.rb', line 5

def self.extended(base)
  base.instance_eval do
    override_default_options base
  end
end

Instance Method Details

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/paperclipdb/storage.rb', line 17

def exists?(style = default_style)
  return getAttachment(path(style)).nil?
end

#flush_deletesObject



51
52
53
54
55
56
57
58
59
# File 'lib/paperclipdb/storage.rb', line 51

def flush_deletes
  @queued_for_delete.each do |path|
    attachment = getAttachment(path)
    if (!attachment.nil?)
      attachment.destroy 
    end
  end
  @queued_for_delete = []
end

#flush_writesObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/paperclipdb/storage.rb', line 38

def flush_writes
  @queued_for_write.each do |style, file|
    attachment = Paperclipdb::Attachment.new
    attachment.base_name = File.basename(path(style))
    attachment.dir_name = File.dirname(path(style))
    attachment.content_type = self.instance_variable_get("@_#{self.name.to_s}_content_type")
    attachment.file_size = file.size
    attachment.file_data = file.read
    attachment.save
  end
  @queued_for_write = {}
end

#getAttachment(file_path) ⇒ Object



21
22
23
# File 'lib/paperclipdb/storage.rb', line 21

def getAttachment(file_path) 
  return Paperclipdb::Attachment.find(:first, :conditions => [ "base_name = ? AND dir_name = ?", File.basename(file_path), File.dirname(file_path) ])
end

#to_file(style = default_style) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/paperclipdb/storage.rb', line 25

def to_file style = default_style
  if @queued_for_write[style]
    @queued_for_write[style]
  elsif exists?(style)
    attachment = getAttachment(path(style))
    tempfile = Tempfile.new attachment.base_name
    tempfile.write attachment.file_data
    tempfile
  else
    nil
  end
end