Class: Mincer::Processors::PgSearch::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/mincer/processors/pg_search/sanitizer.rb

Constant Summary collapse

AVAILABLE_SANITIZERS =
[:coalesce, :ignore_case, :ignore_accent]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, *sanitizers) ⇒ Sanitizer

Returns a new instance of Sanitizer.



8
9
10
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 8

def initialize(term, *sanitizers)
  @term, @sanitizers = term, AVAILABLE_SANITIZERS & Array.wrap(sanitizers).flatten
end

Instance Attribute Details

#sanitizersObject

Returns the value of attribute sanitizers.



6
7
8
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 6

def sanitizers
  @sanitizers
end

#termObject

Returns the value of attribute term.



6
7
8
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 6

def term
  @term
end

Class Method Details

.coalesce(term, val = '') ⇒ Object



54
55
56
57
58
59
60
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 54

def self.coalesce(term, val = '')
  if Mincer.pg_extension_installed?(:unaccent)
    Arel::Nodes::NamedFunction.new('coalesce', [quote(term), quote(val)])
  else
    term
  end
end

.ignore_accent(term) ⇒ Object



50
51
52
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 50

def self.ignore_accent(term)
  Arel::Nodes::NamedFunction.new('unaccent', [quote(term)])
end

.ignore_case(term) ⇒ Object



46
47
48
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 46

def self.ignore_case(term)
  Arel::Nodes::NamedFunction.new('lower', [quote(term)])
end

.quote(string) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 62

def self.quote(string)
  if defined?(Arel::Nodes::Quoted) && !string.is_a?(Arel::Nodes::Quoted) && !string.is_a?(Arel::Nodes::NamedFunction)
    Arel::Nodes::Quoted.new(string)
  else
    string
  end
end

.sanitize_column(term, *sanitizers) ⇒ Object



34
35
36
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 34

def self.sanitize_column(term, *sanitizers)
  new(term, *sanitizers).sanitize_column
end

.sanitize_string(term, *sanitizers) ⇒ Object



38
39
40
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 38

def self.sanitize_string(term, *sanitizers)
  new(term, *sanitizers).sanitize_string
end

.sanitize_string_quoted(term, *sanitizers) ⇒ Object



42
43
44
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 42

def self.sanitize_string_quoted(term, *sanitizers)
  new(term, *sanitizers).sanitize_string(quote: true)
end

Instance Method Details

#sanitize(node) ⇒ Object



27
28
29
30
31
32
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 27

def sanitize(node)
  sanitizers.inject(node) do |query, sanitizer|
    query = self.class.send(sanitizer, query)
    query
  end
end

#sanitize_columnObject



12
13
14
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 12

def sanitize_column
  @sanitized_column ||= sanitize(Arel.sql(@term))
end

#sanitize_string(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/mincer/processors/pg_search/sanitizer.rb', line 16

def sanitize_string(options = {})
  if sanitizers.empty?
    if defined?(Arel::Nodes::Quoted)
      return self.class.quote(@term)
    elsif options[:quote]
      return Mincer.connection.quote(@term)
    end
  end
  @sanitized_string ||= sanitize(@term)
end