Permalink
Browse files

Merge pull request #384 from umamao/update-search-result-answer

Update search result title/summary on answer update
  • Loading branch information...
2 parents 5a85561 + 0673dca commit e6e9acd2c6d21c857fe12ee1bd7546aa6b0f0dd1 Ricardo Panaggio committed Nov 16, 2011
Showing with 30 additions and 9 deletions.
  1. +30 −9 app/models/answer.rb
View
@@ -46,6 +46,7 @@ class Answer < Comment
after_create :create_news_update, :new_answer_notification,
:increment_user_topic_answers_count
+ after_update :update_search_result
before_destroy :unhide_news_update, :decrement_user_topic_answers_count
ensure_index([[:user_id, 1], [:question_id, 1]])
@@ -238,6 +239,30 @@ def decrement_user_topic_answers_count
end
def save_with_search_result
+ search_result = new_search_result
+ if save && search_result.save
+ true
+ else
+ search_result.errors.full_messages.each do |error|
+ errors.add_to_base(error)
+ end
+ search_result.destroy
+ false
+ end
+ end
+
+ def update_search_result
+ if !self.search_result
+ create_search_result
+ return
+ end
+
+ self.search_result.set({:title => title(:truncated => true),
+ :summary => summary})
+ end
+
+protected
+ def new_search_result
search_result =
SearchResult.new(:title => title(:truncated => true),
:summary => summary,
@@ -250,14 +275,10 @@ def save_with_search_result
url_helpers.
question_answer_url(question, id))
self.search_result_id = search_result.id
- if save && search_result.save
- true
- else
- search_result.errors.full_messages.each do |error|
- errors.add_to_base(error)
- end
- search_result.destroy
- false
- end
+ search_result
+ end
+
+ def create_search_result
+ new_search_result.save
end
end

0 comments on commit e6e9acd

Please sign in to comment.