Commit 1217170da52e81246c6d42b4cbd6de5fa55ce7ec

Authored by Daniela Feitosa
1 parent 2c68eefb

Fixed problems with members_of on postgres

Reverted Profile.members_of definition
Added a members_count method

(ActionItem1400)
app/models/person.rb
@@ -4,7 +4,7 @@ class Person < Profile @@ -4,7 +4,7 @@ class Person < Profile
4 acts_as_trackable :after_add => Proc.new {|p,t| notify_activity(t)} 4 acts_as_trackable :after_add => Proc.new {|p,t| notify_activity(t)}
5 acts_as_accessor 5 acts_as_accessor
6 6
7 - named_scope :members_of, lambda { |resource| { :select => 'DISTINCT profiles.*', :include => :role_assignments, :group => 'profiles.id', :conditions => ['role_assignments.resource_type = ? AND role_assignments.resource_id = ?', resource.class.base_class.name, resource.id ] } } 7 + named_scope :members_of, lambda { |resource| { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => ['role_assignments.resource_type = ? AND role_assignments.resource_id = ?', resource.class.base_class.name, resource.id ] } }
8 8
9 def memberships 9 def memberships
10 Profile.memberships_of(self) 10 Profile.memberships_of(self)
app/models/profile.rb
@@ -59,6 +59,10 @@ class Profile < ActiveRecord::Base @@ -59,6 +59,10 @@ class Profile < ActiveRecord::Base
59 Person.members_of(self) 59 Person.members_of(self)
60 end 60 end
61 61
  62 + def members_count
  63 + members.count('DISTINCT(profiles.id)')
  64 + end
  65 +
62 def members_by_role(role) 66 def members_by_role(role)
63 Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', role.id]) 67 Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', role.id])
64 end 68 end
@@ -563,10 +567,10 @@ private :generate_url, :url_options @@ -563,10 +567,10 @@ private :generate_url, :url_options
563 # Adds a person as member of this Profile. 567 # Adds a person as member of this Profile.
564 def add_member(person) 568 def add_member(person)
565 if self.has_members? 569 if self.has_members?
566 - if self.closed? && members.count > 0 570 + if self.closed? && members_count > 0
567 AddMember.create!(:person => person, :organization => self) unless self.already_request_membership?(person) 571 AddMember.create!(:person => person, :organization => self) unless self.already_request_membership?(person)
568 else 572 else
569 - if members.count == 0 573 + if members_count == 0
570 self.affiliate(person, Profile::Roles.admin(environment.id)) 574 self.affiliate(person, Profile::Roles.admin(environment.id))
571 end 575 end
572 self.affiliate(person, Profile::Roles.member(environment.id)) 576 self.affiliate(person, Profile::Roles.member(environment.id))
@@ -783,7 +787,7 @@ private :generate_url, :url_options @@ -783,7 +787,7 @@ private :generate_url, :url_options
783 end 787 end
784 788
785 def more_popular_label 789 def more_popular_label
786 - amount = self.members.count 790 + amount = self.members_count
787 { 791 {
788 0 => _('none'), 792 0 => _('none'),
789 1 => _('one member') 793 1 => _('one member')
app/views/profile/_organization_profile.rhtml
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 <tr> 6 <tr>
7 <td class='field-name'><%= _('Members') %></td> 7 <td class='field-name'><%= _('Members') %></td>
8 <td> 8 <td>
9 - <%= link_to profile.members.count, :controller => 'profile', :action => 'members' %> 9 + <%= link_to profile.members_count, :controller => 'profile', :action => 'members' %>
10 </td> 10 </td>
11 </tr> 11 </tr>
12 12
app/views/profile_members/last_admin.rhtml
1 <h1><%= _('Last administrator leaving %s') % profile.name %></h1> 1 <h1><%= _('Last administrator leaving %s') % profile.name %></h1>
2 2
3 -<% if profile.members.count > 1 %> 3 +<% if profile.members_count > 1 %>
4 <div id='last-admin-message'> 4 <div id='last-admin-message'>
5 <%= _('Since you are the last administrator, you must choose at least one member to administer this community.') % profile.name %> 5 <%= _('Since you are the last administrator, you must choose at least one member to administer this community.') % profile.name %>
6 </div> 6 </div>
features/last_administrator_leaving.feature
@@ -11,13 +11,22 @@ Feature: remove administrator role @@ -11,13 +11,22 @@ Feature: remove administrator role
11 And the following community 11 And the following community
12 | name | identifier | 12 | name | identifier |
13 | Nice people | nice-people | 13 | Nice people | nice-people |
14 - And "Joao Silva" is admin of "Nice people" 14 + And "Joao Silva" is a member of "Nice people"
15 And I am logged in as "joaosilva" 15 And I am logged in as "joaosilva"
16 16
  17 + Scenario: the last administrator removes his administrator role and must choose the new administrator
  18 + Given "Maria Souza" is a member of "Nice people"
  19 + And I am on Nice people's members management
  20 + And I follow "Edit"
  21 + And I uncheck "Profile Administrator"
  22 + When I press "Save changes"
  23 + Then I should see "Since you are the last administrator, you must choose"
  24 +
17 Scenario: the last administrator and member removes his administrator role and the next member to join becomes the new administrator 25 Scenario: the last administrator and member removes his administrator role and the next member to join becomes the new administrator
18 Given I am on Nice people's members management 26 Given I am on Nice people's members management
19 And I follow "Edit" 27 And I follow "Edit"
20 And I uncheck "Profile Administrator" 28 And I uncheck "Profile Administrator"
  29 + And I uncheck "Profile Member"
21 When I press "Save changes" 30 When I press "Save changes"
22 Then I should see "Since you are the last administrator and there is no other member in this community" 31 Then I should see "Since you are the last administrator and there is no other member in this community"
23 And I press "Ok, I want to leave" 32 And I press "Ok, I want to leave"
@@ -30,6 +39,7 @@ Feature: remove administrator role @@ -30,6 +39,7 @@ Feature: remove administrator role
30 And I am on Nice people's members management 39 And I am on Nice people's members management
31 And I follow "Edit" 40 And I follow "Edit"
32 And I uncheck "Profile Administrator" 41 And I uncheck "Profile Administrator"
  42 + And I uncheck "Profile Member"
33 When I press "Save changes" 43 When I press "Save changes"
34 Then I should see "Since you are the last administrator and there is no other member in this community" 44 Then I should see "Since you are the last administrator and there is no other member in this community"
35 And I press "Ok, I want to leave" 45 And I press "Ok, I want to leave"
test/unit/person_test.rb
@@ -1143,6 +1143,6 @@ class PersonTest &lt; Test::Unit::TestCase @@ -1143,6 +1143,6 @@ class PersonTest &lt; Test::Unit::TestCase
1143 community.add_member(person) 1143 community.add_member(person)
1144 1144
1145 assert_equal [person], Person.members_of(community) 1145 assert_equal [person], Person.members_of(community)
1146 - assert_equal 1, Person.members_of(community).count  
1147 end 1146 end
  1147 +
1148 end 1148 end
test/unit/profile_test.rb
@@ -1743,7 +1743,7 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -1743,7 +1743,7 @@ class ProfileTest &lt; Test::Unit::TestCase
1743 1743
1744 should "return none on label if the profile hasn't members" do 1744 should "return none on label if the profile hasn't members" do
1745 p = fast_create(Profile) 1745 p = fast_create(Profile)
1746 - assert_equal 0, p.members.count 1746 + assert_equal 0, p.members_count
1747 assert_equal "none", p.more_popular_label 1747 assert_equal "none", p.more_popular_label
1748 end 1748 end
1749 1749
@@ -1817,6 +1817,22 @@ class ProfileTest &lt; Test::Unit::TestCase @@ -1817,6 +1817,22 @@ class ProfileTest &lt; Test::Unit::TestCase
1817 assert_equal 3, p.forums.count 1817 assert_equal 3, p.forums.count
1818 end 1818 end
1819 1819
  1820 + should 'return unique members of a community' do
  1821 + person = fast_create(Person)
  1822 + community = fast_create(Community)
  1823 + community.add_member(person)
  1824 +
  1825 + assert_equal [person], community.members
  1826 + end
  1827 +
  1828 + should 'count unique members of a community' do
  1829 + person = fast_create(Person)
  1830 + community = fast_create(Community)
  1831 + community.add_member(person)
  1832 +
  1833 + assert_equal 1, community.members_count
  1834 + end
  1835 +
1820 private 1836 private
1821 1837
1822 def assert_invalid_identifier(id) 1838 def assert_invalid_identifier(id)