Sha256: e00aa64a21a76e9196482874ca5aec76f6c78619ade83d6710929538dd72d479

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "demoji/version"
require 'active_support/concern'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/object/blank'

module Demoji

  extend ActiveSupport::Concern

  included do
    alias_method_chain :create_or_update, :utf8_rescue
  end

  private

    def create_or_update_with_utf8_rescue
      _rescued_counter ||= 0

      create_or_update_without_utf8_rescue
    rescue ActiveRecord::StatementInvalid => ex
      raise ex unless ex.message.match /Mysql2::Error: Incorrect string value:/

      _rescued_counter += 1

      raise ex if _rescued_counter > 1

      _fix_utf8_attributes
      retry
    end

    def _fix_utf8_attributes
      self.attributes.each do |k, v|
        next if v.blank? || !v.is_a?(String) || (self.column_for_attribute(k).type == :binary)
        self.send "#{k}=", _fix_chars(v)
      end
    end

    def _fix_chars(str)
      "".tap do |out_str|

        # for instead of split and joins for perf
        for i in (0...str.length)
          char = str[i]
          char = 32.chr if char.ord > 65535
          out_str << char
        end

      end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
demoji-0.0.6 lib/demoji.rb