Sha256: cdee5290aae001ed5941a2181de0ec0b6920220a3b7889f966296e79bc14aa3a

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module ActiveCleaner
  # = 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 <tt>"String with emoticon 😀"</tt> into <tt>"String with emoticon"</tt>.
  #
  # == Options
  #
  # [:nilify]
  #   Whether or not set the field to +nil+ when the field was or is cleaned to <tt>""</tt>.
  #   Default to +false+.
  #
  # == Example
  #
  #   class Comment
  #     include ActiveCleaner
  #
  #     clean :body, as: :utf8mb3
  #   end
  #
  #   comment = Comment.new(body: "Nice! 😀")
  #   comment.save
  #   comment.body
  #   # => "Nice!"
  class Utf8mb3Cleaner < BaseCleaner

    # Cleans the value.
    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activecleaner-0.3.3 lib/active_cleaner/utf8mb3_cleaner.rb
activecleaner-0.3.2 lib/active_cleaner/utf8mb3_cleaner.rb