Commit 37ffee3bd22abe0d4ccffa1562103ed2b36aefd2

Authored by Carlos Purificação
1 parent 789695b2

Added search by profile

lib/noosfero/api/v1/search.rb
... ... @@ -11,11 +11,14 @@ module Noosfero
11 11 # APIHelpers
12 12 asset = :articles
13 13 context = environment
14   - scope = environment.articles.public
  14 + profile = environment.profiles.find(params[:profile_id]) if params[:profile_id]
  15 +
  16 + scope = profile.articles.public || scope = environment.articles.public
15 17  
16 18 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article')
17 19  
18 20 category = params[:category] || ""
  21 +
19 22 query = params[:query] || ""
20 23 order = "more_recent"
21 24  
... ...
test/unit/api/search_test.rb
... ... @@ -67,7 +67,6 @@ class SearchTest < ActiveSupport::TestCase
67 67 get "/api/v1/search/article?query=Article&limit=3"
68 68 json = JSON.parse(last_response.body)
69 69  
70   -
71 70 assert_equal 3, json['results'].size
72 71 end
73 72  
... ... @@ -80,8 +79,20 @@ class SearchTest < ActiveSupport::TestCase
80 79 get "/api/v1/search/article?query=Article&limit=3&page=2"
81 80 json = JSON.parse(last_response.body)
82 81  
83   -
84 82 assert_equal 2, json['results'].size
85 83 end
86 84  
  85 + should 'search articles in profile' do
  86 + person1 = fast_create(Person)
  87 + person2 = fast_create(Person)
  88 +
  89 + art1 = create_article_with_optional_category("Article 1 for profile #{person1.id}", person1)
  90 + art2 = create_article_with_optional_category("Article for profile #{person2.id}", person2)
  91 + art3 = create_article_with_optional_category("Article 2 for profile #{person1.id}", person1)
  92 +
  93 + get "/api/v1/search/article?query=Article&profile_id=#{person1.id}"
  94 + json = JSON.parse(last_response.body)
  95 + # Only for person1
  96 + assert_equal 2, json['results'].size
  97 + end
87 98 end
88 99 \ No newline at end of file
... ...