From f417a265d7783cb10e4b55556f5de9fd0a7c9696 Mon Sep 17 00:00:00 2001
From: randx <dmitriy.zaporozhets@gmail.com>
Date: Mon, 29 Oct 2012 23:45:11 +0200
Subject: [PATCH] Finalize milestones for Merge Requests

---
 app/assets/javascripts/milestones.js.coffee |  7 +++++
 app/assets/stylesheets/common.scss          |  8 ++++++
 app/controllers/milestones_controller.rb    |  2 +-
 app/decorators/user_decorator.rb            | 11 ++++++++
 app/helpers/projects_helper.rb              |  4 +++
 app/models/merge_request.rb                 |  1 +
 app/models/milestone.rb                     | 14 ++++++----
 app/views/milestones/_milestone.html.haml   |  6 ++---
 app/views/milestones/show.html.haml         | 29 +++++++++------------
 9 files changed, 57 insertions(+), 25 deletions(-)
 create mode 100644 app/decorators/user_decorator.rb

diff --git a/app/assets/javascripts/milestones.js.coffee b/app/assets/javascripts/milestones.js.coffee
index 13aba86093..e40a69ebaa 100644
--- a/app/assets/javascripts/milestones.js.coffee
+++ b/app/assets/javascripts/milestones.js.coffee
@@ -5,3 +5,10 @@ $ ->
     $('.milestone-issue-filter li').toggleClass('active')
     $('.milestone-issue-filter tr[data-closed]').toggleClass('hide')
     false
+
+  $('.milestone-merge-requests-filter tr[data-closed]').addClass('hide')
+
+  $('.milestone-merge-requests-filter ul.nav li a').click ->
+    $('.milestone-merge-requests-filter li').toggleClass('active')
+    $('.milestone-merge-requests-filter tr[data-closed]').toggleClass('hide')
+    false
diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss
index dfb436977c..843b683f4e 100644
--- a/app/assets/stylesheets/common.scss
+++ b/app/assets/stylesheets/common.scss
@@ -675,3 +675,11 @@ pre {
   margin-bottom: 0;
   margin-top:4px;
 }
+
+.float-link {
+  float:left;
+  margin-right:15px;
+  .s16 {
+    margin-right:5px;
+  }
+}
diff --git a/app/controllers/milestones_controller.rb b/app/controllers/milestones_controller.rb
index ac28f00015..fadfee2dc0 100644
--- a/app/controllers/milestones_controller.rb
+++ b/app/controllers/milestones_controller.rb
@@ -31,7 +31,7 @@ class MilestonesController < ProjectResourceController
 
   def show
     @issues = @milestone.issues
-    @users = @milestone.participants
+    @users = UserDecorator.decorate(@milestone.participants)
     @merge_requests = @milestone.merge_requests
 
     respond_to do |format|
diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb
new file mode 100644
index 0000000000..af9c6a63e7
--- /dev/null
+++ b/app/decorators/user_decorator.rb
@@ -0,0 +1,11 @@
+class UserDecorator < ApplicationDecorator
+  decorates :user
+
+  def avatar_image size = 16
+    h.image_tag h.gravatar_icon(self.email, size), class: "avatar #{"s#{size}"}", width: size
+  end
+
+  def tm_of(project)
+    project.team_member_by_id(self.id)
+  end
+end
diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb
index 468ace1564..7c302ef417 100644
--- a/app/helpers/projects_helper.rb
+++ b/app/helpers/projects_helper.rb
@@ -10,5 +10,9 @@ module ProjectsHelper
   def link_to_project project
     link_to project.name, project
   end
+
+  def tm_path team_member
+    project_team_member_path(@project, team_member)
+  end
 end
 
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 7dc0095864..c737258d7b 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -1,4 +1,5 @@
 require Rails.root.join("app/models/commit")
+require Rails.root.join("app/roles/static_model")
 
 class MergeRequest < ActiveRecord::Base
   include IssueCommonality
diff --git a/app/models/milestone.rb b/app/models/milestone.rb
index 233270ed74..41412a13bf 100644
--- a/app/models/milestone.rb
+++ b/app/models/milestone.rb
@@ -16,16 +16,20 @@ class Milestone < ActiveRecord::Base
     User.where(id: issues.pluck(:assignee_id))
   end
 
-  def issues_percent_complete
-    ((self.issues.closed.count * 100) / self.issues.count).abs
+  def open_items_count
+    self.issues.opened.count + self.merge_requests.opened.count
   end
 
-  def merge_requests_percent_complete
-    ((self.merge_requests.closed.count * 100) / self.merge_requests.count).abs
+  def closed_items_count
+    self.issues.closed.count + self.merge_requests.closed.count
+  end
+
+  def total_items_count
+    self.issues.count + self.merge_requests.count
   end
 
   def percent_complete
-    (issues_percent_complete + merge_requests_percent_complete) / 2
+    ((closed_items_count * 100) / total_items_count).abs
   rescue ZeroDivisionError
     100
   end
diff --git a/app/views/milestones/_milestone.html.haml b/app/views/milestones/_milestone.html.haml
index 2bb78975ba..05532c3879 100644
--- a/app/views/milestones/_milestone.html.haml
+++ b/app/views/milestones/_milestone.html.haml
@@ -9,13 +9,13 @@
     %small
       = milestone.expires_at
   .row
-    .progress.progress-success.span4
+    .progress.progress-info.span4
       .bar{style: "width: #{milestone.percent_complete}%;"}
     .span6
       - if milestone.issues.any?
-        = link_to project_issues_path(milestone.project, milestone_id: milestone.id), class: "padded" do
+        = link_to project_issues_path(milestone.project, milestone_id: milestone.id), class: "btn very_small" do
           %strong= pluralize milestone.issues.count, 'Issue'
 
       - if milestone.merge_requests.any?
-        = link_to project_merge_requests_path(milestone.project, milestone_id: milestone.id), class: "padded" do
+        = link_to project_merge_requests_path(milestone.project, milestone_id: milestone.id), class: "btn very_small" do
           %strong= pluralize milestone.issues.count, 'Merge Request'
diff --git a/app/views/milestones/show.html.haml b/app/views/milestones/show.html.haml
index ad4580d462..b8bc788c95 100644
--- a/app/views/milestones/show.html.haml
+++ b/app/views/milestones/show.html.haml
@@ -31,10 +31,10 @@
     %h5
       Progress:
       %small
-        #{@milestone.issues.closed.count} closed
+        #{@milestone.closed_items_count} closed
         &ndash;
-        #{@milestone.issues.opened.count} open
-    .progress.progress-success
+        #{@milestone.open_items_count} open
+    .progress.progress-info
       .bar{style: "width: #{@milestone.percent_complete}%;"}
 
 
@@ -58,10 +58,9 @@
               %span.badge.badge-info ##{issue.id}
             &ndash;
             = link_to_gfm truncate(issue.title, length: 60), [@project, issue]
-    %br
 
   .span6
-    %table.milestone-merge_requests-filter
+    %table.milestone-merge-requests-filter
       %thead
         %th
           %ul.nav.nav-pills
@@ -74,15 +73,13 @@
               %span.badge.badge-info ##{merge_request.id}
             &ndash;
             = link_to_gfm truncate(merge_request.title, length: 60), [@project, merge_request]
-    %br
 
-  .span6
-    %table
-      %thead
-        %th Participants
-      - @users.each do |user|
-        %tr
-          %td
-            = image_tag gravatar_icon(user.email, 24), width: "24"
-            &nbsp;
-            = user.name
+%hr
+%h6 Participants:
+%div
+  - @users.each do |user|
+    = link_to tm_path(user.tm_of(@project)), class: 'float-link' do
+      = user.avatar_image
+      = user.name
+
+.clearfix
-- 
2.30.9