@@ -64,7 +64,7 class WatchersController < ApplicationController | |||
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 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 | 68 | if @watched |
|
69 | 69 | @users -= @watched.watcher_users |
|
70 | 70 | end |
@@ -316,7 +316,7 module ApplicationHelper | |||
|
316 | 316 | |
|
317 | 317 | def principals_check_box_tags(name, principals) |
|
318 | 318 | s = '' |
|
319 |
principals |
|
|
319 | principals.each do |principal| | |
|
320 | 320 | s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n" |
|
321 | 321 | end |
|
322 | 322 | s.html_safe |
@@ -26,7 +26,7 module GroupsHelper | |||
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 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 | 30 | principal_count = scope.count |
|
31 | 31 | principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page'] |
|
32 | 32 | principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all |
@@ -19,7 +19,7 | |||
|
19 | 19 | |
|
20 | 20 | module MembersHelper |
|
21 | 21 | def render_principals_for_new_members(project) |
|
22 |
scope = Principal.active.not_member_of(project).like(params[:q]) |
|
|
22 | scope = Principal.active.sorted.not_member_of(project).like(params[:q]) | |
|
23 | 23 | principal_count = scope.count |
|
24 | 24 | principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page'] |
|
25 | 25 | principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all |
@@ -70,6 +70,7 class Principal < ActiveRecord::Base | |||
|
70 | 70 | where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) |
|
71 | 71 | end |
|
72 | 72 | } |
|
73 | scope :sorted, lambda { order(*Principal.fields_for_order_statement)} | |
|
73 | 74 | |
|
74 | 75 | before_create :set_default_empty_values |
|
75 | 76 | |
@@ -88,6 +89,15 class Principal < ActiveRecord::Base | |||
|
88 | 89 | end |
|
89 | 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 | 101 | protected |
|
92 | 102 | |
|
93 | 103 | # Make sure we don't try to insert NULL values (see #4632) |
@@ -114,6 +114,7 class User < Principal | |||
|
114 | 114 | group_id = group.is_a?(Group) ? group.id : group.to_i |
|
115 | 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 | 119 | def set_mail_notification |
|
119 | 120 | self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
@@ -49,6 +49,20 class PrincipalTest < ActiveSupport::TestCase | |||
|
49 | 49 | assert_equal [], Principal.not_member_of([]).sort |
|
50 | 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 | 66 | context "#like" do |
|
53 | 67 | setup do |
|
54 | 68 | Principal.create!(:login => 'login') |
@@ -34,6 +34,10 class UserTest < ActiveSupport::TestCase | |||
|
34 | 34 | @dlopper = User.find(3) |
|
35 | 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 | 41 | def test_generate |
|
38 | 42 | User.generate!(:firstname => 'Testing connection') |
|
39 | 43 | User.generate!(:firstname => 'Testing connection') |
General Comments 0
You need to be logged in to leave comments.
Login now