Commit 744c1e8146252816b4a2dcd3f944808482b75d48

Authored by Leandro Santos
1 parent 0700b802

search articles filter by categories

lib/noosfero/api/v1/search.rb
@@ -20,13 +20,13 @@ module Noosfero @@ -20,13 +20,13 @@ module Noosfero
20 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') 20 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article')
21 21
22 scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? 22 scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present?
23 -  
24 - category = environment.categories.find(params[:category]) unless params[:category].nil?  
25 23
  24 + scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present?
  25 +
26 query = params[:query] || "" 26 query = params[:query] || ""
27 order = "more_recent" 27 order = "more_recent"
28 28
29 - options = {:filter => order, :template_id => params[:template_id], :category => category} 29 + options = {:filter => order, :template_id => params[:template_id]}
30 30
31 search_result = find_by_contents(asset, context, scope, query, paginate_options, options) 31 search_result = find_by_contents(asset, context, scope, query, paginate_options, options)
32 32
test/unit/api/search_test.rb
@@ -113,10 +113,26 @@ class SearchTest < ActiveSupport::TestCase @@ -113,10 +113,26 @@ class SearchTest < ActiveSupport::TestCase
113 article = fast_create(Article, :profile_id => person.id) 113 article = fast_create(Article, :profile_id => person.id)
114 category = fast_create(Category) 114 category = fast_create(Category)
115 article.categories<< category 115 article.categories<< category
116 - get "/api/v1/search/article?category=#{category.id}" 116 + get "/api/v1/search/article?category_ids=#{category.id}"
117 json = JSON.parse(last_response.body) 117 json = JSON.parse(last_response.body)
118 assert_equal 1, json['articles'].count 118 assert_equal 1, json['articles'].count
119 assert_equal article.id, json['articles'].first["id"] 119 assert_equal article.id, json['articles'].first["id"]
120 end 120 end
121 121
  122 + should 'search filter by more than one category' do
  123 + Article.delete_all
  124 + fast_create(Article, :profile_id => person.id)
  125 + article1 = fast_create(Article, :profile_id => person.id)
  126 + article2 = fast_create(Article, :profile_id => person.id)
  127 + category1 = fast_create(Category)
  128 + category2 = fast_create(Category)
  129 + article1.categories<< category1
  130 + article2.categories<< category2
  131 + get "/api/v1/search/article?category_ids[]=#{category1.id}&category_ids[]=#{category2.id}"
  132 + json = JSON.parse(last_response.body)
  133 + assert_equal 2, json['articles'].count
  134 + assert_equal article1.id, json['articles'].first["id"]
  135 + assert_equal article2.id, json['articles'].last["id"]
  136 + end
  137 +
122 end 138 end