summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2013-01-23 17:44:28 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2013-01-23 17:44:28 +0000
commite53a5de918c1db1f409df8d72a0cb5f38b4c7671 (patch)
treebea491687e40e92fa1713da2ba363cece392a24e /app
parentc7df78f3b6aee78403e8563b504e10770270fb24 (diff)
downloadredmine-e53a5de918c1db1f409df8d72a0cb5f38b4c7671.tar.gz
redmine-e53a5de918c1db1f409df8d72a0cb5f38b4c7671.zip
Adds "sorted" scope to Principal and User and sort users/groups properly.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11259 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r--app/controllers/watchers_controller.rb2
-rw-r--r--app/helpers/application_helper.rb2
-rw-r--r--app/helpers/groups_helper.rb2
-rw-r--r--app/helpers/members_helper.rb2
-rw-r--r--app/models/principal.rb10
-rw-r--r--app/models/user.rb1
6 files changed, 15 insertions, 4 deletions
diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb
index d9275b965..e564d64ec 100644
--- a/app/controllers/watchers_controller.rb
+++ b/app/controllers/watchers_controller.rb
@@ -64,7 +64,7 @@ class WatchersController < ApplicationController
end
def autocomplete_for_user
- @users = User.active.like(params[:q]).limit(100).all
+ @users = User.active.sorted.like(params[:q]).limit(100).all
if @watched
@users -= @watched.watcher_users
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 90c0072a9..08141a25c 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -316,7 +316,7 @@ module ApplicationHelper
def principals_check_box_tags(name, principals)
s = ''
- principals.sort.each do |principal|
+ principals.each do |principal|
s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
end
s.html_safe
diff --git a/app/helpers/groups_helper.rb b/app/helpers/groups_helper.rb
index 5e6b1f7aa..aa58eb575 100644
--- a/app/helpers/groups_helper.rb
+++ b/app/helpers/groups_helper.rb
@@ -26,7 +26,7 @@ module GroupsHelper
end
def render_principals_for_new_group_users(group)
- scope = User.active.not_in_group(group).like(params[:q])
+ scope = User.active.sorted.not_in_group(group).like(params[:q])
principal_count = scope.count
principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page']
principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all
diff --git a/app/helpers/members_helper.rb b/app/helpers/members_helper.rb
index eb06a3977..afb90f3df 100644
--- a/app/helpers/members_helper.rb
+++ b/app/helpers/members_helper.rb
@@ -19,7 +19,7 @@
module MembersHelper
def render_principals_for_new_members(project)
- scope = Principal.active.not_member_of(project).like(params[:q]).order('type, login, lastname ASC')
+ scope = Principal.active.sorted.not_member_of(project).like(params[:q])
principal_count = scope.count
principal_pages = Redmine::Pagination::Paginator.new principal_count, 100, params['page']
principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all
diff --git a/app/models/principal.rb b/app/models/principal.rb
index 921677647..97babba1c 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -70,6 +70,7 @@ class Principal < ActiveRecord::Base
where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
end
}
+ scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
before_create :set_default_empty_values
@@ -88,6 +89,15 @@ class Principal < ActiveRecord::Base
end
end
+ # Returns an array of fields names than can be used to make an order statement for principals.
+ # Users are sorted before Groups.
+ # Examples:
+ def self.fields_for_order_statement(table=nil)
+ table ||= table_name
+ columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
+ columns.uniq.map {|field| "#{table}.#{field}"}
+ end
+
protected
# Make sure we don't try to insert NULL values (see #4632)
diff --git a/app/models/user.rb b/app/models/user.rb
index e4b54dc41..a4e1fe47e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -114,6 +114,7 @@ class User < Principal
group_id = group.is_a?(Group) ? group.id : group.to_i
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)
}
+ scope :sorted, lambda { order(*User.fields_for_order_statement)}
def set_mail_notification
self.mail_notification = Setting.default_notification_option if self.mail_notification.blank?