BigW Consortium Gitlab

milestone.rb 1.76 KB
Newer Older
1 2 3 4 5 6 7 8 9
# == Schema Information
#
# Table name: milestones
#
#  id          :integer          not null, primary key
#  title       :string(255)      not null
#  project_id  :integer          not null
#  description :text
#  due_date    :date
Dmitriy Zaporozhets committed
10 11
#  created_at  :datetime
#  updated_at  :datetime
Dmitriy Zaporozhets committed
12
#  state       :string(255)
Dmitriy Zaporozhets committed
13
#  iid         :integer
14 15
#

16
class Milestone < ActiveRecord::Base
17 18
  include InternalId

19 20
  belongs_to :project
  has_many :issues
21
  has_many :merge_requests
Andrey Kumanyaev committed
22
  has_many :participants, through: :issues, source: :assignee
23

24 25
  scope :active, -> { with_state(:active) }
  scope :closed, -> { with_state(:closed) }
26
  scope :of_projects, ->(ids) { where(project_id: ids) }
27

Andrey Kumanyaev committed
28 29
  validates :title, presence: true
  validates :project, presence: true
30

Andrew8xx8 committed
31
  state_machine :state, initial: :active do
32
    event :close do
Andrew8xx8 committed
33
      transition active: :closed
34 35 36
    end

    event :activate do
Andrew8xx8 committed
37
      transition closed: :active
38 39 40 41 42 43
    end

    state :closed

    state :active
  end
44

45 46
  def expired?
    if due_date
47
      due_date.past?
48 49 50
    else
      false
    end
51 52
  end

53 54
  def open_items_count
    self.issues.opened.count + self.merge_requests.opened.count
55 56
  end

57 58 59 60 61 62
  def closed_items_count
    self.issues.closed.count + self.merge_requests.closed.count
  end

  def total_items_count
    self.issues.count + self.merge_requests.count
63 64 65
  end

  def percent_complete
66
    ((closed_items_count * 100) / total_items_count).abs
67 68
  rescue ZeroDivisionError
    100
69 70 71
  end

  def expires_at
72 73 74 75
    if due_date
      if due_date.past?
        "expired at #{due_date.stamp("Aug 21, 2011")}"
      else
76
        "expires at #{due_date.stamp("Aug 21, 2011")}"
77
      end
78
    end
79
  end
80 81

  def can_be_closed?
82
    active? && issues.opened.count.zero?
83 84 85 86
  end

  def is_empty?
    total_items_count.zero?
87 88
  end

89
  def author_id
90
    nil
91
  end
92
end