Create a gist now

Instantly share code, notes, and snippets.

desc "Moves files to a new :path NB! backup your files before use this task (set CLASS and PATH, ATTACHMENT is optional)"
task :change_path => :environment do
raise 'PATH must be specified, like PATH=:rails_root/public/system/:class/:attachment/:style/:id.:extension' if ENV['PATH'].blank?
path = ENV['PATH']
for_all_attachments do |instance, name|
object = instance.send(name)
old_path = object.instance_variable_get :'@path'
styles = [:original] + object.styles.keys
styles.each do |style|
if object.exists?(style)
object.queued_for_write[style] = object.to_file(style)
end
end
object.instance_variable_set :'@path', path
object.flush_writes
object.instance_variable_set :'@path', old_path
object.instance_variable_set :'@queued_for_delete', styles.map { |style| object.path(style) }
object.flush_deletes
# if stopped working then use:
# path = Paperclip::Interpolations.interpolate(path, object, style)
# and own write and delete functions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment