Module: Bhf::ActiveRecord::Upload

Extended by:
ActiveSupport::Concern
Defined in:
lib/bhf/active_record/upload.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#bhf_uploadObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bhf/active_record/upload.rb', line 11

def bhf_upload
  self.class.bhf_upload_settings.each do |settings|
    name_was = send("#{settings[:name]}_was")
    param_name = read_attribute(settings[:name]) || send(settings[:name])
    next if param_name.blank? or param_name.is_a?(String)
    file_string = if param_name && param_name[:delete].to_i != 0
      # File.delete(settings[:path] + name_was.to_s) if File.exist?(settings[:path] + name_was.to_s)
      nil
    else
      file = param_name && param_name[:file]
      if file.is_a? ActionDispatch::Http::UploadedFile
        # File.delete(settings[:path] + name_was.to_s) if File.exist?(settings[:path] + name_was.to_s)

        filename = Time.now.to_i.to_s+'_'+file.original_filename.downcase.gsub(/[^\w\.\-]/, '_')
        path = File.join(settings[:path], filename)
        File.open(path, 'wb') { |f| f.write(file.read) }
        filename
      else
        name_was
      end
    end
    write_attribute settings[:name], file_string
  end
end