]> source.dussan.org Git - redmine.git/commitdiff
Sort members on the DB side.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 3 Dec 2016 08:26:08 +0000 (08:26 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 3 Dec 2016 08:26:08 +0000 (08:26 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@16040 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/member.rb
app/views/projects/settings/_members.html.erb
test/unit/member_test.rb

index 6e7cf258bc7d184df9442f59dae4c0c9e3ffc451..b37a02f48134f64c9e442e7ba5b638138c84cee3 100644 (file)
@@ -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
index fe1295d2c8f030fbd4f4c704e2ad7cec08103212..05707fe781ebb7875a69421abc4d6da942856dea 100644 (file)
@@ -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 %>
 
 <p><%= link_to l(:label_member_new), new_project_membership_path(@project), :remote => true, :class => "icon icon-add" %></p>
 
index d0e2ea042340bef7a1ba828ee6669d2451254c58..ce9898f4b98af6e99321038a91b9bd697b4e254a 100644 (file)
@@ -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