@@ -135,7 +135,7 class MembersController < ApplicationController | |||
|
135 | 135 | end |
|
136 | 136 | |
|
137 | 137 | def autocomplete |
|
138 |
@principals = Principal.active.like(params[:q]). |
|
|
138 | @principals = Principal.active.not_member_of(@project).like(params[:q]).all(:limit => 100) | |
|
139 | 139 | render :layout => false |
|
140 | 140 | end |
|
141 | 141 |
@@ -45,6 +45,7 class Principal < ActiveRecord::Base | |||
|
45 | 45 | |
|
46 | 46 | # Principals that are members of a collection of projects |
|
47 | 47 | named_scope :member_of, lambda {|projects| |
|
48 | projects = [projects] unless projects.is_a?(Array) | |
|
48 | 49 | if projects.empty? |
|
49 | 50 | {:conditions => "1=0"} |
|
50 | 51 | else |
@@ -52,6 +53,16 class Principal < ActiveRecord::Base | |||
|
52 | 53 | {: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]} |
|
53 | 54 | end |
|
54 | 55 | } |
|
56 | # Principals that are not members of projects | |
|
57 | named_scope :not_member_of, lambda {|projects| | |
|
58 | projects = [projects] unless projects.is_a?(Array) | |
|
59 | if projects.empty? | |
|
60 | {:conditions => "1=0"} | |
|
61 | else | |
|
62 | ids = projects.map(&:id) | |
|
63 | {:conditions => ["#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]} | |
|
64 | end | |
|
65 | } | |
|
55 | 66 | |
|
56 | 67 | before_create :set_default_empty_values |
|
57 | 68 |
@@ -55,7 +55,7 | |||
|
55 | 55 | <% end %> |
|
56 | 56 | </div> |
|
57 | 57 | |
|
58 |
<% principals = Principal.active. |
|
|
58 | <% principals = Principal.active.not_member_of(@project).all(:limit => 100, :order => 'type, login, lastname ASC') %> | |
|
59 | 59 | |
|
60 | 60 | <div class="splitcontentright"> |
|
61 | 61 | <% if roles.any? && principals.any? %> |
@@ -18,6 +18,7 | |||
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class PrincipalTest < ActiveSupport::TestCase |
|
21 | fixtures :users, :projects, :members, :member_roles | |
|
21 | 22 | |
|
22 | 23 | def test_active_scope_should_return_groups_and_active_users |
|
23 | 24 | result = Principal.active.all |
@@ -32,6 +33,12 class PrincipalTest < ActiveSupport::TestCase | |||
|
32 | 33 | assert_equal projects.map(&:principals).flatten.sort, Principal.member_of(projects).sort |
|
33 | 34 | end |
|
34 | 35 | |
|
36 | def test_not_member_of_scope_should_return_users_that_have_no_memberships | |
|
37 | projects = Project.find_all_by_id(1, 2) | |
|
38 | expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort | |
|
39 | assert_equal expected, Principal.not_member_of(projects).sort | |
|
40 | end | |
|
41 | ||
|
35 | 42 | context "#like" do |
|
36 | 43 | setup do |
|
37 | 44 | Principal.generate!(:login => 'login') |
General Comments 0
You need to be logged in to leave comments.
Login now