diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index ab1d5a9..182c2e0 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -90,7 +90,6 @@ class SearchController < ApplicationController SEARCH_IN = [ [ :articles, N_('Articles') ], - [ :comments, N_('Comments') ], [ :enterprises, N_('Enterprises') ], [ :people, N_('People') ], [ :communities, N_('Communities') ], diff --git a/app/helpers/assets_helper.rb b/app/helpers/assets_helper.rb index d0f75e5..b3efbd9 100644 --- a/app/helpers/assets_helper.rb +++ b/app/helpers/assets_helper.rb @@ -9,7 +9,6 @@ module AssetsHelper [ options.merge(:asset => 'products'), "icon-menu-product", _('Products') ], [ options.merge(:asset => 'enterprises'), "icon-menu-enterprise", _('Enterprises') ], [ options.merge(:asset => 'communities'), "icon-menu-community", _('Communities') ], - [ options.merge(:asset => 'comments'), "icon-menu-comments", _('Comments') ], [ options.merge(:asset => 'events'), "icon-menu-events", _('Events') ], ].map do |target,css_class,name| diff --git a/app/models/article.rb b/app/models/article.rb index 7be9b86..4f5a04f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -21,7 +21,11 @@ class Article < ActiveRecord::Base acts_as_versioned - acts_as_searchable + acts_as_searchable :additional_fields => [ :comment_data ] + + def comment_data + comments.map {|item| [item.title, item.body].join(' ') }.join(' ') + end before_update do |article| article.advertise = true @@ -120,6 +124,10 @@ class Article < ActiveRecord::Base end end + def comments_updated + ferret_update + end + private def sanitize_tag_list diff --git a/app/models/comment.rb b/app/models/comment.rb index 74276a5..3d822dc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,7 +1,5 @@ class Comment < ActiveRecord::Base - acts_as_searchable :fields => [:title, :body] - validates_presence_of :title, :body belongs_to :article, :counter_cache => true belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' @@ -45,4 +43,10 @@ class Comment < ActiveRecord::Base self.find(:all, :order => 'comments.title', :conditions => ['comments.title like (?) or comments.title like (?)', initial + '%', initial.upcase + '%']) end + after_save :notify_article + after_destroy :notify_article + def notify_article + article.comments_updated + end + end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 01f0131..b2072d4 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -286,5 +286,26 @@ class ArticleTest < Test::Unit::TestCase assert_equal true, a1.display_to?(member) end + should 'reindex when comments are changed' do + a = Article.new + a.expects(:ferret_update) + a.comments_updated + end + + should 'index comments title together with article' do + owner = create_user('testuser').person + art = owner.articles.build(:name => 'ytest'); art.save! + c1 = art.comments.build(:title => 'a nice comment', :body => 'anything', :author => owner); c1.save! + + assert_includes Article.find_by_contents('nice'), art + end + + should 'index comments body together with article' do + owner = create_user('testuser').person + art = owner.articles.build(:name => 'ytest'); art.save! + c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save! + + assert_includes Article.find_by_contents('anything'), art + end end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index cad72a3..5f9a7ac 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -93,22 +93,6 @@ class CommentTest < Test::Unit::TestCase assert_equal 'comment-4321', comment.anchor end - should 'be searched by contents of title' do - owner = create_user('testuser').person - art = owner.articles.build(:name => 'ytest'); art.save! - c1 = art.comments.build(:title => 'a nice comment', :body => 'anything', :author => owner); c1.save! - - assert_includes Comment.find_by_contents('nice'), c1 - end - - should 'be searched by contents of body' do - owner = create_user('testuser').person - art = owner.articles.build(:name => 'ytest'); art.save! - c1 = art.comments.build(:title => 'test comment', :body => 'anything', :author => owner); c1.save! - - assert_includes Comment.find_by_contents('anything'), c1 - end - should 'be able to find recent comments' do Comment.delete_all @@ -155,4 +139,25 @@ class CommentTest < Test::Unit::TestCase assert_not_includes list, c2 end + should 'notify article to reindex after saving' do + owner = create_user('testuser').person + article = owner.articles.create!(:name => 'test', :body => '...') + + article.expects(:comments_updated) + + c1 = article.comments.new(:title => "A comment", :body => '...', :author => owner) + c1.stubs(:article).returns(article) + c1.save! + end + + should 'notify article to reindex after being removed' do + owner = create_user('testuser').person + article = owner.articles.create!(:name => 'test', :body => '...') + c1 = article.comments.create!(:title => "A comment", :body => '...', :author => owner) + + c1.stubs(:article).returns(article) + article.expects(:comments_updated) + c1.destroy + end + end -- libgit2 0.21.2