From f52410be1922ba62c9a9b0313b5b86e13d8df704 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 4 Dec 2011 22:24:33 +0000 Subject: [PATCH] Adds named scopes for users index. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8081 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/users_controller.rb | 16 +++++----------- app/helpers/users_helper.rb | 6 +++--- app/models/user.rb | 10 ++++++++++ test/functional/users_controller_test.rb | 8 ++++++++ 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c153adf41..4bd19af7f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -38,23 +38,17 @@ class UsersController < ApplicationController @limit = per_page_option end - scope = User - scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present? + @status = params[:status] || 1 - @status = params[:status] ? params[:status].to_i : 1 - c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) + scope = User.logged.status(@status) + scope = scope.like(params[:name]) if params[:name].present? + scope = scope.in_group(params[:group_id]) if params[:group_id].present? - unless params[:name].blank? - name = "%#{params[:name].strip.downcase}%" - c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] - end - - @user_count = scope.count(:conditions => c.conditions) + @user_count = scope.count @user_pages = Paginator.new self, @user_count, @limit, params['page'] @offset ||= @user_pages.current.offset @users = scope.find :all, :order => sort_clause, - :conditions => c.conditions, :limit => @limit, :offset => @offset diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 51ff99a9a..6df6b8d3f 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -19,9 +19,9 @@ module UsersHelper def users_status_options_for_select(selected) user_count_by_status = User.count(:group => 'status').to_hash options_for_select([[l(:label_all), ''], - ["#{l(:status_active)} (#{user_count_by_status[1].to_i})", 1], - ["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", 2], - ["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", 3]], selected) + ["#{l(:status_active)} (#{user_count_by_status[1].to_i})", '1'], + ["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", '2'], + ["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", '3']], selected.to_s) end # Options for the new membership projects combo-box diff --git a/app/models/user.rb b/app/models/user.rb index 8268d752e..c7b883e6e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -54,6 +54,16 @@ class User < Principal # Active non-anonymous users scope named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}" + named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}" + named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} } + named_scope :like, lambda {|arg| + if arg.blank? + {} + else + pattern = "%#{arg.to_s.strip.downcase}%" + {:conditions => ["LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :p", {:p => pattern}]} + end + } acts_as_customizable diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 163d8f642..a39358152 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -49,6 +49,14 @@ class UsersControllerTest < ActionController::TestCase assert_nil assigns(:users).detect {|u| !u.active?} end + def test_index_with_status_filter + get :index, :status => 3 + assert_response :success + assert_template 'index' + assert_not_nil assigns(:users) + assert_equal [3], assigns(:users).map(&:status).uniq + end + def test_index_with_name_filter get :index, :name => 'john' assert_response :success