global_milestone.rb 1.9 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

31
  def projects
32
    @projects ||= Project.for_milestones(milestones.select(:id))
33 34
  end

Marin Jankovski's avatar
Marin Jankovski committed
35
  def state
36 37
    milestones.each do |milestone|
      return 'active' if milestone.state != 'closed'
Marin Jankovski's avatar
Marin Jankovski committed
38
    end
39 40

    'closed'
Marin Jankovski's avatar
Marin Jankovski committed
41
  end
42 43 44 45 46 47 48 49 50

  def active?
    state == 'active'
  end

  def closed?
    state == 'closed'
  end

51
  def issues
52
    @issues ||= Issue.of_milestones(milestones.select(:id)).includes(:project, :assignee, :labels)
53 54
  end

55
  def merge_requests
56
    @merge_requests ||= MergeRequest.of_milestones(milestones.select(:id)).includes(:target_project, :assignee, :labels)
57 58 59
  end

  def participants
60
    @participants ||= milestones.includes(:participants).map(&:participants).flatten.compact.uniq
61
  end
62

63
  def labels
64
    @labels ||= GlobalLabel.build_collection(milestones.includes(:labels).map(&:labels).flatten)
65
                           .sort_by!(&:title)
66
  end
67 68 69 70 71 72 73 74 75 76

  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
      end
  end

77 78 79 80 81 82
  def start_date
    return @start_date if defined?(@start_date)

    @start_date =
      if @milestones.all? { |x| x.start_date == @milestones.first.start_date }
        @milestones.first.start_date
83 84
      end
  end
85
end