milestone.rb 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# == 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
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12
#  state       :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
#  iid         :integer
14 15
#

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

19
  attr_accessible :title, :description, :due_date, :state_event
20

21 22
  belongs_to :project
  has_many :issues
23
  has_many :merge_requests
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
24
  has_many :participants, through: :issues, source: :assignee
25

Andrew8xx8's avatar
Andrew8xx8 committed
26 27
  scope :active, -> { with_state(:active) }
  scope :closed, -> { with_state(:closed) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
29 30
  validates :title, presence: true
  validates :project, presence: true
Andrew8xx8's avatar
Andrew8xx8 committed
31

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

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

    state :closed

    state :active
  end
45

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
46 47
  def expired?
    if due_date
48
      due_date.past?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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 69
  rescue ZeroDivisionError
    100
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
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
77
        "expires at #{due_date.stamp("Aug 21, 2011")}"
78
      end
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
79
    end
80
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
81 82

  def can_be_closed?
Andrew8xx8's avatar
Andrew8xx8 committed
83
    active? && issues.opened.count.zero?
84 85 86 87
  end

  def is_empty?
    total_items_count.zero?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
88 89
  end

90
  def author_id
91
    nil
92
  end
93
end