gitlab-org--gitlab-foss/app/models/milestone.rb

94 lines
1.8 KiB
Ruby
Raw Normal View History

2012-11-19 13:24:05 -05:00
# == 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
# updated_at :datetime
2013-03-15 09:16:02 -04:00
# state :string(255)
2013-08-21 05:34:02 -04:00
# iid :integer
2012-11-19 13:24:05 -05:00
#
2012-04-08 17:28:58 -04:00
class Milestone < ActiveRecord::Base
include InternalId
2015-02-05 19:49:41 -05:00
include Sortable
2012-04-08 17:28:58 -04:00
belongs_to :project
has_many :issues
has_many :merge_requests
2013-04-02 18:28:12 -04:00
has_many :participants, through: :issues, source: :assignee
2012-04-08 17:28:58 -04:00
2013-02-18 04:10:09 -05:00
scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) }
scope :of_projects, ->(ids) { where(project_id: ids) }
2012-12-14 00:34:05 -05:00
2012-10-08 20:10:04 -04:00
validates :title, presence: true
validates :project, presence: true
2013-02-18 04:10:09 -05:00
2013-02-18 08:22:18 -05:00
state_machine :state, initial: :active do
2013-02-18 04:10:09 -05:00
event :close do
2013-02-18 08:22:18 -05:00
transition active: :closed
2013-02-18 04:10:09 -05:00
end
event :activate do
2013-02-18 08:22:18 -05:00
transition closed: :active
2013-02-18 04:10:09 -05:00
end
state :closed
state :active
end
2012-04-08 17:28:58 -04:00
2012-12-14 00:34:05 -05:00
def expired?
if due_date
due_date.past?
2012-12-14 00:34:05 -05:00
else
false
end
2012-04-08 17:28:58 -04:00
end
2012-10-29 17:45:11 -04:00
def open_items_count
self.issues.opened.count + self.merge_requests.opened.count
end
2012-10-29 17:45:11 -04:00
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
end
def percent_complete
2012-10-29 17:45:11 -04:00
((closed_items_count * 100) / total_items_count).abs
rescue ZeroDivisionError
100
2012-04-08 17:28:58 -04:00
end
def expires_at
if due_date
if due_date.past?
"expired at #{due_date.stamp("Aug 21, 2011")}"
else
2013-03-20 17:46:30 -04:00
"expires at #{due_date.stamp("Aug 21, 2011")}"
end
2013-03-20 17:46:30 -04:00
end
2012-04-08 17:28:58 -04:00
end
2012-12-14 00:34:05 -05:00
def can_be_closed?
2013-02-18 04:10:09 -05:00
active? && issues.opened.count.zero?
2012-12-18 22:14:05 -05:00
end
def is_empty?
total_items_count.zero?
2012-12-14 00:34:05 -05:00
end
def author_id
nil
end
2012-04-08 17:28:58 -04:00
end