Commit 3f3552397188da7224fcc4962b9bdaee169ce618

Authored by Carlos Purificação
1 parent 3ca7d77b

Fixed pagination. Added fields option to search end point

lib/noosfero/api/helpers.rb
... ... @@ -118,7 +118,11 @@ require 'grape'
118 118  
119 119 def present_articles(asset, method = 'articles')
120 120 articles = find_articles(asset, method)
121   - articles = paginate articles
  121 + present_articles_paginated(articles)
  122 + end
  123 +
  124 + def present_articles_paginated(articles, per_page=nil)
  125 + articles = paginate(articles)
122 126 present articles, :with => Entities::Article, :fields => params[:fields]
123 127 end
124 128  
... ...
lib/noosfero/api/v1/search.rb
... ... @@ -5,6 +5,7 @@ module Noosfero
5 5  
6 6 resource :search do
7 7 resource :article do
  8 + paginate per_page: 20, max_per_page: 200
8 9 get do
9 10 # Security checks
10 11 sanitize_params_hash(params)
... ... @@ -25,9 +26,13 @@ module Noosfero
25 26  
26 27 options = {:filter => order, :template_id => params[:template_id], :category => category}
27 28  
28   - articles = find_by_contents(asset, context, scope, query, paginate_options, options)
  29 + search_result = find_by_contents(asset, context, scope, query, paginate_options, options)
29 30  
30   - present articles[:results], :with => Entities::Article
  31 + articles = search_result[:results]
  32 +
  33 + result = present_articles_paginated(articles)
  34 +
  35 + result
31 36 end
32 37 end
33 38 end
... ...
test/unit/api/search_test.rb
... ... @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/test_helper'
3 3 class SearchTest < ActiveSupport::TestCase
4 4  
5 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)
  6 + fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name)
7 7 end
8 8  
9 9 should 'not list unpublished articles' do
... ... @@ -64,7 +64,7 @@ class SearchTest &lt; ActiveSupport::TestCase
64 64 art = create_article_with_optional_category("Article #{n}", person)
65 65 end
66 66  
67   - get "/api/v1/search/article?query=Article&limit=3"
  67 + get "/api/v1/search/article?query=Article&per_page=3"
68 68 json = JSON.parse(last_response.body)
69 69  
70 70 assert_equal 3, json['articles'].count
... ... @@ -76,7 +76,7 @@ class SearchTest &lt; ActiveSupport::TestCase
76 76 art = create_article_with_optional_category("Article #{n}", person)
77 77 end
78 78  
79   - get "/api/v1/search/article?query=Article&limit=3&page=2"
  79 + get "/api/v1/search/article?query=Article&per_page=3&page=2"
80 80 json = JSON.parse(last_response.body)
81 81  
82 82 assert_equal 2, json['articles'].count
... ... @@ -95,4 +95,13 @@ class SearchTest &lt; ActiveSupport::TestCase
95 95 # Only for person1
96 96 assert_equal 2, json['articles'].count
97 97 end
  98 +
  99 + should 'search with fields' do
  100 + person = fast_create(Person)
  101 + art = create_article_with_optional_category('an article to be found', person)
  102 + get "/api/v1/search/article?fields=title"
  103 + json = JSON.parse(last_response.body)
  104 + assert_not_empty json['articles']
  105 + assert_equal ['title'], json['articles'].first.keys
  106 + end
98 107 end
99 108 \ No newline at end of file
... ...