Class: OpenLoam::Search::LikeDriver

Inherits:
Driver
  • Object
show all
Defined in:
lib/open_loam/search/like_driver.rb

Overview

The default driver: a substring LIKE across the declared columns — exactly the original OpenLoam::Searchable behavior, now behind the seam. Portable (Arel's matches emits ILIKE on PostgreSQL, case-insensitive LIKE on SQLite for ASCII) and needs no index, so index/remove/reindex are no-ops.

What it does NOT do — ignore accents, split on words, rank — is the reason the seam exists: swap in OpenLoam::Search::TokenDriver (or an external engine) and every Model.search(q) call site keeps working unchanged.

Class Method Summary collapse

Methods inherited from Driver

index, reindex, remove

Class Method Details

.search(model, query, scope:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/open_loam/search/like_driver.rb', line 13

def search(model, query, scope:)
  query = query.to_s.strip
  return scope if query.blank? || !model.open_loam_searchable?

  # sanitize_sql_like escapes the LIKE wildcards so a literal % or _ in
  # the query stays literal; the second arg to `matches` declares that
  # escape character to the database (SQLite has none by default).
  pattern = "%#{model.sanitize_sql_like(query)}%"
  conditions = model.open_loam_searchable_columns.map do |column|
    model.arel_table[column].matches(pattern, "\\")
  end
  scope.where(conditions.inject(:or))
end