Module: Paperclip::Storage::Database
- Defined in:
- lib/paperclip_database_storage/storage/database.rb
Class Method Summary collapse
Instance Method Summary collapse
- #exists?(style = default_style) ⇒ Boolean
- #flush_deletes ⇒ Object
- #flush_writes ⇒ Object
- #get_attachment(style) ⇒ Object
- #get_attachment_definitions ⇒ Object
- #path(style = default_style) ⇒ Object
- #to_file(style = default_style) ⇒ Object
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 base end end |
Instance Method Details
#exists?(style = default_style) ⇒ Boolean
16 17 18 |
# File 'lib/paperclip_database_storage/storage/database.rb', line 16 def exists?(style = default_style) return !(style).nil? end |
#flush_deletes ⇒ Object
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| = (style) .destroy if !.nil? end @queued_for_delete = [] end |
#flush_writes ⇒ Object
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 = @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. = .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 (style) return PaperclipDatabaseStorage::Attachment.find(:first, :conditions => { :style => style, :attached_type => self.instance.class.name, :attached_id => self.instance.id, :attachment_name => self..keys.first }) end |
#get_attachment_definitions ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/paperclip_database_storage/storage/database.rb', line 33 def = self.instance.class. if .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 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) = (style) tempfile = Tempfile.new .base_name tempfile.write .file_data tempfile else nil end end |