diff --git a/lib/noosfero/api/v1/search.rb b/lib/noosfero/api/v1/search.rb index 00dec52..1b0005c 100644 --- a/lib/noosfero/api/v1/search.rb +++ b/lib/noosfero/api/v1/search.rb @@ -20,13 +20,13 @@ module Noosfero scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? - - category = environment.categories.find(params[:category]) unless params[:category].nil? + scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present? + query = params[:query] || "" order = "more_recent" - options = {:filter => order, :template_id => params[:template_id], :category => category} + options = {:filter => order, :template_id => params[:template_id]} search_result = find_by_contents(asset, context, scope, query, paginate_options, options) diff --git a/test/unit/api/search_test.rb b/test/unit/api/search_test.rb index aa47c5f..cb080e5 100644 --- a/test/unit/api/search_test.rb +++ b/test/unit/api/search_test.rb @@ -113,10 +113,26 @@ class SearchTest < ActiveSupport::TestCase article = fast_create(Article, :profile_id => person.id) category = fast_create(Category) article.categories<< category - get "/api/v1/search/article?category=#{category.id}" + get "/api/v1/search/article?category_ids=#{category.id}" json = JSON.parse(last_response.body) assert_equal 1, json['articles'].count assert_equal article.id, json['articles'].first["id"] end + should 'search filter by more than one category' do + Article.delete_all + fast_create(Article, :profile_id => person.id) + article1 = fast_create(Article, :profile_id => person.id) + article2 = fast_create(Article, :profile_id => person.id) + category1 = fast_create(Category) + category2 = fast_create(Category) + article1.categories<< category1 + article2.categories<< category2 + get "/api/v1/search/article?category_ids[]=#{category1.id}&category_ids[]=#{category2.id}" + json = JSON.parse(last_response.body) + assert_equal 2, json['articles'].count + assert_equal article1.id, json['articles'].first["id"] + assert_equal article2.id, json['articles'].last["id"] + end + end -- libgit2 0.21.2