##// END OF EJS Templates
Adds named scopes for users index....
Jean-Philippe Lang -
r7961:f52410be1922
parent child
Show More
@@ -38,23 +38,17 class UsersController < ApplicationController
38 @limit = per_page_option
38 @limit = per_page_option
39 end
39 end
40
40
41 scope = User
41 @status = params[:status] || 1
42 scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present?
43
42
44 @status = params[:status] ? params[:status].to_i : 1
43 scope = User.logged.status(@status)
45 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
44 scope = scope.like(params[:name]) if params[:name].present?
45 scope = scope.in_group(params[:group_id]) if params[:group_id].present?
46
46
47 unless params[:name].blank?
47 @user_count = scope.count
48 name = "%#{params[:name].strip.downcase}%"
49 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
50 end
51
52 @user_count = scope.count(:conditions => c.conditions)
53 @user_pages = Paginator.new self, @user_count, @limit, params['page']
48 @user_pages = Paginator.new self, @user_count, @limit, params['page']
54 @offset ||= @user_pages.current.offset
49 @offset ||= @user_pages.current.offset
55 @users = scope.find :all,
50 @users = scope.find :all,
56 :order => sort_clause,
51 :order => sort_clause,
57 :conditions => c.conditions,
58 :limit => @limit,
52 :limit => @limit,
59 :offset => @offset
53 :offset => @offset
60
54
@@ -19,9 +19,9 module UsersHelper
19 def users_status_options_for_select(selected)
19 def users_status_options_for_select(selected)
20 user_count_by_status = User.count(:group => 'status').to_hash
20 user_count_by_status = User.count(:group => 'status').to_hash
21 options_for_select([[l(:label_all), ''],
21 options_for_select([[l(:label_all), ''],
22 ["#{l(:status_active)} (#{user_count_by_status[1].to_i})", 1],
22 ["#{l(:status_active)} (#{user_count_by_status[1].to_i})", '1'],
23 ["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", 2],
23 ["#{l(:status_registered)} (#{user_count_by_status[2].to_i})", '2'],
24 ["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", 3]], selected)
24 ["#{l(:status_locked)} (#{user_count_by_status[3].to_i})", '3']], selected.to_s)
25 end
25 end
26
26
27 # Options for the new membership projects combo-box
27 # Options for the new membership projects combo-box
@@ -54,6 +54,16 class User < Principal
54
54
55 # Active non-anonymous users scope
55 # Active non-anonymous users scope
56 named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
56 named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
57 named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
58 named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
59 named_scope :like, lambda {|arg|
60 if arg.blank?
61 {}
62 else
63 pattern = "%#{arg.to_s.strip.downcase}%"
64 {:conditions => ["LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :p", {:p => pattern}]}
65 end
66 }
57
67
58 acts_as_customizable
68 acts_as_customizable
59
69
@@ -49,6 +49,14 class UsersControllerTest < ActionController::TestCase
49 assert_nil assigns(:users).detect {|u| !u.active?}
49 assert_nil assigns(:users).detect {|u| !u.active?}
50 end
50 end
51
51
52 def test_index_with_status_filter
53 get :index, :status => 3
54 assert_response :success
55 assert_template 'index'
56 assert_not_nil assigns(:users)
57 assert_equal [3], assigns(:users).map(&:status).uniq
58 end
59
52 def test_index_with_name_filter
60 def test_index_with_name_filter
53 get :index, :name => 'john'
61 get :index, :name => 'john'
54 assert_response :success
62 assert_response :success
General Comments 0
You need to be logged in to leave comments. Login now