[Git][noosfero/noosfero][master] 2 commits: adding by_role filter on members endpoint

Leandro Nunes gitlab at mg.gitlab.com
Mon Oct 23 15:49:11 BRST 2017


Leandro Nunes pushed to branch master at Noosfero / noosfero


Commits:
1716a2b7 by Leandro Nunes dos Santos at 2017-10-23T13:27:16-03:00
adding by_role filter on members endpoint

- - - - -
c40c1968 by Leandro Nunes at 2017-10-23T17:48:59+00:00
Merge branch 'filter-by-role' into 'master'

adding by_role filter on members endpoint

See merge request noosfero/noosfero!1333
- - - - -


2 changed files:

- app/api/helpers.rb
- test/api/people_test.rb


Changes:

=====================================
app/api/helpers.rb
=====================================
--- a/app/api/helpers.rb
+++ b/app/api/helpers.rb
@@ -296,6 +296,15 @@ module Api
       timestamp
     end
 
+    def by_roles(scope, params)
+      role_ids = params[:roles]
+      if role_ids.nil?
+        scope
+      else
+        scope.by_role(role_ids)
+      end
+    end
+
     def by_reference(scope, params)
       reference_id = params[:reference_id].to_i == 0 ? nil : params[:reference_id].to_i
       if reference_id.nil?
@@ -323,6 +332,7 @@ module Api
       objects = is_a_relation?(method_or_relation) ? method_or_relation : object.send(method_or_relation)
       objects = by_reference(objects, params)
       objects = by_categories(objects, params)
+      objects = by_roles(objects, params)
 
       objects = objects.where(conditions).where(timestamp).reorder(order)
 


=====================================
test/api/people_test.rb
=====================================
--- a/test/api/people_test.rb
+++ b/test/api/people_test.rb
@@ -798,6 +798,25 @@ class PeopleTest < ActiveSupport::TestCase
     assert_equal Api::Status::Friendship::FRIEND, json['code']
   end
 
+  should 'logged user list all admin members of a community' do
+    login_api
+    person1 = fast_create(Person)
+    person2 = fast_create(Person)
+    person3 = fast_create(Person)
+    person4 = fast_create(Person)
+    community = fast_create(Community)
+    community.add_admin(person3)
+    community.add_member(person1)
+    community.add_member(person2)
+    community.add_member(person4)
+    params['roles'] = [Role.find_by(key: "profile_admin").id]
+
+    get "/api/v1/profiles/#{community.id}/members?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_equal 1, json.count
+    assert_equivalent [person3.id], json.map{|p| p["id"]}
+  end
+
   #####
   
   ATTRIBUTES = [:email, :country, :state, :city, :nationality, :formation, :schooling]



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/464f73ab9dec65afda6e34369b1bb177064653ae...c40c1968616fd470476215123a2e87cfefbc5567

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/464f73ab9dec65afda6e34369b1bb177064653ae...c40c1968616fd470476215123a2e87cfefbc5567
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20171023/fb03ebbe/attachment-0001.html>


More information about the Noosfero-dev mailing list