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

View File

@@ -5,6 +5,7 @@ class Proposal < ActiveRecord::Base
include Measurable
include Sanitizable
include PgSearch
include SearchCache
apply_simple_captcha
acts_as_votable
@@ -28,7 +29,6 @@ class Proposal < ActiveRecord::Base
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
before_validation :set_responsible_name
before_validation :calculate_tsvector
before_save :calculate_hot_score, :calculate_confidence_score
@@ -59,20 +59,7 @@ class Proposal < ActiveRecord::Base
}
def searchable_fields
%w(title question summary description) << tags_to_sql
end
def tags_to_sql
"\'#{tag_list.join(' ')}\'"
end
def searchable_content
searchable_fields.collect { |field| "coalesce(#{field},'')" }.join(" || ' ' || ")
end
def calculate_tsvector
ActiveRecord::Base.connection.execute("
UPDATE proposals SET tsv = (to_tsvector('spanish', #{searchable_content})) WHERE id = #{self.id}")
%w(title question summary description)
end
def description