Commit 7a56a75ed67c4b1a9e63b6d15b9a3ab86852bab3

Authored by Michel Felipe
Committed by Victor Costa
1 parent 4c7d2f35

api: added param 'has_children' into search article endpoint

lib/noosfero/api/v1/articles.rb
@@ -197,7 +197,7 @@ module Noosfero @@ -197,7 +197,7 @@ module Noosfero
197 child.hit 197 child.hit
198 present child, :with => Entities::Article, :fields => params[:fields] 198 present child, :with => Entities::Article, :fields => params[:fields]
199 end 199 end
200 - 200 +
201 desc 'Suggest a article to another profile' do 201 desc 'Suggest a article to another profile' do
202 detail 'Suggest a article to another profile (person, community...)' 202 detail 'Suggest a article to another profile (person, community...)'
203 params Noosfero::API::Entities::Article.documentation 203 params Noosfero::API::Entities::Article.documentation
lib/noosfero/api/v1/search.rb
@@ -23,6 +23,8 @@ module Noosfero @@ -23,6 +23,8 @@ module Noosfero
23 23
24 scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present? 24 scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present?
25 25
  26 + scope = scope.where('children_count > 0') if params[:has_children].present?
  27 +
26 query = params[:query] || "" 28 query = params[:query] || ""
27 order = "more_recent" 29 order = "more_recent"
28 30
test/unit/api/articles_test.rb
@@ -40,11 +40,12 @@ class ArticlesTest < ActiveSupport::TestCase @@ -40,11 +40,12 @@ class ArticlesTest < ActiveSupport::TestCase
40 end 40 end
41 41
42 should 'list article children' do 42 should 'list article children' do
43 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
44 - child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing")  
45 - child2 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") 43 + article = create(Article, :profile_id => user.person.id, :name => "Parent")
  44 + child1 = create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some Child")
  45 + child2 = create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some Child2")
46 get "/api/v1/articles/#{article.id}/children?#{params.to_query}" 46 get "/api/v1/articles/#{article.id}/children?#{params.to_query}"
47 json = JSON.parse(last_response.body) 47 json = JSON.parse(last_response.body)
  48 +
48 assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] } 49 assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] }
49 end 50 end
50 51
test/unit/api/search_test.rb
@@ -23,6 +23,16 @@ class SearchTest < ActiveSupport::TestCase @@ -23,6 +23,16 @@ class SearchTest < ActiveSupport::TestCase
23 assert_not_empty json['articles'] 23 assert_not_empty json['articles']
24 end 24 end
25 25
  26 + should 'list only articles that has children' do
  27 + article = fast_create(Article, :profile_id => person.id)
  28 + parent = create(Article, :profile_id => person.id, :name => 'parent article')
  29 + child = create(Article, :profile_id => person.id, :parent_id => parent.id, :name => 'child article')
  30 +
  31 + get "/api/v1/search/article?has_children=true"
  32 + json = JSON.parse(last_response.body)
  33 + assert_equal parent.id, json['articles'].first['id']
  34 + end
  35 +
26 should 'invalid search string articles' do 36 should 'invalid search string articles' do
27 fast_create(Article, :profile_id => person.id, :name => 'some article') 37 fast_create(Article, :profile_id => person.id, :name => 'some article')
28 get "/api/v1/search/article?query=test" 38 get "/api/v1/search/article?query=test"