Module: Technoweenie::AttachmentFu::Backends::FileSystemBackend

Defined in:
lib/attachment_fu/backends/file_system_backend.rb

Overview

Methods for file system backed attachments

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



7
8
9
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 7

def self.included(base) #:nodoc:
  base.before_update :rename_file
end

Instance Method Details

#attachment_path_idObject

The attachment ID used in the full path of a file



30
31
32
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 30

def attachment_path_id
  ((respond_to?(:parent_id) && parent_id) || id).to_i
end

#base_pathObject

Used as the base path that #public_filename strips off full_filename to create the public path



25
26
27
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 25

def base_path
  @base_path ||= File.join(RAILS_ROOT, 'public')
end

#create_temp_fileObject

Creates a temp file from the currently saved file.



56
57
58
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 56

def create_temp_file
  copy_to_temp_file full_filename
end

#filename=(value) ⇒ Object



50
51
52
53
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 50

def filename=(value)
  @old_filename = full_filename unless filename.nil? || @old_filename
  write_attribute :filename, sanitize_filename(value)
end

#full_filename(thumbnail = nil) ⇒ Object

Gets the full path to the filename in this format:

# This assumes a model name like MyModel
# public/#{table_name} is the default filesystem path 
RAILS_ROOT/public/my_models/5/blah.jpg

Overwrite this method in your model to customize the filename. The optional thumbnail argument will output the thumbnail’s filename.



19
20
21
22
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 19

def full_filename(thumbnail = nil)
  file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
  File.join(RAILS_ROOT, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail)))
end

#partitioned_path(*args) ⇒ Object

by default paritions files into directories e.g. 0000/0001/image.jpg to turn this off set :partition => false



36
37
38
39
40
41
42
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 36

def partitioned_path(*args)
  if respond_to?(:attachment_options) && attachment_options[:partition] == false 
    args
  else 
    ("%08d" % attachment_path_id).scan(/..../) + args
  end
end

#public_filename(thumbnail = nil) ⇒ Object

Gets the public path to the file The optional thumbnail argument will output the thumbnail’s filename.



46
47
48
# File 'lib/attachment_fu/backends/file_system_backend.rb', line 46

def public_filename(thumbnail = nil)
  full_filename(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), ''
end