Class: Rails::Contact::Search::Backends::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/contact/search/backends/database.rb

Constant Summary collapse

MAX_QUERY_LENGTH =

Cap user input so the LIKE pattern stays bounded; 200 chars covers any realistic name/email/phone substring.

200
METADATA_KEY_FORMAT =

Configured metadata keys are interpolated into SQL fragments, so they must be plain identifiers. This is a foot-gun guard for host developers, not a user-input path — params never reach the key.

/\A[a-zA-Z0-9_]+\z/

Instance Method Summary collapse

Instance Method Details

#search(query, filters, page:, per_page:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rails/contact/search/backends/database.rb', line 15

def search(query, filters, page:, per_page:)
  query = sanitize_query(query)
  # Free-text search keeps recency order: searching means hunting a
  # specific contact, so a metadata sort would only bury the match.
  filters = filters.except("sort") if query.present?
  offset = (page - 1) * per_page
  scope = Contact.includes(:emails, :phones, :labels).recent_first
  scope = apply_filters(scope, filters)
  scope = apply_query(scope, query) if query.present?

  Search::Result.new(
    records: scope.offset(offset).limit(per_page).to_a,
    total_count: count_for(scope),
    page: page,
    per_page: per_page
  )
end