[Git][noosfero/noosfero][master] 3 commits: api: use search engine when filtering a collection

Leandro Nunes gitlab at mg.gitlab.com
Mon Mar 27 15:20:43 BRT 2017


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
d169d3be by Victor Costa at 2017-03-27T13:52:58-03:00
api: use search engine when filtering a collection

- - - - -
9e959ff7 by Victor Costa at 2017-03-27T14:00:11-03:00
api: deprecate search endpoint

- - - - -
1ac744df by Leandro Nunes at 2017-03-27T18:20:33+00:00
Merge branch 'api-search' into 'master'

api: use search engine when filtering a collection

See merge request !1153
- - - - -


6 changed files:

- app/api/helpers.rb
- app/api/v1/search.rb
- test/api/articles_test.rb
- test/api/communities_test.rb
- test/api/people_test.rb
- test/api/search_test.rb


Changes:

=====================================
app/api/helpers.rb
=====================================
--- a/app/api/helpers.rb
+++ b/app/api/helpers.rb
@@ -321,6 +321,11 @@ module Api
 
       objects = objects.where(conditions).where(timestamp).reorder(order)
 
+      if params[:search].present?
+        asset = objects.model.name.underscore.pluralize
+        objects = find_by_contents(asset, object, objects, params[:search])[:results]
+      end
+
       params[:page] ||= 1
       params[:per_page] ||= limit
       paginate(objects)


=====================================
app/api/v1/search.rb
=====================================
--- a/app/api/v1/search.rb
+++ b/app/api/v1/search.rb
@@ -26,7 +26,7 @@ module Api
             search_result = find_by_contents(asset, context, scope, query, {:page => 1}, options)
 
             articles = search_result[:results]
-
+            status Api::Status::DEPRECATED
             present_articles(articles)
           end
         end


=====================================
test/api/articles_test.rb
=====================================
--- a/test/api/articles_test.rb
+++ b/test/api/articles_test.rb
@@ -843,4 +843,22 @@ class ArticlesTest < ActiveSupport::TestCase
     assert_not_includes json.first.keys, 'title'
   end
 
+  should 'search for articles' do
+    article1 = fast_create(Article, profile_id: user.person.id, name: "Some thing")
+    article2 = fast_create(Article, profile_id: user.person.id, name: "Other thing")
+    params[:search] = 'some'
+    get "/api/v1/articles/?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal [article1.id], json.map { |a| a["id"] }
+  end
+
+  should 'search for articles of different types' do
+    article1 = fast_create(Event, profile_id: user.person.id, name: "Some thing")
+    article2 = fast_create(TextArticle, profile_id: user.person.id, name: "Some other thing")
+    article3 = fast_create(Article, profile_id: user.person.id, name: "Other thing")
+    params[:search] = 'some'
+    get "/api/v1/articles/?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equivalent [article1.id, article2.id], json.map { |a| a["id"] }
+  end
 end


=====================================
test/api/communities_test.rb
=====================================
--- a/test/api/communities_test.rb
+++ b/test/api/communities_test.rb
@@ -378,4 +378,12 @@ class CommunitiesTest < ActiveSupport::TestCase
     assert_not_nil json['members']
   end
 
+  should 'search for communities' do
+    community1 = fast_create(Community)
+    community2 = fast_create(Community, name: 'Rails Community')
+    params[:search] = 'rails'
+    get "/api/v1/communities?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal [community2.id], json.map {|c| c['id']}
+  end
 end


=====================================
test/api/people_test.rb
=====================================
--- a/test/api/people_test.rb
+++ b/test/api/people_test.rb
@@ -662,7 +662,26 @@ class PeopleTest < ActiveSupport::TestCase
     json = JSON.parse(last_response.body)
     assert_equal json['id'], person.id
   end  
-  
+
+  should 'search for people' do
+    person1 = fast_create(Person, :public_profile => true)
+    person2 = fast_create(Person, name: 'John Snow')
+    params[:search] = 'john'
+    get "/api/v1/people?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal [person2.id], json.map {|c| c['id']}
+  end
+
+  should 'search for people with pagination' do
+    5.times { fast_create(Person, name: 'John Snow') }
+    params[:search] = 'john'
+    params[:per_page] = 2
+    get "/api/v1/people?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 2, json.length
+    assert_equal 5, last_response.headers['Total'].to_i
+  end
+
   #####
   
   ATTRIBUTES = [:email, :country, :state, :city, :nationality, :formation, :schooling]


=====================================
test/api/search_test.rb
=====================================
--- a/test/api/search_test.rb
+++ b/test/api/search_test.rb
@@ -155,4 +155,8 @@ class SearchTest < ActiveSupport::TestCase
     assert_equal [article2.id], json.map {|a| a['id']}
   end
 
+  should 'list articles in search endpoint be deprecated' do
+    get "/api/v1/search/article"
+    assert_equal Api::Status::DEPRECATED, last_response.status
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/ebdcd0b234e2ddd6896ac1140ec61ebcc88149ff...1ac744dfa7cf2c6d1e874df32590579405144a47
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170327/408e9f37/attachment-0001.html>


More information about the Noosfero-dev mailing list