Module: Sanitization::ActiveRecordExtension

Defined in:
lib/sanitization/active_record_extension.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

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
"\u180E\u200B\u200C\u200D\u2060\uFEFF".freeze
MULTIBYTE_BLANK =
/[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
MULTIBYTE_SUPPORTED =
"\u0020" == " "

Class Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



3
4
5
6
# File 'lib/sanitization/active_record_extension.rb', line 3

def self.append_features(base)
  super
  base.extend(ClassMethods)
end

.format_value(value, settings) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sanitization/active_record_extension.rb', line 24

def self.format_value(value, settings)
  return value unless value.is_a?(String)

  v = value.dup
  v.strip! if value_or_default(settings, :strip)

  if value_or_default(settings, :collapse)
    if MULTIBYTE_SUPPORTED && Encoding.compatible?(v, MULTIBYTE_BLANK)
      v.gsub!(/#{MULTIBYTE_BLANK}+/, " ")
    else
      v.squeeze!(" ")
    end
  end

  return nil if value_or_default(settings, :nullify) && v.empty? && settings[:null]

  case_method = value_or_default(settings, :case)
  v = v.send(case_method) if case_method && case_method != :none

  v
end

.value_or_default(settings, transform) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/sanitization/active_record_extension.rb', line 46

def self.value_or_default(settings, transform)
  if settings[transform].nil?
    Sanitization.configuration[transform]
  else
    settings[transform]
  end
end