From 3ca7d77baabe39919c0d2d58b621a8168dc32bbf Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Thu, 3 Sep 2015 12:24:50 -0300 Subject: [PATCH] Fixed search result type --- lib/noosfero/api/v1/search.rb | 6 ++++-- test/unit/api/search_test.rb | 18 +++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/noosfero/api/v1/search.rb b/lib/noosfero/api/v1/search.rb index 84429fe..47e8b53 100644 --- a/lib/noosfero/api/v1/search.rb +++ b/lib/noosfero/api/v1/search.rb @@ -11,9 +11,10 @@ module Noosfero # APIHelpers asset = :articles context = environment + profile = environment.profiles.find(params[:profile_id]) if params[:profile_id] - scope = profile.articles.public || scope = environment.articles.public + scope = profile.nil? ? environment.articles.public : profile.articles.public scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') @@ -25,7 +26,8 @@ module Noosfero options = {:filter => order, :template_id => params[:template_id], :category => category} articles = find_by_contents(asset, context, scope, query, paginate_options, options) - present articles + + present articles[:results], :with => Entities::Article end end end diff --git a/test/unit/api/search_test.rb b/test/unit/api/search_test.rb index f965f12..3c13753 100644 --- a/test/unit/api/search_test.rb +++ b/test/unit/api/search_test.rb @@ -12,7 +12,7 @@ class SearchTest < ActiveSupport::TestCase assert !article.published? get "/api/v1/search/article" json = JSON.parse(last_response.body) - assert_empty json['results'] + assert_empty json['articles'] end should 'list articles' do @@ -20,7 +20,7 @@ class SearchTest < ActiveSupport::TestCase art = create_article_with_optional_category('an article to be found', person) get "/api/v1/search/article" json = JSON.parse(last_response.body) - assert_not_empty json['results'] + assert_not_empty json['articles'] end should 'invalid search string articles' do @@ -28,7 +28,7 @@ class SearchTest < ActiveSupport::TestCase art = create_article_with_optional_category('an article to be found', person) get "/api/v1/search/article?query=test" json = JSON.parse(last_response.body) - assert_empty json['results'] + assert_empty json['articles'] end should 'do not list articles of wrong type' do @@ -36,7 +36,7 @@ class SearchTest < ActiveSupport::TestCase art = create_article_with_optional_category('an article to be found', person) get "/api/v1/search/article?type=TinyMceArticle" json = JSON.parse(last_response.body) - assert_empty json['results'] + assert_empty json['articles'] end should 'list articles of one type' do @@ -45,7 +45,7 @@ class SearchTest < ActiveSupport::TestCase article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) get "/api/v1/search/article?type=TinyMceArticle" json = JSON.parse(last_response.body) - assert_equal 1, json['results'].size + assert_equal 1, json['articles'].count end should 'list articles of one type and query string' do @@ -55,7 +55,7 @@ class SearchTest < ActiveSupport::TestCase article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) get "/api/v1/search/article?type=TinyMceArticle&query=thing" json = JSON.parse(last_response.body) - assert_equal 1, json['results'].size + assert_equal 1, json['articles'].count end should 'not return more entries than page limit' do @@ -67,7 +67,7 @@ class SearchTest < ActiveSupport::TestCase get "/api/v1/search/article?query=Article&limit=3" json = JSON.parse(last_response.body) - assert_equal 3, json['results'].size + assert_equal 3, json['articles'].count end should 'return entries second page' do @@ -79,7 +79,7 @@ class SearchTest < ActiveSupport::TestCase get "/api/v1/search/article?query=Article&limit=3&page=2" json = JSON.parse(last_response.body) - assert_equal 2, json['results'].size + assert_equal 2, json['articles'].count end should 'search articles in profile' do @@ -93,6 +93,6 @@ class SearchTest < ActiveSupport::TestCase get "/api/v1/search/article?query=Article&profile_id=#{person1.id}" json = JSON.parse(last_response.body) # Only for person1 - assert_equal 2, json['results'].size + assert_equal 2, json['articles'].count end end \ No newline at end of file -- libgit2 0.21.2