milestone.rb 1.22 KB
Newer Older
1
class Milestone < ActiveRecord::Base
2 3
  attr_accessible :title, :description, :due_date, :closed

4 5
  belongs_to :project
  has_many :issues
6
  has_many :merge_requests
7

Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
8 9
  validates :title, presence: true
  validates :project, presence: true
10 11

  def self.active
12
    where("due_date > ? OR due_date IS NULL", Date.today)
13 14
  end

15
  def participants
Nihad Abbasov's avatar
Nihad Abbasov committed
16
    User.where(id: issues.pluck(:assignee_id))
17 18
  end

19 20
  def open_items_count
    self.issues.opened.count + self.merge_requests.opened.count
21 22
  end

23 24 25 26 27 28
  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
29 30 31
  end

  def percent_complete
32
    ((closed_items_count * 100) / total_items_count).abs
33 34
  rescue ZeroDivisionError
    100
35 36 37 38 39 40
  end

  def expires_at
    "expires at #{due_date.stamp("Aug 21, 2011")}" if due_date
  end
end
Nihad Abbasov's avatar
Nihad Abbasov committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54

# == 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
#  closed      :boolean         default(FALSE), not null
#  created_at  :datetime        not null
#  updated_at  :datetime        not null
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
55