Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. def old_path
  2. ':rails_root/public/system/:class/:attachment/:id_partition/:style/:filename'
  3. end
  4.  
  5. def migrate_old_pdf
  6. logger = Logger.new('./migrate.log')
  7. Invoice.find_each do |invoice|
  8. unless invoice.pdf?
  9. logger.warn "#{invoice.id} にPDFを持っていないためskipします"
  10. next
  11. end
  12. pdf_path = Paperclip::Interpolations.interpolate(old_path, invoice.pdf, 'original')
  13. unless File.exists?(pdf_path)
  14. logger.warn "#{invoice.id} にPDFを持っていますがFSにファイルが存在していないのでskipします"
  15. next
  16. end
  17. File.open(pdf_path) do |file|
  18. invoice.pdf = file
  19. invoice.save!
  20. end
  21. logger.info "#{invoice.id} のPDFを移行しました"
  22. end
  23. end
  24.  
  25. def delete_old_pdf
  26. require 'FileUtils'
  27. Invoice.find_each do |invoice|
  28. pdf_path = Paperclip::Interpolations.interpolate(old_path, invoice.pdf, 'original')
  29. FileUtils.rm(pdf_path)
  30. end
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement