From 72b0a3340d1d8d56cbd90f532f5a7fee948d7548 Mon Sep 17 00:00:00 2001 From: JoenioCosta Date: Thu, 27 Mar 2008 20:05:49 +0000 Subject: [PATCH] ActionItem46: fixing basic search of product, community, enterprise, comment, people and article --- app/controllers/public/search_controller.rb | 11 ++++++++++- app/helpers/application_helper.rb | 4 ++++ app/views/search/_article.rhtml | 1 - app/views/search/_comment.rhtml | 10 ++++++++++ app/views/search/index.rhtml | 13 +++++-------- test/functional/search_controller_test.rb | 24 ++++++++++++++++++++++++ test/unit/application_helper_test.rb | 6 ++++++ test/unit/category_test.rb | 9 +++++++++ 8 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 app/views/search/_comment.rhtml diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 2eb8810..c96a145 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -23,8 +23,17 @@ class SearchController < ApplicationController @finder ||= @environment @results = {} - [:articles, :comments, :enterprises, :people, :communities, :products].each do |key| + @names = {} + [ + [ :articles, _('Articles') ], + [ :comments, _('Comments') ], + [ :enterprises, _('Enterprises') ], + [ :people, _('People') ], + [ :communities, _('Communities') ], + [ :products, _('Products') ] + ].each do |key, description| @results[key] = search(@finder.send(key), @filtered_query) if params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s) + @names[key] = description end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f57d9e7..7a5629d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -318,6 +318,10 @@ module ApplicationHelper end def partial_for_class(klass) + if klass.nil? + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' + end + name = klass.name.underscore if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml")) name diff --git a/app/views/search/_article.rhtml b/app/views/search/_article.rhtml index 8006742..92dc933 100644 --- a/app/views/search/_article.rhtml +++ b/app/views/search/_article.rhtml @@ -8,4 +8,3 @@ <%= strip_tags(hit.abstract) %> - diff --git a/app/views/search/_comment.rhtml b/app/views/search/_comment.rhtml new file mode 100644 index 0000000..f8563f2 --- /dev/null +++ b/app/views/search/_comment.rhtml @@ -0,0 +1,10 @@ +
+
+ + <%= link_to(hit.title, hit.url) %> + +
+
+ <%= strip_tags(hit.body) %> +
+
diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml index de6d113..c249d22 100644 --- a/app/views/search/index.rhtml +++ b/app/views/search/index.rhtml @@ -1,16 +1,13 @@

<%= _('Search results for "%s"') % @query %>

-<% [ [@articles, _('Articles'), 'articles'], - [@people, _('People'), 'people'], - [@enterprises, _('Entreprises'), 'enterprises'], - [@communities, _('Communities'), 'communities'], - [@products, _('Products'), 'products'] ].each do |results, iname, name| %> - <% if results && !results.empty? %> -
-

<%= iname %>

+<% @results.each do |name,results| %> + <% if !results.nil? and !results.empty? %> +
+

<%= @names[name] %>

<% results.each do |hit| %> <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %> <% end %>
<% end %> <% end %> + diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 0c2b181..4f754e1 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -182,4 +182,28 @@ class SearchControllerTest < Test::Unit::TestCase # 'assets' menu should 'list products in a specific category' + should 'display search results' do + ent = Enterprise.create!(:name => 'display enterprise', :identifier => 'teste1') + product = ent.products.create!(:name => 'display product') + person = create_user('displayperson').person; person.name = 'display person'; person.save! + article = person.articles.create!(:name => 'display article') + comment = article.comments.create!(:title => 'display comment', :body => '...', :author => person) + community = Community.create!(:name => 'display community', :identifier => 'an_bea_comm') + + get :index, :query => 'display' + + names = { + :articles => 'Articles', + :comments => 'Comments', + :people => 'People', + :enterprises => 'Enterprises', + :communities => 'Communities', + :products => 'Products', + } + names.each do |thing, description| + assert_tag :tag => 'div', :attributes => { :id => "search-results-#{thing}" }, :descendant => { :tag => 'h3', :content => description } + assert_tag :tag => 'a', :content => "display #{thing.to_s.singularize}" + end + end + end diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 39dcfe1..26cee55 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -16,6 +16,12 @@ class ApplicationHelperTest < Test::Unit::TestCase assert_equal 'runtime_error', partial_for_class(RuntimeError) end + should 'give error when there is no partial for class' do + assert_raises ArgumentError do + partial_for_class(nil) + end + end + should 'generate link to stylesheet' do File.expects(:exists?).with(File.join(RAILS_ROOT, 'public', 'stylesheets', 'something.css')).returns(true) expects(:filename_for_stylesheet).with('something', nil).returns('/stylesheets/something.css') diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index f33db6c..2c80dd1 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -345,4 +345,13 @@ class CategoryTest < Test::Unit::TestCase assert_includes c.people, person assert_not_includes c.people, ent end + + should 'list people that are categorized in children categories' do + c1 = @env.categories.create!(:name => 'top category') + c2 = @env.categories.create!(:name => 'child category', :parent => c1) + person = create_user('test_user').person + person.categories << c2 + assert_includes c1.people, person + end + end -- libgit2 0.21.2