Module: PgSearchable::Model::Builder
- Defined in:
- lib/pg-searchable/model.rb
Constant Summary collapse
- DISALLOWED_TSQUERY_CHARACTERS =
/['?\\:‘’]/.freeze
Class Method Summary collapse
- .dictionary ⇒ Object
- .normalize(x) ⇒ Object
- .tsquery(query, prefix:, raw:) ⇒ Object
-
.tsquery_expression(term, negated:, prefix:) ⇒ Object
After this, the SQL expression evaluates to a string containing the term surrounded by single-quotes.
- .tsquery_for_terms(terms, prefix:) ⇒ Object
- .tsquery_term(unsanitized_term, prefix:) ⇒ Object
Class Method Details
.dictionary ⇒ Object
49 50 51 |
# File 'lib/pg-searchable/model.rb', line 49 def self.dictionary Arel::Nodes.build_quoted(:simple) end |
.normalize(x) ⇒ Object
53 54 55 |
# File 'lib/pg-searchable/model.rb', line 53 def self.normalize(x) x # TODO end |
.tsquery(query, prefix:, raw:) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/pg-searchable/model.rb', line 94 def self.tsquery(query, prefix:, raw:) query_terms = query.split(" ").compact tsq = if query.blank? Arel::Nodes.build_quoted("") elsif raw Arel::Nodes.build_quoted(query) else tsquery_for_terms(query_terms, prefix: prefix) end Arel::Nodes::NamedFunction.new("to_tsquery", [dictionary, tsq]) end |
.tsquery_expression(term, negated:, prefix:) ⇒ Object
After this, the SQL expression evaluates to a string containing the term surrounded by single-quotes. If :prefix is true, then the term will have :* appended to the end. If :negated is true, then the term will have ! prepended to the front.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pg-searchable/model.rb', line 80 def self.tsquery_expression(term, negated:, prefix:) terms = [ (Arel::Nodes.build_quoted('!') if negated), Arel::Nodes.build_quoted("' "), Arel::Nodes.build_quoted(term), Arel::Nodes.build_quoted(" '"), (Arel::Nodes.build_quoted(":*") if prefix) ].compact terms.inject do |memo, term| Arel::Nodes::InfixOperation.new("||", memo, Arel::Nodes.build_quoted(term)) end end |
.tsquery_for_terms(terms, prefix:) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/pg-searchable/model.rb', line 57 def self.tsquery_for_terms(terms, prefix:) terms = terms.map { |term| tsquery_term(term, prefix: prefix) } terms.inject do |memo, term| term_anded = Arel::Nodes::InfixOperation.new("||", Arel::Nodes.build_quoted(" & "), term) Arel::Nodes::InfixOperation.new("||", memo, term_anded) end end |
.tsquery_term(unsanitized_term, prefix:) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pg-searchable/model.rb', line 65 def self.tsquery_term(unsanitized_term, prefix:) negated = false if unsanitized_term.start_with?("!") unsanitized_term[0] = '' negated = true end sanitized_term = unsanitized_term.gsub(DISALLOWED_TSQUERY_CHARACTERS, " ") tsquery_expression(sanitized_term, negated: negated, prefix: prefix) end |