Module: ConcernsOnRails::Models::Searchable

Extended by:
ActiveSupport::Concern
Defined in:
lib/concerns_on_rails/models/searchable.rb

Overview

LIKE-based search across one or more columns.

class Article < ApplicationRecord
  include ConcernsOnRails::Searchable

  searchable_by :title, :body
end

Article.search("hello")            # WHERE title ILIKE '%hello%' OR body ILIKE '%hello%'
Article.search("")                 # no-op — returns the full relation
Article.search("foo").where(...)   # chainable like any scope

Uses Arel’s ‘matches`, which emits ILIKE on Postgres and LIKE elsewhere —so case-insensitivity comes for free on PG. The query is escaped before interpolation, so `%` / `_` / `` from user input are treated as literals.

Constant Summary collapse

LIKE_ESCAPE =
"\\".freeze
LIKE_SPECIAL =
/[\\%_]/