##// END OF EJS Templates
Adds "sorted" scope to Principal and User and sort users/groups properly....
Jean-Philippe Lang -
r11029:e53a5de918c1
parent child
Show More
@@ -64,7 +64,7 class WatchersController < ApplicationController
64 end
64 end
65
65
66 def autocomplete_for_user
66 def autocomplete_for_user
67 @users = User.active.like(params[:q]).limit(100).all
67 @users = User.active.sorted.like(params[:q]).limit(100).all
68 if @watched
68 if @watched
69 @users -= @watched.watcher_users
69 @users -= @watched.watcher_users
70 end
70 end
@@ -316,7 +316,7 module ApplicationHelper
316
316
317 def principals_check_box_tags(name, principals)
317 def principals_check_box_tags(name, principals)
318 s = ''
318 s = ''
319 principals.sort.each do |principal|
319 principals.each do |principal|
320 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
320 s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
321 end
321 end
322 s.html_safe
322 s.html_safe
@@ -26,7 +26,7 module GroupsHelper
26 end
26 end
27
27
28 def render_principals_for_new_group_users(group)
28 def render_principals_for_new_group_users(group)
29 scope = User.active.not_in_group(group).like(params[:q])
29 scope = User.active.sorted.not_in_group(group).like(params[:q])
30 principal_count = scope.count
30 principal_count = scope.count
31 principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page']
31 principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page']
32 principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all
32 principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all
@@ -19,7 +19,7
19
19
20 module MembersHelper
20 module MembersHelper
21 def render_principals_for_new_members(project)
21 def render_principals_for_new_members(project)
22 scope = Principal.active.not_member_of(project).like(params[:q]).order('type, login, lastname ASC')
22 scope = Principal.active.sorted.not_member_of(project).like(params[:q])
23 principal_count = scope.count
23 principal_count = scope.count
24 principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page']
24 principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page']
25 principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all
25 principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all
@@ -70,6 +70,7 class Principal < ActiveRecord::Base
70 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
70 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
71 end
71 end
72 }
72 }
73 scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
73
74
74 before_create :set_default_empty_values
75 before_create :set_default_empty_values
75
76
@@ -88,6 +89,15 class Principal < ActiveRecord::Base
88 end
89 end
89 end
90 end
90
91
92 # Returns an array of fields names than can be used to make an order statement for principals.
93 # Users are sorted before Groups.
94 # Examples:
95 def self.fields_for_order_statement(table=nil)
96 table ||= table_name
97 columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
98 columns.uniq.map {|field| "#{table}.#{field}"}
99 end
100
91 protected
101 protected
92
102
93 # Make sure we don't try to insert NULL values (see #4632)
103 # Make sure we don't try to insert NULL values (see #4632)
@@ -114,6 +114,7 class User < Principal
114 group_id = group.is_a?(Group) ? group.id : group.to_i
114 group_id = group.is_a?(Group) ? group.id : group.to_i
115 where("#{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)
115 where("#{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)
116 }
116 }
117 scope :sorted, lambda { order(*User.fields_for_order_statement)}
117
118
118 def set_mail_notification
119 def set_mail_notification
119 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
120 self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?
@@ -49,6 +49,20 class PrincipalTest < ActiveSupport::TestCase
49 assert_equal [], Principal.not_member_of([]).sort
49 assert_equal [], Principal.not_member_of([]).sort
50 end
50 end
51
51
52 def test_sorted_scope_should_sort_users_before_groups
53 scope = Principal.where("type <> ?", 'AnonymousUser')
54 expected_order = scope.all.sort do |a, b|
55 if a.is_a?(User) && b.is_a?(Group)
56 -1
57 elsif a.is_a?(Group) && b.is_a?(User)
58 1
59 else
60 a.name.downcase <=> b.name.downcase
61 end
62 end
63 assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase)
64 end
65
52 context "#like" do
66 context "#like" do
53 setup do
67 setup do
54 Principal.create!(:login => 'login')
68 Principal.create!(:login => 'login')
@@ -34,6 +34,10 class UserTest < ActiveSupport::TestCase
34 @dlopper = User.find(3)
34 @dlopper = User.find(3)
35 end
35 end
36
36
37 def test_sorted_scope_should_sort_user_by_display_name
38 assert_equal User.all.map(&:name).map(&:downcase).sort, User.sorted.all.map(&:name).map(&:downcase)
39 end
40
37 def test_generate
41 def test_generate
38 User.generate!(:firstname => 'Testing connection')
42 User.generate!(:firstname => 'Testing connection')
39 User.generate!(:firstname => 'Testing connection')
43 User.generate!(:firstname => 'Testing connection')
General Comments 0
You need to be logged in to leave comments. Login now