diff --git a/app/models/person.rb b/app/models/person.rb
index 9cb73c8..3a2f97f 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -4,7 +4,7 @@ class Person < Profile
acts_as_trackable :after_add => Proc.new {|p,t| notify_activity(t)}
acts_as_accessor
- 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 ] } }
+ 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 ] } }
def memberships
Profile.memberships_of(self)
diff --git a/app/models/profile.rb b/app/models/profile.rb
index aeb8c18..76c5b76 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -59,6 +59,10 @@ class Profile < ActiveRecord::Base
Person.members_of(self)
end
+ def members_count
+ members.count('DISTINCT(profiles.id)')
+ end
+
def members_by_role(role)
Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', role.id])
end
@@ -563,10 +567,10 @@ private :generate_url, :url_options
# Adds a person as member of this Profile.
def add_member(person)
if self.has_members?
- if self.closed? && members.count > 0
+ if self.closed? && members_count > 0
AddMember.create!(:person => person, :organization => self) unless self.already_request_membership?(person)
else
- if members.count == 0
+ if members_count == 0
self.affiliate(person, Profile::Roles.admin(environment.id))
end
self.affiliate(person, Profile::Roles.member(environment.id))
@@ -783,7 +787,7 @@ private :generate_url, :url_options
end
def more_popular_label
- amount = self.members.count
+ amount = self.members_count
{
0 => _('none'),
1 => _('one member')
diff --git a/app/views/profile/_organization_profile.rhtml b/app/views/profile/_organization_profile.rhtml
index ca4c386..69cf8cb 100644
--- a/app/views/profile/_organization_profile.rhtml
+++ b/app/views/profile/_organization_profile.rhtml
@@ -6,7 +6,7 @@
<%= _('Members') %> |
- <%= link_to profile.members.count, :controller => 'profile', :action => 'members' %>
+ <%= link_to profile.members_count, :controller => 'profile', :action => 'members' %>
|
diff --git a/app/views/profile_members/last_admin.rhtml b/app/views/profile_members/last_admin.rhtml
index 270225a..476c123 100644
--- a/app/views/profile_members/last_admin.rhtml
+++ b/app/views/profile_members/last_admin.rhtml
@@ -1,6 +1,6 @@
<%= _('Last administrator leaving %s') % profile.name %>
-<% if profile.members.count > 1 %>
+<% if profile.members_count > 1 %>
<%= _('Since you are the last administrator, you must choose at least one member to administer this community.') % profile.name %>
diff --git a/features/last_administrator_leaving.feature b/features/last_administrator_leaving.feature
index 83debd1..350a344 100644
--- a/features/last_administrator_leaving.feature
+++ b/features/last_administrator_leaving.feature
@@ -11,13 +11,22 @@ Feature: remove administrator role
And the following community
| name | identifier |
| Nice people | nice-people |
- And "Joao Silva" is admin of "Nice people"
+ And "Joao Silva" is a member of "Nice people"
And I am logged in as "joaosilva"
+ Scenario: the last administrator removes his administrator role and must choose the new administrator
+ Given "Maria Souza" is a member of "Nice people"
+ And I am on Nice people's members management
+ And I follow "Edit"
+ And I uncheck "Profile Administrator"
+ When I press "Save changes"
+ Then I should see "Since you are the last administrator, you must choose"
+
Scenario: the last administrator and member removes his administrator role and the next member to join becomes the new administrator
Given I am on Nice people's members management
And I follow "Edit"
And I uncheck "Profile Administrator"
+ And I uncheck "Profile Member"
When I press "Save changes"
Then I should see "Since you are the last administrator and there is no other member in this community"
And I press "Ok, I want to leave"
@@ -30,6 +39,7 @@ Feature: remove administrator role
And I am on Nice people's members management
And I follow "Edit"
And I uncheck "Profile Administrator"
+ And I uncheck "Profile Member"
When I press "Save changes"
Then I should see "Since you are the last administrator and there is no other member in this community"
And I press "Ok, I want to leave"
diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb
index a798764..5853a8d 100644
--- a/test/unit/person_test.rb
+++ b/test/unit/person_test.rb
@@ -1143,6 +1143,6 @@ class PersonTest < Test::Unit::TestCase
community.add_member(person)
assert_equal [person], Person.members_of(community)
- assert_equal 1, Person.members_of(community).count
end
+
end
diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb
index cb67532..7224683 100644
--- a/test/unit/profile_test.rb
+++ b/test/unit/profile_test.rb
@@ -1743,7 +1743,7 @@ class ProfileTest < Test::Unit::TestCase
should "return none on label if the profile hasn't members" do
p = fast_create(Profile)
- assert_equal 0, p.members.count
+ assert_equal 0, p.members_count
assert_equal "none", p.more_popular_label
end
@@ -1817,6 +1817,22 @@ class ProfileTest < Test::Unit::TestCase
assert_equal 3, p.forums.count
end
+ should 'return unique members of a community' do
+ person = fast_create(Person)
+ community = fast_create(Community)
+ community.add_member(person)
+
+ assert_equal [person], community.members
+ end
+
+ should 'count unique members of a community' do
+ person = fast_create(Person)
+ community = fast_create(Community)
+ community.add_member(person)
+
+ assert_equal 1, community.members_count
+ end
+
private
def assert_invalid_identifier(id)
--
libgit2 0.21.2