diff options
Diffstat (limited to 'app/models/principal.rb')
-rw-r--r-- | app/models/principal.rb | 10 |
1 files changed, 10 insertions, 0 deletions
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) |