demarches-normaliennes/app/models/search.rb

42 lines
1 KiB
Ruby
Raw Normal View History

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
.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")
.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
# Refreshes the materialized searches view.
2016-10-25 18:45:05 +02:00
def self.refresh
# 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