milestones_helper.rb 4.6 KB
Newer Older
1 2 3
module MilestonesHelper
  def milestones_filter_path(opts = {})
    if @project
4
      project_milestones_path(@project, opts)
5 6
    elsif @group
      group_milestones_path(@group, opts)
D
Douwe Maan 已提交
7 8
    else
      dashboard_milestones_path(opts)
9 10
    end
  end
11

R
Rubén Dávila 已提交
12 13
  def milestones_label_path(opts = {})
    if @project
14
      project_issues_path(@project, opts)
R
Rubén Dávila 已提交
15 16 17 18 19 20 21
    elsif @group
      issues_group_path(@group, opts)
    else
      issues_dashboard_path(opts)
    end
  end

22 23
  def milestones_browse_issuables_path(milestone, state: nil, type:)
    opts = { milestone_title: milestone.title, state: state }
R
Rubén Dávila 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37

    if @project
      polymorphic_path([@project.namespace.becomes(Namespace), @project, type], opts)
    elsif @group
      polymorphic_url([type, @group], opts)
    else
      polymorphic_url([type, :dashboard], opts)
    end
  end

  def milestone_issues_by_label_count(milestone, label, state:)
    milestone.issues.with_label(label.title).send(state).size
  end

38
  # Returns count of milestones for different states
39
  # Uses explicit hash keys as the 'opened' state URL params differs from the db value
40
  # and we need to add the total
41 42
  def milestone_counts(milestones)
    counts = milestones.reorder(nil).group(:state).count
43

44
    {
45 46 47
      opened: counts['active'] || 0,
      closed: counts['closed'] || 0,
      all: counts.values.sum || 0
48
    }
49 50
  end

51 52 53
  # Show 'active' class if provided GET param matches check
  # `or_blank` allows the function to return 'active' when given an empty param
  # Could be refactored to be simpler but that may make it harder to read
54
  def milestone_class_for_state(param, check, match_blank_param = false)
55 56 57 58 59 60 61
    if match_blank_param
      'active' if param.blank? || param == check
    else
      'active' if param == check
    end
  end

62 63 64
  def milestone_progress_bar(milestone)
    options = {
      class: 'progress-bar progress-bar-success',
65
      style: "width: #{milestone.percent_complete(current_user)}%;"
66 67 68 69 70 71
    }

    content_tag :div, class: 'progress' do
      content_tag :div, nil, options
    end
  end
72

P
Phil Hughes 已提交
73
  def milestones_filter_dropdown_path
74
    project = @target_project || @project
75
    if project
76
      project_milestones_path(project, :json)
77 78
    elsif @group
      group_milestones_path(@group, :json)
P
Phil Hughes 已提交
79
    else
80
      dashboard_milestones_path(:json)
P
Phil Hughes 已提交
81
    end
82
  end
83

84
  def milestone_remaining_days(milestone)
85
    if milestone.expired?
F
Fatih Acet 已提交
86
      content_tag(:strong, 'Past due')
V
Valery Sizov 已提交
87 88
    elsif milestone.upcoming?
      content_tag(:strong, 'Upcoming')
M
Michael 已提交
89 90 91 92 93
    elsif milestone.due_date
      time_ago = time_ago_in_words(milestone.due_date)
      content = time_ago.gsub(/\d+/) { |match| "<strong>#{match}</strong>" }
      content.slice!("about ")
      content << " remaining"
94
      content.html_safe
V
Valery Sizov 已提交
95 96 97 98
    elsif milestone.start_date && milestone.start_date.past?
      days    = milestone.elapsed_days
      content = content_tag(:strong, days)
      content << " #{'day'.pluralize(days)} elapsed"
99
      content.html_safe
V
Valery Sizov 已提交
100 101 102 103 104
    end
  end

  def milestone_date_range(milestone)
    if milestone.start_date && milestone.due_date
105
      "#{milestone.start_date.to_s(:medium)}#{milestone.due_date.to_s(:medium)}"
V
Valery Sizov 已提交
106 107 108 109 110 111 112 113 114 115 116 117
    elsif milestone.due_date
      if milestone.due_date.past?
        "expired on #{milestone.due_date.to_s(:medium)}"
      else
        "expires on #{milestone.due_date.to_s(:medium)}"
      end
    elsif milestone.start_date
      if milestone.start_date.past?
        "started on #{milestone.start_date.to_s(:medium)}"
      else
        "starts on #{milestone.start_date.to_s(:medium)}"
      end
118 119
    end
  end
P
Phil Hughes 已提交
120

P
Phil Hughes 已提交
121
  def milestone_merge_request_tab_path(milestone)
P
Phil Hughes 已提交
122
    if @project
123
      merge_requests_project_milestone_path(@project, milestone, format: :json)
P
Phil Hughes 已提交
124 125
    elsif @group
      merge_requests_group_milestone_path(@group, milestone.safe_title, title: milestone.title, format: :json)
126 127
    else
      merge_requests_dashboard_milestone_path(milestone, title: milestone.title, format: :json)
P
Phil Hughes 已提交
128 129 130
    end
  end

P
Phil Hughes 已提交
131
  def milestone_participants_tab_path(milestone)
P
Phil Hughes 已提交
132
    if @project
133
      participants_project_milestone_path(@project, milestone, format: :json)
P
Phil Hughes 已提交
134 135
    elsif @group
      participants_group_milestone_path(@group, milestone.safe_title, title: milestone.title, format: :json)
136 137
    else
      participants_dashboard_milestone_path(milestone, title: milestone.title, format: :json)
P
Phil Hughes 已提交
138 139 140
    end
  end

P
Phil Hughes 已提交
141
  def milestone_labels_tab_path(milestone)
P
Phil Hughes 已提交
142
    if @project
143
      labels_project_milestone_path(@project, milestone, format: :json)
P
Phil Hughes 已提交
144 145
    elsif @group
      labels_group_milestone_path(@group, milestone.safe_title, title: milestone.title, format: :json)
146 147
    else
      labels_dashboard_milestone_path(milestone, title: milestone.title, format: :json)
P
Phil Hughes 已提交
148 149
    end
  end
150
end