BigW Consortium Gitlab

global_milestone.rb 1.73 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 self.build_collection(milestones)
    milestones = milestones.group_by(&:title)

    milestones.map do |title, milestones|
Phil Hughes committed
11
      new(title, milestones)
12 13 14
    end
  end

15 16
  def initialize(title, milestones)
    @title = title
17
    @name = title
18 19 20
    @milestones = milestones
  end

21
  def safe_title
22
    @title.to_slug.normalize.to_s
23
  end
24

25 26 27 28 29 30 31 32
  def expired?
    if due_date
      due_date.past?
    else
      false
    end
  end

33
  def projects
34
    @projects ||= Project.for_milestones(milestones.map(&:id))
35 36
  end

37
  def state
Marin Jankovski committed
38
    state = milestones.map { |milestone| milestone.state }
39

40
    if state.count('closed') == state.size
41
      'closed'
42 43
    else
      'active'
44 45
    end
  end
46 47 48 49 50 51 52 53 54

  def active?
    state == 'active'
  end

  def closed?
    state == 'closed'
  end

55
  def issues
56
    @issues ||= Issue.of_milestones(milestones.map(&:id)).includes(:project)
57 58
  end

59
  def merge_requests
60
    @merge_requests ||= MergeRequest.of_milestones(milestones.map(&:id)).includes(:target_project)
61 62 63
  end

  def participants
64
    @participants ||= milestones.map(&:participants).flatten.compact.uniq
65
  end
66

67
  def labels
68 69
    @labels ||= GlobalLabel.build_collection(milestones.map(&:labels).flatten)
                           .sort_by!(&:title)
70
  end
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

  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?
86
        "expired on #{due_date.to_s(:medium)}"
87
      else
88
        "expires on #{due_date.to_s(:medium)}"
89 90 91
      end
    end
  end
92
end