2016-10-25 18:45:05 +02:00
|
|
|
# See:
|
|
|
|
# - https://robots.thoughtbot.com/implementing-multi-table-full-text-search-with-postgres
|
|
|
|
# - http://calebthompson.io/talks/search.html
|
|
|
|
class Search < ActiveRecord::Base
|
|
|
|
extend Textacular
|
|
|
|
|
|
|
|
attr_accessor :gestionnaire
|
|
|
|
attr_accessor :query
|
|
|
|
|
|
|
|
belongs_to :dossier
|
|
|
|
|
|
|
|
def results
|
|
|
|
if @query.present?
|
|
|
|
self.class
|
2016-10-28 19:34:29 +02:00
|
|
|
.select("DISTINCT(searches.dossier_id)")
|
2016-10-25 18:45:05 +02:00
|
|
|
.search(@query)
|
|
|
|
.joins(:dossier)
|
|
|
|
.where(dossier_id: @gestionnaire.dossier_ids)
|
|
|
|
.where("dossiers.archived = ? AND dossiers.state != ?", false, "draft")
|
2016-10-28 19:34:29 +02:00
|
|
|
.preload(:dossier)
|
2016-10-25 18:45:05 +02:00
|
|
|
.map(&:dossier)
|
|
|
|
else
|
|
|
|
Search.none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.searchable_language
|
|
|
|
"french"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.searchable_columns
|
|
|
|
%i(term)
|
|
|
|
end
|
|
|
|
|
2016-10-28 19:34:29 +02:00
|
|
|
# Refreshes the materialized searches view.
|
2016-10-25 18:45:05 +02:00
|
|
|
def self.refresh
|
2016-10-28 19:34:29 +02:00
|
|
|
# NOTE: could be executed concurrently
|
|
|
|
# See https://github.com/thoughtbot/scenic#what-about-materialized-views
|
2016-10-25 18:45:05 +02:00
|
|
|
Scenic.database.refresh_materialized_view(table_name, concurrently: false)
|
|
|
|
end
|
|
|
|
end
|