Commit 72b0a3340d1d8d56cbd90f532f5a7fee948d7548
1 parent
2b4760fd
Exists in
master
and in
28 other branches
ActionItem46: fixing basic search of product, community, enterprise, comment, people and article
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1608 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
8 changed files
with
68 additions
and
10 deletions
Show diff stats
app/controllers/public/search_controller.rb
| ... | ... | @@ -23,8 +23,17 @@ class SearchController < ApplicationController |
| 23 | 23 | @finder ||= @environment |
| 24 | 24 | |
| 25 | 25 | @results = {} |
| 26 | - [:articles, :comments, :enterprises, :people, :communities, :products].each do |key| | |
| 26 | + @names = {} | |
| 27 | + [ | |
| 28 | + [ :articles, _('Articles') ], | |
| 29 | + [ :comments, _('Comments') ], | |
| 30 | + [ :enterprises, _('Enterprises') ], | |
| 31 | + [ :people, _('People') ], | |
| 32 | + [ :communities, _('Communities') ], | |
| 33 | + [ :products, _('Products') ] | |
| 34 | + ].each do |key, description| | |
| 27 | 35 | @results[key] = search(@finder.send(key), @filtered_query) if params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s) |
| 36 | + @names[key] = description | |
| 28 | 37 | end |
| 29 | 38 | end |
| 30 | 39 | ... | ... |
app/helpers/application_helper.rb
| ... | ... | @@ -318,6 +318,10 @@ module ApplicationHelper |
| 318 | 318 | end |
| 319 | 319 | |
| 320 | 320 | def partial_for_class(klass) |
| 321 | + if klass.nil? | |
| 322 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | |
| 323 | + end | |
| 324 | + | |
| 321 | 325 | name = klass.name.underscore |
| 322 | 326 | if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml")) |
| 323 | 327 | name | ... | ... |
app/views/search/_article.rhtml
app/views/search/index.rhtml
| 1 | 1 | <h2> <%= _('Search results for "%s"') % @query %> </h2> |
| 2 | 2 | |
| 3 | -<% [ [@articles, _('Articles'), 'articles'], | |
| 4 | - [@people, _('People'), 'people'], | |
| 5 | - [@enterprises, _('Entreprises'), 'enterprises'], | |
| 6 | - [@communities, _('Communities'), 'communities'], | |
| 7 | - [@products, _('Products'), 'products'] ].each do |results, iname, name| %> | |
| 8 | - <% if results && !results.empty? %> | |
| 9 | - <div id="search-result-<%= name %>"> | |
| 10 | - <h3> <%= iname %> </h3> | |
| 3 | +<% @results.each do |name,results| %> | |
| 4 | + <% if !results.nil? and !results.empty? %> | |
| 5 | + <div id="search-results-<%= name %>"> | |
| 6 | + <h3><%= @names[name] %></h3> | |
| 11 | 7 | <% results.each do |hit| %> |
| 12 | 8 | <%= render :partial => partial_for_class(hit.class), :locals => { :hit => hit } %> |
| 13 | 9 | <% end %> |
| 14 | 10 | </div> |
| 15 | 11 | <% end %> |
| 16 | 12 | <% end %> |
| 13 | + | ... | ... |
test/functional/search_controller_test.rb
| ... | ... | @@ -182,4 +182,28 @@ class SearchControllerTest < Test::Unit::TestCase |
| 182 | 182 | # 'assets' menu |
| 183 | 183 | should 'list products in a specific category' |
| 184 | 184 | |
| 185 | + should 'display search results' do | |
| 186 | + ent = Enterprise.create!(:name => 'display enterprise', :identifier => 'teste1') | |
| 187 | + product = ent.products.create!(:name => 'display product') | |
| 188 | + person = create_user('displayperson').person; person.name = 'display person'; person.save! | |
| 189 | + article = person.articles.create!(:name => 'display article') | |
| 190 | + comment = article.comments.create!(:title => 'display comment', :body => '...', :author => person) | |
| 191 | + community = Community.create!(:name => 'display community', :identifier => 'an_bea_comm') | |
| 192 | + | |
| 193 | + get :index, :query => 'display' | |
| 194 | + | |
| 195 | + names = { | |
| 196 | + :articles => 'Articles', | |
| 197 | + :comments => 'Comments', | |
| 198 | + :people => 'People', | |
| 199 | + :enterprises => 'Enterprises', | |
| 200 | + :communities => 'Communities', | |
| 201 | + :products => 'Products', | |
| 202 | + } | |
| 203 | + names.each do |thing, description| | |
| 204 | + assert_tag :tag => 'div', :attributes => { :id => "search-results-#{thing}" }, :descendant => { :tag => 'h3', :content => description } | |
| 205 | + assert_tag :tag => 'a', :content => "display #{thing.to_s.singularize}" | |
| 206 | + end | |
| 207 | + end | |
| 208 | + | |
| 185 | 209 | end | ... | ... |
test/unit/application_helper_test.rb
| ... | ... | @@ -16,6 +16,12 @@ class ApplicationHelperTest < Test::Unit::TestCase |
| 16 | 16 | assert_equal 'runtime_error', partial_for_class(RuntimeError) |
| 17 | 17 | end |
| 18 | 18 | |
| 19 | + should 'give error when there is no partial for class' do | |
| 20 | + assert_raises ArgumentError do | |
| 21 | + partial_for_class(nil) | |
| 22 | + end | |
| 23 | + end | |
| 24 | + | |
| 19 | 25 | should 'generate link to stylesheet' do |
| 20 | 26 | File.expects(:exists?).with(File.join(RAILS_ROOT, 'public', 'stylesheets', 'something.css')).returns(true) |
| 21 | 27 | expects(:filename_for_stylesheet).with('something', nil).returns('/stylesheets/something.css') | ... | ... |
test/unit/category_test.rb
| ... | ... | @@ -345,4 +345,13 @@ class CategoryTest < Test::Unit::TestCase |
| 345 | 345 | assert_includes c.people, person |
| 346 | 346 | assert_not_includes c.people, ent |
| 347 | 347 | end |
| 348 | + | |
| 349 | + should 'list people that are categorized in children categories' do | |
| 350 | + c1 = @env.categories.create!(:name => 'top category') | |
| 351 | + c2 = @env.categories.create!(:name => 'child category', :parent => c1) | |
| 352 | + person = create_user('test_user').person | |
| 353 | + person.categories << c2 | |
| 354 | + assert_includes c1.people, person | |
| 355 | + end | |
| 356 | + | |
| 348 | 357 | end | ... | ... |