Module: UploadColumn::ActiveRecordExtension::ClassMethods

Defined in:
lib/upload_column/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#image_column(name, options = {}) ⇒ Object



113
114
115
# File 'lib/upload_column/active_record_extension.rb', line 113

def image_column(name, options={})
  upload_column(name, options.reverse_merge(UploadColumn.image_column_configuration))
end

#reflect_on_upload_columnsObject

returns a hash of all UploadColumns defined on the model and their options.



135
136
137
# File 'lib/upload_column/active_record_extension.rb', line 135

def reflect_on_upload_columns
  @upload_columns || {}
end

#upload_column(name, options = {}) ⇒ Object

handle the attr attribute as an “upload-column” field, generating additional methods as explained in the README. You should pass the attribute’s name as a symbol, like this:

upload_column :picture

upload_column can manipulate file with the following options:

versions

Creates different versions of the file, can be an Array or a Hash, in the latter case the values of the Hash will be passed to the manipulator

manipulator

Takes a module that must have a method called process! that takes a single argument. Use this in conjucntion with :versions and :process

process

This instrucion is passed to the manipulators process! method.

you can customize file storage with the following:

store_dir

Determines where the file will be stored permanently, you can pass a String or a Proc that takes the current instance and the attribute name as parameters, see the README for detaills.

tmp_dir

Determines where the file will be stored temporarily before it is stored to its final location, you can pass a String or a Proc that takes the current instance and the attribute name as parameters, see the README for detaills.

old_files

Determines what happens when a file becomes outdated. It can be set to one of :keep, :delete and :replace. If set to :keep UploadColumn will always keep old files, and if set to :delete it will always delete them. If it’s set to :replace, the file will be replaced when a new one is uploaded, but will be kept when the associated object is deleted. Default to :delete.

permissions

Specify the Unix permissions to be used with UploadColumn. Defaults to 0644. Remember that permissions are usually counted in octal and that in Ruby octal numbers start with a zero, so 0644 != 644.

root_dir

The root path where image will be stored, it will be prepended to store_dir and tmp_dir

it also accepts the following, less common options:

web_root

Prepended to all addresses returned by UploadColumn::UploadedFile.url

extensions

A white list of files that can be used together with validates_integrity_of to secure your uploads against malicious files.

fix_file_extensions

Try to fix the file’s extension based on its mime-type, note that this does not give you any security, to make sure that no dangerous files are uploaded, use validates_integrity_of. This defaults to true.

get_content_type_from_file_exec

If this is set to true, UploadColumn::SanitizedFile will use a *nix exec to try to figure out the content type of the uploaded file.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/upload_column/active_record_extension.rb', line 91

def upload_column(name, options = {})
  @upload_columns ||= {}
  @upload_columns[name] = Column.new(name, options)

  define_method( name ) { get_upload_column(name) }
  define_method( "#{name}=" ) { |file| set_upload_column(name, file) }
  
  define_submethod( name, "temp" ) { get_upload_column_temp(name) }      
  define_submethod( name, "temp=" ) { |path| set_upload_column_temp(name, path) }

  define_submethod( name, "public_path" ) { get_upload_column(name).public_path rescue nil }
  define_submethod( name, "path" ) { get_upload_column(name).path rescue nil }

  if options[:versions]
    options[:versions].each do |k, v|
      define_submethod( name, k ) { get_upload_column(name).send(k) rescue nil }
      define_submethod( name, k, "public_path" ) { get_upload_column(name).send(k).public_path rescue nil }
      define_submethod( name, k, "path" ) { get_upload_column(name).send(k).path rescue nil }
    end
  end
end

#validates_integrity_of(*attr_names) ⇒ Object

Validates whether the images extension is in the array passed to :extensions. By default this is the UploadColumn.extensions array

Use this to prevent upload of files which could potentially damage your system, such as executables or script files (.rb, .php, etc…).



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/upload_column/active_record_extension.rb', line 122

def validates_integrity_of(*attr_names)
  configuration = { :message => "is not of a valid file type." }
  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)

  attr_names.each { |name| self.reflect_on_upload_columns[name].options[:validate_integrity] = true }

  validates_each(attr_names, configuration) do |record, attr, value|
    value = record.instance_variable_get('@files')[attr]
    record.errors.add(attr, value.message) if value.is_a?(IntegrityError)
  end
end