module Sanitization::ActiveRecordExtension::InstanceMethods

Constants

MULTIBYTE_BLANK
MULTIBYTE_SPACE
MULTIBYTE_SUPPORTED
MULTIBYTE_WHITE

Taken from ‘strip_attributes`: github.com/rmm5t/strip_attributes/blob/master/lib/strip_attributes.rb Unicode invisible and whitespace characters. The POSIX character class

:space:

corresponds to the Unicode class Z (“separator”). We also

include the following characters from Unicode class C (“control”), which are spaces or invisible characters that make no sense at the start or end of a string:

U+180E MONGOLIAN VOWEL SEPARATOR
U+200B ZERO WIDTH SPACE
U+200C ZERO WIDTH NON-JOINER
U+200D ZERO WIDTH JOINER
U+2060 WORD JOINER
U+FEFF ZERO WIDTH NO-BREAK SPACE

Private Instance Methods

sanitization__format_column(col_name, col_formatting) click to toggle source
# File lib/sanitization/active_record_extension.rb, line 96
def sanitization__format_column(col_name, col_formatting)
  return unless self[col_name].is_a?(String)

  self[col_name].strip! if value_or_default(col_formatting, :strip)

  if value_or_default(col_formatting, :collapse)
    if MULTIBYTE_SUPPORTED && Encoding.compatible?(self[col_name], MULTIBYTE_BLANK)
      self[col_name].gsub!(/#{MULTIBYTE_BLANK}+/, " ")
    else
      self[col_name].squeeze!(" ")
    end
  end

  if value_or_default(col_formatting, :nullify) && !self[col_name].nil? && self[col_name].to_s.empty? && \
    self.class.columns.select { |c| c.name == col_name }.first.null
    return self[col_name] = nil
  end

  case_formatting_method = value_or_default(col_formatting, :case)
  if !case_formatting_method.nil? && case_formatting_method != :none
    self[col_name] = self[col_name].send(case_formatting_method)
  end

  self[col_name]
end
sanitization__format_strings() click to toggle source
# File lib/sanitization/active_record_extension.rb, line 87
def sanitization__format_strings
  return unless self.class.sanitization__store

  class_formatting = self.class.sanitization__store
  class_formatting.each_pair do |col_name, col_formatting|
    sanitization__format_column(col_name, col_formatting)
  end
end
value_or_default(col_formatting, transform) click to toggle source
# File lib/sanitization/active_record_extension.rb, line 122
def value_or_default(col_formatting, transform)
  if col_formatting[transform].nil?
    Sanitization.configuration[transform]
  else
    col_formatting[transform]
  end
end