Module: AttachmentSaver::DataStores::InColumn

Defined in:
lib/datastores/in_column.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
# File 'lib/datastores/in_column.rb', line 8

def self.included(base)
  base.attachment_options[:column_name] ||= 'data'
  base.attachment_options[:temp_directory] ||= Dir.tmpdir
end

Instance Method Details

#delete_attachmentObject

delete_attachment is used when the record is deleted, so we don’t need to do anything



24
# File 'lib/datastores/in_column.rb', line 24

def delete_attachment; end

#in_storage?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/datastores/in_column.rb', line 26

def in_storage?
  !send(self.class.attachment_options[:column_name]).nil?
end

#reprocess!Object

there is no public_path, since you need to make a controller to pull the blob from the database



32
33
34
35
36
# File 'lib/datastores/in_column.rb', line 32

def reprocess!
  raise "this attachment already has a file open to process" unless uploaded_file.nil?
  save_temporary_and_process_attachment
  save!
end

#save_attachmentObject



13
14
15
16
17
18
19
20
21
# File 'lib/datastores/in_column.rb', line 13

def save_attachment
  return unless @save_upload # this method is called every time the model is saved, not just when a new file has been uploaded

  send("#{self.class.attachment_options[:column_name]}=", uploaded_data)

  save_temporary_and_process_attachment if process_attachment?

  @save_upload = nil
end

#save_temporary_and_process_attachmentObject



38
39
40
41
42
43
44
45
46
# File 'lib/datastores/in_column.rb', line 38

def save_temporary_and_process_attachment
  FileUtils.mkdir_p(self.class.attachment_options[:temp_directory])
  Tempfile.open("asctemp", self.class.attachment_options[:temp_directory]) do |temp|
    temp.binmode
    temp.write(send(self.class.attachment_options[:column_name]))
    temp.flush
    process_attachment_with_wrapping(temp.path)
  end
end

#tidy_attachmentObject



23
# File 'lib/datastores/in_column.rb', line 23

def   tidy_attachment; end