Skip to content

Instantly share code, notes, and snippets.

@bplexico
Created June 9, 2013 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bplexico/5745171 to your computer and use it in GitHub Desktop.
Save bplexico/5745171 to your computer and use it in GitHub Desktop.
SearchObserver with no update_index
class SearchObserver < ActiveRecord::Observer
observe :topic, :post, :user, :category
def after_save(obj)
if obj.class == Post && obj.cooked_changed?
search_data = obj.scrubbed_html << " " << obj.topic.title
search_data << " " << obj.topic.category.name if obj.topic.category
ObserverSearchData.new(obj, search_data).update_index
end
if obj.class == User && (obj.username_changed? || obj.name_changed?)
search_data = obj.username.dup << " " << (obj.name || "")
ObserverSearchData.new(obj, search_data).update_index
end
if obj.class == Topic && obj.title_changed?
if obj.posts
post = obj.posts.where(post_number: 1).first
if post
search_data = post.scrubbed_html << " " << obj.title
search_data << " " << obj.category.name if obj.category
ObserverSearchData.new(post, search_data).update_index
end
end
end
if obj.class == Category && obj.name_changed?
ObserverSearchData.new(obj, obj.name).update_index
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment