Orbit/lib/tasks/uploader.rake

41 lines
1.0 KiB
Ruby
Raw Permalink Normal View History

namespace :uploader do
task :recreate => :environment do
mongoid_collections = Mongoid.database.collections.reject{|c| c.name.include?('.')}
ARGV.shift
if ARGV.present?
uploaders = []
ARGV.each{ |arg|
task arg.to_sym do ; end
uploaders << arg
}
else
uploaders = %w(AvatarUploader ImageUploader)
end
klasses = mongoid_collections.inject([]) { |klasses, coll|
if klasses.blank?
klasses = coll.find().map{|c| c['_type']}.uniq.compact
else
klasses += coll.find().map{|c| c['_type']}.uniq.compact
end
klasses
}
to_recreate = klasses.inject([]) { |names, name|
klass = name.constantize rescue nil
klass.uploaders.each_value{ |value|
if uploaders.include?(value.to_s) && !names.include?(name)
names << name
end
} unless klass.nil?
names
}
to_recreate.each { |name|
klass = name.constantize
klass.all.each{ |object|
klass.uploaders.each_key{ |key|
object.send(key).recreate_versions! if object[key] rescue nil
}
}
}
end
end