Guest User

Untitled

a guest
Dec 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. class Product << ActiveRecord::Base
  2. before_save :clean_name_description_details
  3.  
  4. def clean_name_description_details
  5. # clean up the name
  6. if name_changed?
  7. self.name = self.name.clean_non_utf8
  8. end
  9. if description_changed?
  10. self.description = self.description.clean_non_utf8
  11. end
  12. if details_changed?
  13. self.details = self.details.clean_non_utf8
  14. end
  15. end
  16. end
  17.  
  18.  
  19. # lib/extensions/string.rb
  20. class String
  21. def clean_non_utf8
  22. converter = Iconv.new('UTF-8//IGNORE//TRANSLIT', 'ASCII//IGNORE//TRANSLIT')
  23. self.gsub!(/[\u00BC]/," 1/4").gsub!(/[\u00BD]/," 1/2").gsub!(/[\u00BE]/," 3/4")
  24. self.gsub!(/[\u2018\u2019\u201A\u201B\u2032\u2035]/,"'").gsub!(/[\u201C\u201D\u201E\u201F\u2033\u2036]/,'"')
  25. converter.iconv(self).unpack('U*').select{|cp| cp < 127}.pack('U*')
  26. end
Add Comment
Please, Sign In to add comment