BigW Consortium Gitlab

milestones_helper.rb 5.3 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)
Douwe Maan committed
7 8
    else
      dashboard_milestones_path(opts)
9 10
    end
  end
11

12 13
  def milestones_label_path(opts = {})
    if @project
14
      project_issues_path(@project, opts)
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 }
24 25 26 27 28 29 30 31 32 33 34

    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:)
35 36 37 38 39 40 41 42 43 44 45 46
    issues = milestone.issues.with_label(label.title)
    issues =
      case state
      when :opened
        issues.opened
      when :closed
        issues.closed
      else
        raise ArgumentError, "invalid milestone state `#{state}`"
      end

    issues.size
47 48
  end

49
  # Returns count of milestones for different states
50
  # Uses explicit hash keys as the 'opened' state URL params differs from the db value
51
  # and we need to add the total
52 53
  def milestone_counts(milestones)
    counts = milestones.reorder(nil).group(:state).count
54

55
    {
56 57 58
      opened: counts['active'] || 0,
      closed: counts['closed'] || 0,
      all: counts.values.sum || 0
59
    }
60 61
  end

62 63 64
  # 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
65
  def milestone_class_for_state(param, check, match_blank_param = false)
66 67
    if match_blank_param
      'active' if param.blank? || param == check
Felipe Artur committed
68 69
    elsif param == check
      'active'
70
    else
Felipe Artur committed
71
      check
72 73 74
    end
  end

75 76 77
  def milestone_progress_bar(milestone)
    options = {
      class: 'progress-bar progress-bar-success',
78
      style: "width: #{milestone.percent_complete(current_user)}%;"
79 80 81 82 83 84
    }

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

86
  def milestones_filter_dropdown_path
87
    project = @target_project || @project
88
    if project
89
      project_milestones_path(project, :json)
90 91
    elsif @group
      group_milestones_path(@group, :json)
92
    else
93
      dashboard_milestones_path(:json)
94
    end
95
  end
96

97 98 99 100 101 102
  def milestone_tooltip_title(milestone)
    if milestone.due_date
      [milestone.due_date.to_s(:medium), "(#{milestone_remaining_days(milestone)})"].join(' ')
    end
  end

103
  def milestone_remaining_days(milestone)
104
    if milestone.expired?
Fatih Acet committed
105
      content_tag(:strong, 'Past due')
106 107
    elsif milestone.upcoming?
      content_tag(:strong, 'Upcoming')
108 109 110 111 112
    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"
113
      content.html_safe
114 115 116 117
    elsif milestone.start_date && milestone.start_date.past?
      days    = milestone.elapsed_days
      content = content_tag(:strong, days)
      content << " #{'day'.pluralize(days)} elapsed"
118
      content.html_safe
119 120 121 122 123
    end
  end

  def milestone_date_range(milestone)
    if milestone.start_date && milestone.due_date
124
      "#{milestone.start_date.to_s(:medium)}#{milestone.due_date.to_s(:medium)}"
125 126 127 128 129 130 131 132 133 134 135 136
    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
137 138
    end
  end
139

140
  def milestone_merge_request_tab_path(milestone)
141
    if @project
142
      merge_requests_project_milestone_path(@project, milestone, format: :json)
143 144
    elsif @group
      merge_requests_group_milestone_path(@group, milestone.safe_title, title: milestone.title, format: :json)
145 146
    else
      merge_requests_dashboard_milestone_path(milestone, title: milestone.title, format: :json)
147 148 149
    end
  end

150
  def milestone_participants_tab_path(milestone)
151
    if @project
152
      participants_project_milestone_path(@project, milestone, format: :json)
153 154
    elsif @group
      participants_group_milestone_path(@group, milestone.safe_title, title: milestone.title, format: :json)
155 156
    else
      participants_dashboard_milestone_path(milestone, title: milestone.title, format: :json)
157 158 159
    end
  end

160
  def milestone_labels_tab_path(milestone)
161
    if @project
162
      labels_project_milestone_path(@project, milestone, format: :json)
163 164
    elsif @group
      labels_group_milestone_path(@group, milestone.safe_title, title: milestone.title, format: :json)
165 166
    else
      labels_dashboard_milestone_path(milestone, title: milestone.title, format: :json)
167 168
    end
  end
Felipe Artur committed
169 170 171 172

  def group_milestone_route(milestone, params = {})
    params = nil if params.empty?

173
    if milestone.legacy_group_milestone?
Felipe Artur committed
174 175 176 177 178
      group_milestone_path(@group, milestone.safe_title, title: milestone.title, milestone: params)
    else
      group_milestone_path(@group, milestone.iid, milestone: params)
    end
  end
179
end