Class: Technoweenie::AttachmentFu::Backends::DbFileBackend

Inherits:
BackendDelegator
  • Object
show all
Defined in:
lib/technoweenie/attachment_fu/backends/db_file_backend.rb

Overview

Methods for DB backed attachments

Instance Attribute Summary

Attributes inherited from BackendDelegator

#attachment_options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BackendDelegator

#__getobj__, #initialize

Constructor Details

This class inherits a constructor from Technoweenie::AttachmentFu::Backends::BackendDelegator

Class Method Details

.included_in_base(base) ⇒ Object



6
7
8
9
# File 'lib/technoweenie/attachment_fu/backends/db_file_backend.rb', line 6

def self.included_in_base(base)
  Object.const_set(:DbFile, Class.new(ActiveRecord::Base)) unless Object.const_defined?(:DbFile)
  base.belongs_to  :db_file, :class_name => '::DbFile', :foreign_key => 'db_file_id'
end

Instance Method Details

#current_dataObject

Gets the current data from the database



14
15
16
# File 'lib/technoweenie/attachment_fu/backends/db_file_backend.rb', line 14

def current_data
  db_file && db_file.data
end

#destroy_fileObject

Destroys the file. Called in the after_destroy callback



19
20
21
# File 'lib/technoweenie/attachment_fu/backends/db_file_backend.rb', line 19

def destroy_file
  db_file.destroy if db_file
end

#rename_fileObject



11
# File 'lib/technoweenie/attachment_fu/backends/db_file_backend.rb', line 11

def rename_file ; end

#save_to_storageObject

Saves the data to the DbFile model



24
25
26
27
28
29
30
31
32
# File 'lib/technoweenie/attachment_fu/backends/db_file_backend.rb', line 24

def save_to_storage
  if save_attachment?
    (db_file || build_db_file).data = temp_data
    db_file.save!
    @obj.db_file_id = db_file.id
    @obj.class.where(id: @obj.id).update_all db_file_id: db_file.id
  end
  true
end