Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Product << ActiveRecord::Base
- before_save :clean_name_description_details
- def clean_name_description_details
- # clean up the name
- if name_changed?
- self.name = self.name.clean_non_utf8
- end
- if description_changed?
- self.description = self.description.clean_non_utf8
- end
- if details_changed?
- self.details = self.details.clean_non_utf8
- end
- end
- end
- # lib/extensions/string.rb
- class String
- def clean_non_utf8
- converter = Iconv.new('UTF-8//IGNORE//TRANSLIT', 'ASCII//IGNORE//TRANSLIT')
- self.gsub!(/[\u00BC]/," 1/4").gsub!(/[\u00BD]/," 1/2").gsub!(/[\u00BE]/," 3/4")
- self.gsub!(/[\u2018\u2019\u201A\u201B\u2032\u2035]/,"'").gsub!(/[\u201C\u201D\u201E\u201F\u2033\u2036]/,'"')
- converter.iconv(self).unpack('U*').select{|cp| cp < 127}.pack('U*')
- end
Add Comment
Please, Sign In to add comment