[Git][noosfero/noosfero][api] api: only search visible organization in current model

Larissa Reis gitlab at gitlab.com
Sat Jun 20 00:59:38 BRT 2015


Larissa Reis pushed to branch api at Noosfero / noosfero


Commits:
c366e6ec by Larissa Reis at 2015-06-20T00:36:27Z
api: only search visible organization in current model

  scopes are defined as class methods on the singleton of the class they
  were named instead of the class that calls them, so regardless if they
  are called by Enterprise or Community, they were defined on
  Organization, so #visible_for_person() always gives us
    `WHERE "profiles"."type" IN ('Organization', 'Enterprise', 'Community')`

  This wouldn't be a problem if rails 3.2 didn't merge the WHERE clauses
  of nested scopes, which causes the previous filtering by type of
  Enterprise/Community to be thrown away in favor of the broader one.

  This was fixed in later rails versions but we want to be able to
  search only for Enterprises or Communities, so I translated the scope
  to a class method on the parent class Organization, so it can be
  inherited by the other.

- - - - -


3 changed files:

- app/models/organization.rb
- test/unit/api/communities_test.rb
- test/unit/api/enterprises_test.rb


Changes:

=====================================
app/models/organization.rb
=====================================
--- a/app/models/organization.rb
+++ b/app/models/organization.rb
@@ -16,7 +16,7 @@ class Organization < Profile
   #   visible.
   #   4) The user is not a member of the organization but the organization is
   #   visible, public and enabled.
-  scope :visible_for_person, lambda { |person|
+  def self.visible_for_person(person)
     joins('LEFT JOIN "role_assignments" ON ("role_assignments"."resource_id" = "profiles"."id"
           AND "role_assignments"."resource_type" = \'Profile\') OR (
           "role_assignments"."resource_id" = "profiles"."environment_id" AND
@@ -31,7 +31,7 @@ class Organization < Profile
       'profile_admin', 'environment_administrator', Profile.name, person.id,
       Profile.name, person.id,  true, true, true]
     ).uniq
-  }
+  end
 
   settings_items :closed, :type => :boolean, :default => false
   def closed?


=====================================
test/unit/api/communities_test.rb
=====================================
--- a/test/unit/api/communities_test.rb
+++ b/test/unit/api/communities_test.rb
@@ -7,6 +7,15 @@ class CommunitiesTest < ActiveSupport::TestCase
     login_api
   end
 
+  should 'list only communities' do
+    community = fast_create(Community)
+    enterprise = fast_create(Enterprise) # should not list this enterprise
+    get "/api/v1/communities?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_not_includes json['communities'].map {|c| c['id']}, enterprise.id
+    assert_includes json['communities'].map {|c| c['id']}, community.id
+  end
+
   should 'list all communities' do
     community1 = fast_create(Community, :public_profile => true)
     community2 = fast_create(Community)


=====================================
test/unit/api/enterprises_test.rb
=====================================
--- a/test/unit/api/enterprises_test.rb
+++ b/test/unit/api/enterprises_test.rb
@@ -7,6 +7,15 @@ class EnterprisesTest < ActiveSupport::TestCase
     login_api
   end
 
+  should 'list only enterprises' do
+    community = fast_create(Community) # should not list this community
+    enterprise = fast_create(Enterprise, :public_profile => true)
+    get "/api/v1/enterprises?#{params.to_query}"
+    json = JSON.parse(last_response.body)
+    assert_includes json['enterprises'].map {|c| c['id']}, enterprise.id
+    assert_not_includes json['enterprises'].map {|c| c['id']}, community.id
+  end
+
   should 'list all enterprises' do
     enterprise1 = fast_create(Enterprise, :public_profile => true)
     enterprise2 = fast_create(Enterprise)



View it on GitLab: https://gitlab.com/noosfero/noosfero/commit/c366e6ecb18b9aa7add7bc3f2a3a93051babef28
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150620/a0cee3ca/attachment-0001.html>


More information about the Noosfero-dev mailing list