Created
December 3, 2009 23:32
-
-
Save MarkoZabcic/248671 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Paperclip convert id => id_partition | |
# | |
require 'ftools' #FileUtils | |
class PaperclipExtend | |
def self.obtain_class | |
class_name = ENV['CLASS'] || ENV['class'] | |
uploads_path = ENV['UPLOADS_PATH'] || ENV['uploads_path'] | |
raise "Must specify CLASS" unless class_name | |
raise "Must specify UPLOADS_PATH" unless uploads_path | |
[class_name.tableize, uploads_path] | |
end | |
def self.convert_id_to_id_partition | |
klass, path = obtain_class | |
base_dir = [path, klass].join("/") | |
dirs = Dir.entries(base_dir) | |
dirs.each do |dir| | |
id = dir | |
if id.to_i == 0 | |
# example . | .. | 000 | |
puts "Not a valid directory #{id}" | |
else | |
id_partition = paperclip_id_partition(id.to_i) | |
old_path = [base_dir, id].join("/") | |
new_path = [base_dir, id_partition].join("/") | |
FileUtils.mkdir_p(new_path) | |
FileUtils.mv Dir.glob("#{old_path}/*"), new_path, :verbose => false | |
FileUtils.rmdir old_path | |
puts "#{old_path.ljust(base_dir.length+5)} => #{new_path}" | |
end | |
end | |
end | |
def self.paperclip_id_partition(id) | |
("%09d" % id).scan(/\d{3}/).join("/") | |
end | |
end | |
namespace :paperclip do | |
desc "Convert id => id_partition for given CLASS and UPLOADS_PATH e.g. 'public/uploads'" | |
task :id_to_id_partition => :environment do | |
PaperclipExtend.convert_id_to_id_partition | |
end | |
end |
It was very helpful for me also, thanks.
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow.. that was long time ago :-) I'm glad it was of help.