Read about our upcoming Code of Conduct on this issue

global_milestone.rb 2.02 KB
Newer Older
1
class GlobalMilestone
2
3
  include Milestoneish

4
  attr_accessor :title, :milestones
5
6
  alias_attribute :name, :title

7
8
9
10
  def for_display
    @first_milestone
  end

11
12
13
14
  def self.build_collection(milestones)
    milestones = milestones.group_by(&:title)

    milestones.map do |title, milestones|
15
16
      milestones_relation = Milestone.where(id: milestones.map(&:id))
      new(title, milestones_relation)
17
18
19
    end
  end

20
21
  def initialize(title, milestones)
    @title = title
22
    @name = title
23
    @milestones = milestones
24
    @first_milestone = milestones.find {|m| m.description.present? } || milestones.first
25
26
  end

27
  def safe_title
28
    @title.to_slug.normalize.to_s
29
  end
30

Greg Smethells's avatar
Greg Smethells committed
31
32
33
34
35
36
37
38
  def expired?
    if due_date
      due_date.past?
    else
      false
    end
  end

39
  def projects
40
    @projects ||= Project.for_milestones(milestones.select(:id))
41
42
  end

Marin Jankovski's avatar
Marin Jankovski committed
43
  def state
Marin Jankovski's avatar
Marin Jankovski committed
44
    state = milestones.map { |milestone| milestone.state }
45

46
    if state.count('closed') == state.size
Marin Jankovski's avatar
Marin Jankovski committed
47
      'closed'
48
49
    else
      'active'
Marin Jankovski's avatar
Marin Jankovski committed
50
51
    end
  end
52
53
54
55
56
57
58
59
60

  def active?
    state == 'active'
  end

  def closed?
    state == 'closed'
  end

61
  def issues
62
    @issues ||= Issue.of_milestones(milestones.select(:id)).includes(:project, :assignee, :labels)
63
64
  end

65
  def merge_requests
66
    @merge_requests ||= MergeRequest.of_milestones(milestones.select(:id)).includes(:target_project, :assignee, :labels)
67
68
69
  end

  def participants
70
    @participants ||= milestones.includes(:participants).map(&:participants).flatten.compact.uniq
71
  end
72

73
  def labels
74
    @labels ||= GlobalLabel.build_collection(milestones.includes(:labels).map(&:labels).flatten)
75
                           .sort_by!(&:title)
76
  end
Greg Smethells's avatar
Greg Smethells committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

  def due_date
    return @due_date if defined?(@due_date)

    @due_date =
      if @milestones.all? { |x| x.due_date == @milestones.first.due_date }
        @milestones.first.due_date
      else
        nil
      end
  end

  def expires_at
    if due_date
      if due_date.past?
92
        "expired on #{due_date.to_s(:medium)}"
Greg Smethells's avatar
Greg Smethells committed
93
      else
94
        "expires on #{due_date.to_s(:medium)}"
Greg Smethells's avatar
Greg Smethells committed
95
96
97
      end
    end
  end
98
end