2013-02-05 14:16:51 -05:00
|
|
|
class SearchObserver < ActiveRecord::Observer
|
|
|
|
observe :topic, :post, :user, :category
|
|
|
|
|
|
|
|
def self.scrub_html_for_search(html)
|
|
|
|
HtmlScrubber.scrub(html)
|
|
|
|
end
|
|
|
|
|
2013-05-22 15:33:33 -04:00
|
|
|
def self.update_index(table, id, search_data)
|
|
|
|
table_name = "#{table}_search_data"
|
|
|
|
foreign_key = "#{table}_id"
|
|
|
|
|
|
|
|
# Would be nice to use AR here but not sure how to execut Postgres functions
|
|
|
|
# when inserting data like this.
|
|
|
|
rows = Post.exec_sql_row_count("UPDATE #{table_name} SET search_data = TO_TSVECTOR('english', ?) WHERE #{foreign_key} = ?", search_data, id)
|
|
|
|
if rows == 0
|
|
|
|
Post.exec_sql("INSERT INTO #{table_name} (#{foreign_key}, search_data) VALUES (?, TO_TSVECTOR('english', ?))", id, search_data)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-22 15:33:33 -04:00
|
|
|
rescue
|
|
|
|
# don't allow concurrency to mess up saving a post
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.update_posts_index(post_id, cooked, title, category)
|
2013-05-22 15:33:33 -04:00
|
|
|
search_data = scrub_html_for_search(cooked) << " " << title
|
|
|
|
search_data << " " << category if category
|
|
|
|
update_index('post', post_id, search_data)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.update_users_index(user_id, username, name)
|
2013-05-22 15:33:33 -04:00
|
|
|
search_data = username.dup << " " << (name || "")
|
|
|
|
update_index('user', user_id, search_data)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.update_categories_index(category_id, name)
|
2013-05-22 15:33:33 -04:00
|
|
|
update_index('category', category_id, name)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def after_save(obj)
|
|
|
|
if obj.class == Post && obj.cooked_changed?
|
|
|
|
category_name = obj.topic.category.name if obj.topic.category
|
|
|
|
SearchObserver.update_posts_index(obj.id, obj.cooked, obj.topic.title, category_name)
|
|
|
|
end
|
|
|
|
if obj.class == User && (obj.username_changed? || obj.name_changed?)
|
|
|
|
SearchObserver.update_users_index(obj.id, obj.username, obj.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
if obj.class == Topic && obj.title_changed?
|
|
|
|
if obj.posts
|
2013-02-07 10:45:24 -05:00
|
|
|
post = obj.posts.where(post_number: 1).first
|
2013-02-05 14:16:51 -05:00
|
|
|
if post
|
|
|
|
category_name = obj.category.name if obj.category
|
|
|
|
SearchObserver.update_posts_index(post.id, post.cooked, obj.title, category_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-07 10:45:24 -05:00
|
|
|
if obj.class == Category && obj.name_changed?
|
2013-02-05 14:16:51 -05:00
|
|
|
SearchObserver.update_categories_index(obj.id, obj.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class HtmlScrubber < Nokogiri::XML::SAX::Document
|
|
|
|
attr_reader :scrubbed
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@scrubbed = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.scrub(html)
|
2013-02-28 13:54:12 -05:00
|
|
|
me = new
|
2013-02-05 14:16:51 -05:00
|
|
|
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
2013-02-07 10:45:24 -05:00
|
|
|
begin
|
|
|
|
copy = "<div>"
|
2013-02-05 14:16:51 -05:00
|
|
|
copy << html unless html.nil?
|
|
|
|
copy << "</div>"
|
|
|
|
parser.parse(html) unless html.nil?
|
|
|
|
end
|
|
|
|
me.scrubbed
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_element(name, attributes=[])
|
|
|
|
attributes = Hash[*attributes.flatten]
|
|
|
|
if attributes["alt"]
|
|
|
|
scrubbed << " "
|
|
|
|
scrubbed << attributes["alt"]
|
|
|
|
scrubbed << " "
|
|
|
|
end
|
|
|
|
if attributes["title"]
|
|
|
|
scrubbed << " "
|
2013-02-07 10:45:24 -05:00
|
|
|
scrubbed << attributes["title"]
|
2013-02-05 14:16:51 -05:00
|
|
|
scrubbed << " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def characters(string)
|
|
|
|
scrubbed << " "
|
|
|
|
scrubbed << string
|
|
|
|
scrubbed << " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|