From e22159a3cb721384de38166e4a014f08ebe08829 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 3 Dec 2016 08:26:08 +0000 Subject: [PATCH] Sort members on the DB side. git-svn-id: http://svn.redmine.org/redmine/trunk@16040 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/member.rb | 7 +++++++ app/views/projects/settings/_members.html.erb | 2 +- test/unit/member_test.rb | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/member.rb b/app/models/member.rb index 6e7cf258b..b37a02f48 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -31,6 +31,13 @@ class Member < ActiveRecord::Base scope :active, lambda { joins(:principal).where(:users => {:status => Principal::STATUS_ACTIVE})} + # Sort by first role and principal + scope :sorted, lambda { + includes(:member_roles, :roles, :principal). + reorder("#{Role.table_name}.position"). + order(Principal.fields_for_order_statement) + } + alias :base_reload :reload def reload(*args) @managed_roles = nil diff --git a/app/views/projects/settings/_members.html.erb b/app/views/projects/settings/_members.html.erb index fe1295d2c..05707fe78 100644 --- a/app/views/projects/settings/_members.html.erb +++ b/app/views/projects/settings/_members.html.erb @@ -1,5 +1,5 @@ <% roles = Role.find_all_givable - members = @project.memberships.active.includes(:member_roles, :roles, :principal).to_a.sort %> + members = @project.memberships.active.sorted.to_a %>

<%= link_to l(:label_member_new), new_project_membership_path(@project), :remote => true, :class => "icon icon-add" %>

diff --git a/test/unit/member_test.rb b/test/unit/member_test.rb index d0e2ea042..ce9898f4b 100644 --- a/test/unit/member_test.rb +++ b/test/unit/member_test.rb @@ -38,6 +38,12 @@ class MemberTest < ActiveSupport::TestCase @jsmith = Member.find(1) end + def test_sorted_scope_on_project_members + members = Project.find(1).members.sorted.to_a + roles = members.map {|m| m.roles.sort.first} + assert_equal roles, roles.sort + end + def test_create member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2]) assert member.save -- 2.39.5