global_milestone.rb 2 KB
Newer Older
1
class GlobalMilestone
2
  attr_accessor :title, :milestones
3 4
  alias_attribute :name, :title

5 6 7 8 9 10 11 12
  def self.build_collection(milestones)
    milestones = milestones.group_by(&:title)

    milestones.map do |title, milestones|
      new(title, milestones)
    end
  end

13 14 15 16 17
  def initialize(title, milestones)
    @title = title
    @milestones = milestones
  end

18
  def safe_title
19
    @title.to_slug.to_s
20
  end
21

22
  def projects
Marin Jankovski's avatar
Marin Jankovski committed
23
    milestones.map { |milestone| milestone.project }
24 25 26
  end

  def issue_count
Marin Jankovski's avatar
Marin Jankovski committed
27
    milestones.map { |milestone| milestone.issues.count }.sum
28 29 30
  end

  def merge_requests_count
Marin Jankovski's avatar
Marin Jankovski committed
31
    milestones.map { |milestone| milestone.merge_requests.count }.sum
32 33
  end

34
  def open_items_count
Marin Jankovski's avatar
Marin Jankovski committed
35
    milestones.map { |milestone| milestone.open_items_count }.sum
36 37
  end

38
  def closed_items_count
Marin Jankovski's avatar
Marin Jankovski committed
39
    milestones.map { |milestone| milestone.closed_items_count }.sum
40 41 42
  end

  def total_items_count
Marin Jankovski's avatar
Marin Jankovski committed
43
    milestones.map { |milestone| milestone.total_items_count }.sum
44 45 46 47 48
  end

  def percent_complete
    ((closed_items_count * 100) / total_items_count).abs
  rescue ZeroDivisionError
49
    0
50
  end
Marin Jankovski's avatar
Marin Jankovski committed
51 52

  def state
Marin Jankovski's avatar
Marin Jankovski committed
53
    state = milestones.map { |milestone| milestone.state }
54

55
    if state.count('closed') == state.size
Marin Jankovski's avatar
Marin Jankovski committed
56
      'closed'
57 58
    else
      'active'
Marin Jankovski's avatar
Marin Jankovski committed
59 60
    end
  end
61 62 63 64 65 66 67 68 69

  def active?
    state == 'active'
  end

  def closed?
    state == 'closed'
  end

70
  def issues
71
    @issues ||= milestones.map(&:issues).flatten.group_by(&:state)
72 73
  end

74
  def merge_requests
75
    @merge_requests ||= milestones.map(&:merge_requests).flatten.group_by(&:state)
76 77 78
  end

  def participants
79
    @participants ||= milestones.map(&:participants).flatten.compact.uniq
80
  end
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  def opened_issues
    issues.values_at("opened", "reopened").compact.flatten
  end

  def closed_issues
    issues['closed']
  end

  def opened_merge_requests
    merge_requests.values_at("opened", "reopened").compact.flatten
  end

  def closed_merge_requests
    merge_requests.values_at("closed", "merged", "locked").compact.flatten
  end
97 98 99 100

  def complete?
    total_items_count == closed_items_count
  end
101
end