6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/rails/contact/search/backends/database.rb', line 6
def search(query, filters, page:, per_page:)
offset = (page - 1) * per_page
scope = Contact.includes(:emails, :phones, :labels).recent_first
scope = apply_filters(scope, filters)
if query.blank?
total = scope.count
records = scope.offset(offset).limit(per_page).to_a
return Search::Result.new(records: records, total_count: total, page: page, per_page: per_page)
end
wildcard = "%#{query.downcase}%"
filtered = scope.left_joins(:emails, :phones, :labels).where(
"LOWER(rails_contact_contacts.given_name) LIKE :q OR "\
"LOWER(rails_contact_contacts.family_name) LIKE :q OR "\
"LOWER(COALESCE(rails_contact_contacts.metadata->>'company', '')) LIKE :q OR "\
"LOWER(COALESCE(rails_contact_contacts.metadata->>'job_title', '')) LIKE :q OR "\
"LOWER(rails_contact_contact_emails.value) LIKE :q OR "\
"rails_contact_contact_phones.e164 LIKE :raw OR "\
"LOWER(rails_contact_labels.name) LIKE :q",
q: wildcard,
raw: "%#{query}%"
).distinct
total = filtered.count(:id)
records = filtered.offset(offset).limit(per_page).to_a
Search::Result.new(records: records, total_count: total, page: page, per_page: per_page)
end
|