Files
nairobi/app/models/concerns/search_cache.rb
Javi Martín e66b9687a2 Fix calculating tsvector on hidden records
We introduced this bug in commit 55d339572, since we didn't take hidden
records into consideration.

I've tried to use `update_column` to simplify the code, but got a syntax
error `unnamed portal parameter` and didn't find how to fix it.
2022-08-23 14:30:38 +02:00

34 lines
809 B
Ruby

module SearchCache
extend ActiveSupport::Concern
included do
after_save :calculate_tsvector
end
def calculate_tsvector
self.class.with_hidden.where(id: id).update_all("tsv = (#{searchable_values_sql})")
end
private
def searchable_values_sql
searchable_values
.select { |k, _| k.present? }
.map { |value, weight| set_tsvector(value, weight) }
.join(" || ")
end
def set_tsvector(value, weight)
dict = quote(SearchDictionarySelector.call)
"setweight(to_tsvector(#{dict}, unaccent(coalesce(#{quote(strip_html(value))}, ''))), #{quote(weight)})"
end
def quote(value)
ActiveRecord::Base.connection.quote(value)
end
def strip_html(value)
ActionController::Base.helpers.sanitize(value, tags: [])
end
end