From b972b5a647cae4cef4dfeb9686cb529705886107 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 1 Apr 2011 16:47:30 +0000 Subject: [PATCH] Fixed: list of users for adding to a group may be empty if 100 first users have been added (#8029). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5284 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/groups_controller.rb | 2 +- app/models/user.rb | 4 ++++ app/views/groups/_users.html.erb | 2 +- test/functional/groups_controller_test.rb | 9 +++++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index 29e4e4b07..db56eff96 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -132,7 +132,7 @@ class GroupsController < ApplicationController def autocomplete_for_user @group = Group.find(params[:id]) - @users = User.active.like(params[:q]).find(:all, :limit => 100) - @group.users + @users = User.active.not_in_group(@group).like(params[:q]).all(:limit => 100) render :layout => false end diff --git a/app/models/user.rb b/app/models/user.rb index a5ec74a35..1018c33e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -80,6 +80,10 @@ class User < Principal group_id = group.is_a?(Group) ? group.id : group.to_i { :conditions => ["#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] } } + named_scope :not_in_group, lambda {|group| + group_id = group.is_a?(Group) ? group.id : group.to_i + { :conditions => ["#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] } + } def before_create self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? diff --git a/app/views/groups/_users.html.erb b/app/views/groups/_users.html.erb index 580851d5a..d7c462e1b 100644 --- a/app/views/groups/_users.html.erb +++ b/app/views/groups/_users.html.erb @@ -24,7 +24,7 @@
-<% users = User.active.find(:all, :limit => 100) - @group.users %> +<% users = User.active.not_in_group(@group).all(:limit => 100) %> <% if users.any? %> <% remote_form_for(:group, @group, :url => {:controller => 'groups', :action => 'add_users', :id => @group}, :method => :post) do |f| %>
<%=l(:label_user_new)%> diff --git a/test/functional/groups_controller_test.rb b/test/functional/groups_controller_test.rb index a1a164b07..f45dfc456 100644 --- a/test/functional/groups_controller_test.rb +++ b/test/functional/groups_controller_test.rb @@ -104,4 +104,13 @@ class GroupsControllerTest < ActionController::TestCase post :destroy_membership, :id => 10, :membership_id => 6 end end + + def test_autocomplete_for_user + get :autocomplete_for_user, :id => 10, :q => 'mis' + assert_response :success + users = assigns(:users) + assert_not_nil users + assert users.any? + assert !users.include?(Group.find(10).users.first) + end end