diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index 80328aa7f..11cbeff32 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -135,7 +135,7 @@ class MembersController < ApplicationController end def autocomplete - @principals = Principal.active.like(params[:q]).find(:all, :limit => 100) - @project.principals + @principals = Principal.active.not_member_of(@project).like(params[:q]).all(:limit => 100) render :layout => false end diff --git a/app/models/principal.rb b/app/models/principal.rb index 9955501e3..de6539986 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -45,6 +45,7 @@ class Principal < ActiveRecord::Base # Principals that are members of a collection of projects named_scope :member_of, lambda {|projects| + projects = [projects] unless projects.is_a?(Array) if projects.empty? {:conditions => "1=0"} else @@ -52,6 +53,16 @@ class Principal < ActiveRecord::Base {:conditions => ["#{Principal.table_name}.status = 1 AND #{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]} end } + # Principals that are not members of projects + named_scope :not_member_of, lambda {|projects| + projects = [projects] unless projects.is_a?(Array) + if projects.empty? + {:conditions => "1=0"} + else + ids = projects.map(&:id) + {:conditions => ["#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]} + end + } before_create :set_default_empty_values diff --git a/app/views/projects/settings/_members.html.erb b/app/views/projects/settings/_members.html.erb index 2710fa82a..b5086d836 100644 --- a/app/views/projects/settings/_members.html.erb +++ b/app/views/projects/settings/_members.html.erb @@ -55,7 +55,7 @@ <% end %> -<% principals = Principal.active.find(:all, :limit => 100, :order => 'type, login, lastname ASC') - @project.principals %> +<% principals = Principal.active.not_member_of(@project).all(:limit => 100, :order => 'type, login, lastname ASC') %>
<% if roles.any? && principals.any? %> diff --git a/test/unit/principal_test.rb b/test/unit/principal_test.rb index c33286208..f51527b0a 100644 --- a/test/unit/principal_test.rb +++ b/test/unit/principal_test.rb @@ -18,6 +18,7 @@ require File.expand_path('../../test_helper', __FILE__) class PrincipalTest < ActiveSupport::TestCase + fixtures :users, :projects, :members, :member_roles def test_active_scope_should_return_groups_and_active_users result = Principal.active.all @@ -32,6 +33,12 @@ class PrincipalTest < ActiveSupport::TestCase assert_equal projects.map(&:principals).flatten.sort, Principal.member_of(projects).sort end + def test_not_member_of_scope_should_return_users_that_have_no_memberships + projects = Project.find_all_by_id(1, 2) + expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort + assert_equal expected, Principal.not_member_of(projects).sort + end + context "#like" do setup do Principal.generate!(:login => 'login')