Commit 57e3afc186238eca67223797c0fc2a84adda5567

Authored by Carlos Purificação
1 parent ea5cb22f

Added parent to search

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]) if params[:parent].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 &lt; ActiveSupport::TestCase @@ -97,11 +97,24 @@ class SearchTest &lt; 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 +
  108 + should 'search with parent' do
  109 + person = fast_create(Person)
  110 + parent = fast_create(Folder, :profile_id => person.id, :name => "parent", :published => true)
  111 + art = fast_create(Article, :profile_id => person.id, :name => "child", :parent_id => parent.id)
  112 + art2 = fast_create(Article, :profile_id => person.id, :name => "child2")
  113 + get "/api/v1/search/article?parent=#{parent.id}"
  114 + json = JSON.parse(last_response.body)
  115 + assert_not_empty json['articles']
  116 + assert_equal art.id, json['articles'].first["id"]
  117 + assert_equal 1, json['articles'].count
  118 + end
  119 +
107 end 120 end
108 \ No newline at end of file 121 \ No newline at end of file