Skip to content

Instantly share code, notes, and snippets.

@dmitry
Created September 19, 2009 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry/189479 to your computer and use it in GitHub Desktop.
Save dmitry/189479 to your computer and use it in GitHub Desktop.
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