File column is very useful plugin to upload multi sizes of images. Currently in file column plugin we are not able to provide file name.
I found number of forums which are facing this issue but not able to find the answer.
I found the solution which required to customize the file column plugin.
Add option filename in file column method.
1 2 3 4 | T= Time.nowfile_column :uploaded_data, :magick => {:versions => { "thumb" => "150x150", "medium" => "640x480>" }},:filename => "#{T.strftime("%Y%m%d%H%M")}" |
Initialized filename option in FileColumn.rb file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def self.init_options(defaults, model, attr)options = defaults.dupoptions[:store_dir] ||= File.join(options[:root_path], model, attr)options[:filename]||=nilunless options[:store_dir].is_a?(Symbol)options[:tmp_base_dir] ||= File.join(options[:store_dir], "tmp")endoptions[:base_url] ||= options[:web_root] + File.join(model, attr)[:store_dir, :tmp_base_dir].each do |dir_sym|if options[dir_sym].is_a?(String) and !File.exists?(options[dir_sym])FileUtils.mkpath(options[dir_sym])endendoptionsend |
Assign filename to temp image path. Update code @class TempUploadedFile store_upload method line number 219
1 | @filename = options[:filename] || FileColumn::sanitize_filename(file.original_filename) |
Now test your code and get the uploaded image new file name.
Your code works very well but Rails cache the filename because of "config.cache_classes = true" . So the filename is the same for my images. How to disable cache for the filename ?
ReplyDelete