extracts cached tsvector to concern

This commit is contained in:
rgarcia
2015-11-18 10:19:02 +01:00
parent fa83e196ca
commit 1de91658c7
2 changed files with 27 additions and 15 deletions

View File

@@ -0,0 +1,25 @@
module SearchCache
extend ActiveSupport::Concern
included do
after_save :calculate_tsvector
end
def calculate_tsvector
ActiveRecord::Base.connection.execute("
UPDATE proposals SET tsv = (to_tsvector('spanish', #{fields_to_sql})) WHERE id = #{self.id}")
end
def fields_to_sql
fields.collect { |field| "coalesce(#{field},'')" }.join(" || ' ' || ")
end
def fields
searchable_fields << tags_to_sql
end
def tags_to_sql
"\'#{tag_list.join(' ')}\'"
end
end