BigW Consortium Gitlab

milestone.rb 1.78 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
  include InternalId
18
  include Sortable
19

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

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

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

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

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

    state :closed

    state :active
  end
45

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

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

58 59 60 61 62 63
  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
64 65 66
  end

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

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

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

  def is_empty?
    total_items_count.zero?
88 89
  end

90
  def author_id
91
    nil
92
  end
93
end