Module: Paperclip::Storage::Database

Defined in:
lib/paperclip_database_storage/storage/database.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
# File 'lib/paperclip_database_storage/storage/database.rb', line 4

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

Instance Method Details

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/paperclip_database_storage/storage/database.rb', line 16

def exists?(style = default_style)
  return !get_attachment(style).nil?
end

#flush_deletesObject



75
76
77
78
79
80
81
# 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_writesObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



24
25
26
27
28
29
30
31
# 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_definitionsObject



33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



20
21
22
# File 'lib/paperclip_database_storage/storage/database.rb', line 20

def path(style = default_style)
  return style
end

#to_file(style = default_style) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# 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