public
Last active

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.