2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
module SearchVector
|
2019-07-30 21:29:24 +00:00
|
|
|
AcceptedTables = %w[supporters payments].freeze
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.query(query_string, expr = nil)
|
2018-03-25 17:30:42 +00:00
|
|
|
(expr || Qexpr.new).where(
|
|
|
|
"to_tsvector('english', coalesce(supporters.name, '') || ' ' || coalesce(supporters.email, '')) @@ plainto_tsquery('english', $search)",
|
2019-07-30 21:29:24 +00:00
|
|
|
search: query_string
|
2018-03-25 17:30:42 +00:00
|
|
|
)
|
|
|
|
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"
|
|
|
|
)
|
2019-07-30 21:29:24 +00:00
|
|
|
.from(:payments)
|
|
|
|
.left_outer_join('supporters', 'payments.supporter_id=supporters.id')
|
|
|
|
.left_outer_join('donations', 'payments.donation_id=donations.id')
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Construct of query of ids and search blobs for all supporters
|
|
|
|
# for use in a sub-query
|
|
|
|
def self._supporters_blob_query
|
2019-07-30 21:29:24 +00:00
|
|
|
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)
|
2018-03-25 17:30:42 +00:00
|
|
|
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"
|
|
|
|
)
|
2019-07-30 21:29:24 +00:00
|
|
|
.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')
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|