gitlabhq/app/models/milestone.rb

95 lines
1.9 KiB
Ruby
Raw Normal View History

2012-11-19 19:24:05 +01: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
# closed :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#
2012-04-08 23:28:58 +02:00
class Milestone < ActiveRecord::Base
2013-02-18 10:10:09 +01:00
attr_accessible :title, :description, :due_date, :state_event, :author_id_of_changes
attr_accessor :author_id_of_changes
2012-04-08 23:28:58 +02:00
belongs_to :project
has_many :issues
has_many :merge_requests
2012-04-08 23:28:58 +02:00
2013-02-18 10:10:09 +01:00
scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) }
2012-12-14 06:34:05 +01:00
2012-10-09 02:10:04 +02:00
validates :title, presence: true
validates :project, presence: true
2013-02-18 10:10:09 +01:00
2013-02-18 14:22:18 +01:00
state_machine :state, initial: :active do
2013-02-18 10:10:09 +01:00
event :close do
2013-02-18 14:22:18 +01:00
transition active: :closed
2013-02-18 10:10:09 +01:00
end
event :activate do
2013-02-18 14:22:18 +01:00
transition closed: :active
2013-02-18 10:10:09 +01:00
end
state :closed
state :active
end
2012-04-08 23:28:58 +02:00
2012-12-14 06:34:05 +01:00
def expired?
if due_date
due_date.past?
2012-12-14 06:34:05 +01:00
else
false
end
2012-04-08 23:28:58 +02:00
end
def participants
2012-09-27 08:28:10 +02:00
User.where(id: issues.pluck(:assignee_id))
end
2012-10-29 22:45:11 +01:00
def open_items_count
self.issues.opened.count + self.merge_requests.opened.count
end
2012-10-29 22:45:11 +01: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 22:45:11 +01:00
((closed_items_count * 100) / total_items_count).abs
rescue ZeroDivisionError
100
2012-04-08 23:28:58 +02:00
end
def expires_at
if due_date
if due_date.past?
"expired at #{due_date.stamp("Aug 21, 2011")}"
else
"expires at #{due_date.stamp("Aug 21, 2011")}"
end
end
2012-04-08 23:28:58 +02:00
end
2012-12-14 06:34:05 +01:00
def can_be_closed?
2013-02-18 10:10:09 +01:00
active? && issues.opened.count.zero?
2012-12-19 04:14:05 +01:00
end
def is_empty?
total_items_count.zero?
2012-12-14 06:34:05 +01:00
end
def author_id
author_id_of_changes
end
2012-04-08 23:28:58 +02:00
end