houdini/lib/search_vector.rb
Bradley M. Kuhn 6772312ea7 Relicense all .rb files under new project license.
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

with some specific files to be licensed under the one of two licenses:
   CC0-1.0
   LGPL-3.0-or-later

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

64 lines
2 KiB
Ruby

# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
module SearchVector
AcceptedTables = ['supporters', 'payments']
def self.query(query_string, expr=nil)
(expr || Qexpr.new).where(
"to_tsvector('english', coalesce(supporters.name, '') || ' ' || coalesce(supporters.email, '')) @@ plainto_tsquery('english', $search)",
{ search: query_string}
)
end
def self._payments_blob_query
Qexpr.new.select(
'payments.id',
"concat_ws(' '
, payments.gross_amount
, payments.kind
, payments.towards
, supporters.name
, supporters.organization
, supporters.email
, supporters.city
, supporters.state_code
, donations.designation
, donations.dedication
) AS search_blob"
)
.from(:payments)
.left_outer_join('supporters', 'payments.supporter_id=supporters.id')
.left_outer_join('donations', 'payments.donation_id=donations.id')
end
# Construct of query of ids and search blobs for all supporters
# for use in a sub-query
def self._supporters_blob_query
fields_subquery = Qexpr.new.select("string_agg(value::text, ' ') AS value", "supporter_id")
.from(:custom_field_joins)
.group_by(:supporter_id)
.as(:custom_field_joins)
Qexpr.new.select(
'supporters.id',
"concat_ws(' '
, custom_field_joins.value
, supporters.name
, supporters.organization
, supporters.id
, supporters.email
, supporters.city
, supporters.state_code
, donations.designation
, donations.dedication
, payments.kind
, payments.towards
) AS search_blob"
)
.from(:supporters)
.left_outer_join(:payments, "payments.supporter_id=supporters.id")
.left_outer_join(:donations, "donations.supporter_id=supporters.id")
.left_outer_join(fields_subquery, "custom_field_joins.supporter_id=supporters.id")
end
end