extracts cached tsvector to concern
This commit is contained in:
25
app/models/concerns/search_cache.rb
Normal file
25
app/models/concerns/search_cache.rb
Normal 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
|
||||
Reference in New Issue
Block a user