Class: ActiveCleaner::Utf8mb3Cleaner

Inherits:
BaseCleaner show all
Defined in:
lib/active_cleaner/utf8mb3_cleaner.rb

Overview

Utf8mb3Cleaner

Cleans a string by removes all 4-bytes encoded chars in UTF8 strings that mess with the utf8mb3 (also simply known as utf8) encoding in MySQL.

Useful for user input that may contain iOS6 emojis for example (as they are only compatible with utf8mb4 and cause string truncation, at best).

It turns "String with emoticon 😀" into "String with emoticon".

Options

:nilify

Whether or not set the field to nil when the field was or is cleaned to "". Default to false.

Example

class Comment
  include ActiveCleaner

  clean :body, as: :utf8mb3
end

comment = Comment.new(body: "Nice! 😀")
comment.save
comment.body
# => "Nice!"

Instance Attribute Summary

Attributes inherited from BaseCleaner

#attr_name, #options

Instance Method Summary collapse

Methods inherited from BaseCleaner

#==, #clean, #initialize, kind, #kind, #nilify_value?

Constructor Details

This class inherits a constructor from ActiveCleaner::BaseCleaner

Instance Method Details

#clean_value(old_value, _record = nil) ⇒ Object

Cleans the value.



35
36
37
38
39
40
41
42
# File 'lib/active_cleaner/utf8mb3_cleaner.rb', line 35

def clean_value(old_value, _record = nil)
  case old_value
  when String
    old_value.each_char.select { |char| char.bytesize < 4 }.join
  else
    old_value
  end
end