From 5deae7ebe344e2011c18aece4af0b2b4fcbc19c3 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 3 Dec 2010 14:08:52 -0800 Subject: [PATCH] [#800 #801] Refactor AutoCompletesController#users to not be coupled to Groups --- app/controllers/auto_completes_controller.rb | 11 +++-- app/views/groups/_users.html.erb | 2 +- .../auto_completes_controller_test.rb | 49 +++++++++++++------ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/app/controllers/auto_completes_controller.rb b/app/controllers/auto_completes_controller.rb index 9ab983c4..fd6b58ac 100644 --- a/app/controllers/auto_completes_controller.rb +++ b/app/controllers/auto_completes_controller.rb @@ -34,10 +34,15 @@ class AutoCompletesController < ApplicationController end def users - @group = Group.find(params[:id]) - @users = User.active.like(params[:q]).find(:all, :limit => 100) - @group.users + if params[:remove_group_members].present? + @group = Group.find(params[:remove_group_members]) + @removed_users = @group.users + else + @removed_users = [] + end + + @users = User.active.like(params[:q]).find(:all, :limit => 100) - @removed_users render :layout => false - end private diff --git a/app/views/groups/_users.html.erb b/app/views/groups/_users.html.erb index 5e9ed0c3..2fa91cb5 100644 --- a/app/views/groups/_users.html.erb +++ b/app/views/groups/_users.html.erb @@ -33,7 +33,7 @@ <%= observe_field(:user_search, :frequency => 0.5, :update => :users, - :url => auto_complete_users_path(:id => @group), + :url => auto_complete_users_path(:remove_group_members => @group), :with => 'q') %> diff --git a/test/functional/auto_completes_controller_test.rb b/test/functional/auto_completes_controller_test.rb index c561f70c..fb49e491 100644 --- a/test/functional/auto_completes_controller_test.rb +++ b/test/functional/auto_completes_controller_test.rb @@ -71,22 +71,43 @@ class AutoCompletesControllerTest < ActionController::TestCase @lastname = User.generate!(:lastname => 'Complete') @none = User.generate!(:login => 'hello', :firstname => 'ABC', :lastname => 'DEF') @inactive = User.generate!(:firstname => 'Complete', :status => User::STATUS_LOCKED) + end + + context "with no restrictions" do + setup do + get :users, :q => 'complete' + end - get :users, :q => 'complete', :id => Group.first.id + should_respond_with :success + + should "render a list of matching users in checkboxes" do + assert_select "input[type=checkbox][value=?]", @login.id + assert_select "input[type=checkbox][value=?]", @firstname.id + assert_select "input[type=checkbox][value=?]", @lastname.id + assert_select "input[type=checkbox][value=?]", @none.id, :count => 0 + end + + should "only show active users" do + assert_select "input[type=checkbox][value=?]", @inactive.id, :count => 0 + end end - - should_respond_with :success - - should "render a list of matching users in checkboxes" do - assert_select "input[type=checkbox][value=?]", @login.id - assert_select "input[type=checkbox][value=?]", @firstname.id - assert_select "input[type=checkbox][value=?]", @lastname.id - assert_select "input[type=checkbox][value=?]", @none.id, :count => 0 - end - - should "only show active users" do - assert_select "input[type=checkbox][value=?]", @inactive.id, :count => 0 + + context "restrict by removing group members" do + setup do + @group = Group.first + @group.users << @login + @group.users << @firstname + get :users, :q => 'complete', :remove_group_members => @group.id + end + + should_respond_with :success + + should "not include existing members of the Group" do + assert_select "input[type=checkbox][value=?]", @lastname.id + + assert_select "input[type=checkbox][value=?]", @login.id, :count => 0 + assert_select "input[type=checkbox][value=?]", @firstname.id, :count => 0 + end end end - end