X-Git-Url: https://projects.mako.cc/source/selectricity-live/blobdiff_plain/6a935d078f20d2b1b0d60f0a30c41a642d40758a..5e74f498cd2f8d765e309ca52a45a7c2db0a1a6f:/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/db_file_backend.rb diff --git a/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/db_file_backend.rb b/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/db_file_backend.rb new file mode 100644 index 0000000..23881e7 --- /dev/null +++ b/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/db_file_backend.rb @@ -0,0 +1,39 @@ +module Technoweenie # :nodoc: + module AttachmentFu # :nodoc: + module Backends + # Methods for DB backed attachments + module DbFileBackend + def self.included(base) #:nodoc: + 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 + + # Creates a temp file with the current db data. + def create_temp_file + write_to_temp_file current_data + end + + # Gets the current data from the database + def current_data + db_file.data + end + + protected + # Destroys the file. Called in the after_destroy callback + def destroy_file + db_file.destroy if db_file + end + + # Saves the data to the DbFile model + def save_to_storage + if save_attachment? + (db_file || build_db_file).data = temp_data + db_file.save! + self.class.update_all ['db_file_id = ?', self.db_file_id = db_file.id], ['id = ?', id] + end + true + end + end + end + end +end \ No newline at end of file