feature-33174-with-deprecation-warning.patch

Mizuki ISHIKAWA, 2020-04-01 06:13

Download (3.34 KB)

View differences:

app/controllers/projects_controller.rb
162 162
      return
163 163
    end
164 164

  
165
    @users_by_role = @project.users_by_role
165
    @principals_by_role = @project.principals_by_role
166 166
    @subprojects = @project.children.visible.to_a
167 167
    @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").to_a
168 168
    @trackers = @project.rolled_up_trackers.visible
app/models/project.rb
507 507
    end
508 508
  end
509 509

  
510
  # Returns a hash of project users grouped by role
511
  def users_by_role
512
    members.includes(:user, :roles).inject({}) do |h, m|
510
  # Returns a hash of project users/groups grouped by role
511
  def principals_by_role
512
    memberships.includes(:principal, :roles).inject({}) do |h, m|
513 513
      m.roles.each do |r|
514 514
        h[r] ||= []
515
        h[r] << m.user
515
        h[r] << m.principal
516 516
      end
517 517
      h
518 518
    end
519 519
  end
520 520

  
521
  def members_by_role
522
    ActiveSupport::Deprecation.warn "Project#members_by_role will be removed. Use Project#principals_by_role instead."
523
    principals_by_role
524
  end
525

  
521 526
  # Adds user as a project member with the default role
522 527
  # Used for when a non-admin user creates a project
523 528
  def add_default_member(user)
app/views/projects/_members_box.html.erb
1
  <% if @users_by_role.any? %>
1
  <% if @principals_by_role.any? %>
2 2
  <div class="members box">
3 3
    <h3 class="icon icon-group"><%=l(:label_member_plural)%></h3>
4
    <% @users_by_role.keys.sort.each do |role| %>
5
      <p><span class="label"><%= role %>:</span> <%= @users_by_role[role].sort.collect{|u| link_to_user u}.join(", ").html_safe %></p>
4
    <% @principals_by_role.keys.sort.each do |role| %>
5
      <p><span class="label"><%= role %>:</span> <%= @principals_by_role[role].sort.collect{|p| link_to_user p}.join(", ").html_safe %></p>
6 6
    <% end %>
7 7
  </div>
8 8
  <% end %>
test/unit/project_test.rb
455 455
    assert Project.find(3).allowed_parents.include?(nil)
456 456
  end
457 457

  
458
  def test_users_by_role
459
    users_by_role = Project.find(1).users_by_role
460
    assert_kind_of Hash, users_by_role
458
  def test_principals_by_role
459
    group = Group.find(10)
460
    Member.create!(:principal => group, :project_id => 1, :role_ids => [1])
461

  
462
    principals_by_role = Project.find(1).principals_by_role
463
    assert_kind_of Hash, principals_by_role
461 464
    role = Role.find(1)
462
    assert_kind_of Array, users_by_role[role]
463
    assert users_by_role[role].include?(User.find(2))
465
    assert_kind_of Array, principals_by_role[role]
466
    assert principals_by_role[role].include?(User.find(2))
467
    assert principals_by_role[role].include?(group)
464 468
  end
465 469

  
466 470
  def test_rolled_up_trackers