Commit 2afa1f7500489e990d6549dcd962ec3da702ef89
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'api-search' into 'staging'
Added parent to search Added search option to specify the parent article. See merge request !10
Showing
2 changed files
with
19 additions
and
4 deletions
Show diff stats
lib/noosfero/api/v1/search.rb
@@ -18,6 +18,8 @@ module Noosfero | @@ -18,6 +18,8 @@ module Noosfero | ||
18 | scope = profile.nil? ? environment.articles.public : profile.articles.public | 18 | scope = profile.nil? ? environment.articles.public : profile.articles.public |
19 | 19 | ||
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 | + | ||
22 | + scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? | ||
21 | 23 | ||
22 | category = params[:category] || "" | 24 | category = params[:category] || "" |
23 | 25 |
test/unit/api/search_test.rb
@@ -2,8 +2,8 @@ require File.dirname(__FILE__) + '/test_helper' | @@ -2,8 +2,8 @@ require File.dirname(__FILE__) + '/test_helper' | ||
2 | 2 | ||
3 | class SearchTest < ActiveSupport::TestCase | 3 | class SearchTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | - def create_article_with_optional_category(name, profile, category = nil) | ||
6 | - fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name) | 5 | + def create_article_with_optional_category(name, profile, category = nil, parent = nil) |
6 | + fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name, :parent => parent) | ||
7 | end | 7 | end |
8 | 8 | ||
9 | should 'not list unpublished articles' do | 9 | should 'not list unpublished articles' do |
@@ -97,11 +97,24 @@ class SearchTest < ActiveSupport::TestCase | @@ -97,11 +97,24 @@ class SearchTest < ActiveSupport::TestCase | ||
97 | end | 97 | end |
98 | 98 | ||
99 | should 'search with fields' do | 99 | should 'search with fields' do |
100 | - person = fast_create(Person) | 100 | + person = fast_create(Person) |
101 | art = create_article_with_optional_category('an article to be found', person) | 101 | art = create_article_with_optional_category('an article to be found', person) |
102 | get "/api/v1/search/article?fields=title" | 102 | get "/api/v1/search/article?fields=title" |
103 | json = JSON.parse(last_response.body) | 103 | json = JSON.parse(last_response.body) |
104 | assert_not_empty json['articles'] | 104 | assert_not_empty json['articles'] |
105 | assert_equal ['title'], json['articles'].first.keys | 105 | assert_equal ['title'], json['articles'].first.keys |
106 | end | 106 | end |
107 | -end | ||
108 | \ No newline at end of file | 107 | \ No newline at end of file |
108 | + | ||
109 | + should 'search with parent' do | ||
110 | + person = fast_create(Person) | ||
111 | + parent = fast_create(Folder, :profile_id => person.id, :name => "parent", :published => true) | ||
112 | + art = fast_create(Article, :profile_id => person.id, :name => "child", :parent_id => parent.id) | ||
113 | + art2 = fast_create(Article, :profile_id => person.id, :name => "child2") | ||
114 | + get "/api/v1/search/article?parent_id=#{parent.id}" | ||
115 | + json = JSON.parse(last_response.body) | ||
116 | + assert_not_empty json['articles'] | ||
117 | + assert_equal art.id, json['articles'].first["id"] | ||
118 | + assert_equal 1, json['articles'].count | ||
119 | + end | ||
120 | + | ||
121 | +end |