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
This commit is contained in:
Jean-Philippe Lang
2013-01-23 17:44:28 +00:00
parent c7df78f3b6
commit e53a5de918
8 changed files with 33 additions and 4 deletions
+10
View 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)
+1
View 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?