Commit 3ca7d77baabe39919c0d2d58b621a8168dc32bbf

Authored by Carlos Purificação
1 parent 37ffee3b

Fixed search result type

lib/noosfero/api/v1/search.rb
@@ -11,9 +11,10 @@ module Noosfero @@ -11,9 +11,10 @@ module Noosfero
11 # APIHelpers 11 # APIHelpers
12 asset = :articles 12 asset = :articles
13 context = environment 13 context = environment
  14 +
14 profile = environment.profiles.find(params[:profile_id]) if params[:profile_id] 15 profile = environment.profiles.find(params[:profile_id]) if params[:profile_id]
15 16
16 - scope = profile.articles.public || scope = environment.articles.public 17 + scope = profile.nil? ? environment.articles.public : profile.articles.public
17 18
18 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') 19 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article')
19 20
@@ -25,7 +26,8 @@ module Noosfero @@ -25,7 +26,8 @@ module Noosfero
25 options = {:filter => order, :template_id => params[:template_id], :category => category} 26 options = {:filter => order, :template_id => params[:template_id], :category => category}
26 27
27 articles = find_by_contents(asset, context, scope, query, paginate_options, options) 28 articles = find_by_contents(asset, context, scope, query, paginate_options, options)
28 - present articles 29 +
  30 + present articles[:results], :with => Entities::Article
29 end 31 end
30 end 32 end
31 end 33 end
test/unit/api/search_test.rb
@@ -12,7 +12,7 @@ class SearchTest < ActiveSupport::TestCase @@ -12,7 +12,7 @@ class SearchTest < ActiveSupport::TestCase
12 assert !article.published? 12 assert !article.published?
13 get "/api/v1/search/article" 13 get "/api/v1/search/article"
14 json = JSON.parse(last_response.body) 14 json = JSON.parse(last_response.body)
15 - assert_empty json['results'] 15 + assert_empty json['articles']
16 end 16 end
17 17
18 should 'list articles' do 18 should 'list articles' do
@@ -20,7 +20,7 @@ class SearchTest < ActiveSupport::TestCase @@ -20,7 +20,7 @@ class SearchTest < ActiveSupport::TestCase
20 art = create_article_with_optional_category('an article to be found', person) 20 art = create_article_with_optional_category('an article to be found', person)
21 get "/api/v1/search/article" 21 get "/api/v1/search/article"
22 json = JSON.parse(last_response.body) 22 json = JSON.parse(last_response.body)
23 - assert_not_empty json['results'] 23 + assert_not_empty json['articles']
24 end 24 end
25 25
26 should 'invalid search string articles' do 26 should 'invalid search string articles' do
@@ -28,7 +28,7 @@ class SearchTest < ActiveSupport::TestCase @@ -28,7 +28,7 @@ class SearchTest < ActiveSupport::TestCase
28 art = create_article_with_optional_category('an article to be found', person) 28 art = create_article_with_optional_category('an article to be found', person)
29 get "/api/v1/search/article?query=test" 29 get "/api/v1/search/article?query=test"
30 json = JSON.parse(last_response.body) 30 json = JSON.parse(last_response.body)
31 - assert_empty json['results'] 31 + assert_empty json['articles']
32 end 32 end
33 33
34 should 'do not list articles of wrong type' do 34 should 'do not list articles of wrong type' do
@@ -36,7 +36,7 @@ class SearchTest < ActiveSupport::TestCase @@ -36,7 +36,7 @@ class SearchTest < ActiveSupport::TestCase
36 art = create_article_with_optional_category('an article to be found', person) 36 art = create_article_with_optional_category('an article to be found', person)
37 get "/api/v1/search/article?type=TinyMceArticle" 37 get "/api/v1/search/article?type=TinyMceArticle"
38 json = JSON.parse(last_response.body) 38 json = JSON.parse(last_response.body)
39 - assert_empty json['results'] 39 + assert_empty json['articles']
40 end 40 end
41 41
42 should 'list articles of one type' do 42 should 'list articles of one type' do
@@ -45,7 +45,7 @@ class SearchTest < ActiveSupport::TestCase @@ -45,7 +45,7 @@ class SearchTest < ActiveSupport::TestCase
45 article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) 45 article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true)
46 get "/api/v1/search/article?type=TinyMceArticle" 46 get "/api/v1/search/article?type=TinyMceArticle"
47 json = JSON.parse(last_response.body) 47 json = JSON.parse(last_response.body)
48 - assert_equal 1, json['results'].size 48 + assert_equal 1, json['articles'].count
49 end 49 end
50 50
51 should 'list articles of one type and query string' do 51 should 'list articles of one type and query string' do
@@ -55,7 +55,7 @@ class SearchTest < ActiveSupport::TestCase @@ -55,7 +55,7 @@ class SearchTest < ActiveSupport::TestCase
55 article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true) 55 article = fast_create(TinyMceArticle, :profile_id => person.id, :name => "Some thing", :published => true)
56 get "/api/v1/search/article?type=TinyMceArticle&query=thing" 56 get "/api/v1/search/article?type=TinyMceArticle&query=thing"
57 json = JSON.parse(last_response.body) 57 json = JSON.parse(last_response.body)
58 - assert_equal 1, json['results'].size 58 + assert_equal 1, json['articles'].count
59 end 59 end
60 60
61 should 'not return more entries than page limit' do 61 should 'not return more entries than page limit' do
@@ -67,7 +67,7 @@ class SearchTest < ActiveSupport::TestCase @@ -67,7 +67,7 @@ class SearchTest < ActiveSupport::TestCase
67 get "/api/v1/search/article?query=Article&limit=3" 67 get "/api/v1/search/article?query=Article&limit=3"
68 json = JSON.parse(last_response.body) 68 json = JSON.parse(last_response.body)
69 69
70 - assert_equal 3, json['results'].size 70 + assert_equal 3, json['articles'].count
71 end 71 end
72 72
73 should 'return entries second page' do 73 should 'return entries second page' do
@@ -79,7 +79,7 @@ class SearchTest < ActiveSupport::TestCase @@ -79,7 +79,7 @@ class SearchTest < ActiveSupport::TestCase
79 get "/api/v1/search/article?query=Article&limit=3&page=2" 79 get "/api/v1/search/article?query=Article&limit=3&page=2"
80 json = JSON.parse(last_response.body) 80 json = JSON.parse(last_response.body)
81 81
82 - assert_equal 2, json['results'].size 82 + assert_equal 2, json['articles'].count
83 end 83 end
84 84
85 should 'search articles in profile' do 85 should 'search articles in profile' do
@@ -93,6 +93,6 @@ class SearchTest < ActiveSupport::TestCase @@ -93,6 +93,6 @@ class SearchTest < ActiveSupport::TestCase
93 get "/api/v1/search/article?query=Article&profile_id=#{person1.id}" 93 get "/api/v1/search/article?query=Article&profile_id=#{person1.id}"
94 json = JSON.parse(last_response.body) 94 json = JSON.parse(last_response.body)
95 # Only for person1 95 # Only for person1
96 - assert_equal 2, json['results'].size 96 + assert_equal 2, json['articles'].count
97 end 97 end
98 end 98 end
99 \ No newline at end of file 99 \ No newline at end of file