]> source.dussan.org Git - redmine.git/commitdiff
Adds "sorted" scope to Principal and User and sort users/groups properly.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 23 Jan 2013 17:44:28 +0000 (17:44 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 23 Jan 2013 17:44:28 +0000 (17:44 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11259 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/watchers_controller.rb
app/helpers/application_helper.rb
app/helpers/groups_helper.rb
app/helpers/members_helper.rb
app/models/principal.rb
app/models/user.rb
test/unit/principal_test.rb
test/unit/user_test.rb

index d9275b96581589e0350044b0566029614654c72c..e564d64ec8f17b6be474ec6ed685fe03a64c13fe 100644 (file)
@@ -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
index 90c0072a9d706a685592978fd0a8900c04efeca6..08141a25cf92c203f3ce1e42bba17ff6bbce2d7e 100644 (file)
@@ -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
index 5e6b1f7aaf8d94dac7a0759b24b81589a5774caf..aa58eb57584f12393348bfb5176e7db876ddc4b2 100644 (file)
@@ -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
index eb06a397791e8a14c5c4b0863e2ee7637ff6df0c..afb90f3df0959a86801a7b2c19905510c1bde812 100644 (file)
@@ -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
index 9216776479fee5c76a266abc450297bb06aca20e..97babba1ce1fd5056576f0dbc47029b8c4887556 100644 (file)
@@ -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)
index e4b54dc41d2583ac3284de88841dd07cf2bb91d6..a4e1fe47e58bdf9ebf5ab62cb78a1e432bdc4879 100644 (file)
@@ -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?
index b96e786e96ce404068fa8e04f672777f50036230..8cd511ea2bbdf78c2d47005ac8b98b0c0ff810fd 100644 (file)
@@ -49,6 +49,20 @@ class PrincipalTest < ActiveSupport::TestCase
     assert_equal [], Principal.not_member_of([]).sort
   end
 
+  def test_sorted_scope_should_sort_users_before_groups
+    scope = Principal.where("type <> ?", 'AnonymousUser')
+    expected_order = scope.all.sort do |a, b|
+      if a.is_a?(User) && b.is_a?(Group)
+        -1
+      elsif a.is_a?(Group) && b.is_a?(User)
+        1
+      else
+        a.name.downcase <=> b.name.downcase
+      end
+    end
+    assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase)
+  end
+
   context "#like" do
     setup do
       Principal.create!(:login => 'login')
index bf74dd6da505fe5916f7d133ab26686a9470d2e5..c0e51d4694deb4b763f6b303a3db2a41338ddf91 100644 (file)
@@ -34,6 +34,10 @@ class UserTest < ActiveSupport::TestCase
     @dlopper = User.find(3)
   end
 
+  def test_sorted_scope_should_sort_user_by_display_name
+    assert_equal User.all.map(&:name).map(&:downcase).sort, User.sorted.all.map(&:name).map(&:downcase)
+  end
+
   def test_generate
     User.generate!(:firstname => 'Testing connection')
     User.generate!(:firstname => 'Testing connection')