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
|
2014-04-09 08:05:03 -04:00
|
|
|
# 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
|
2015-06-28 16:12:32 -04:00
|
|
|
# Represents a "No Milestone" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no milestone assigned.
|
|
|
|
None = Struct.new(:title).new('No Milestone')
|
|
|
|
|
2013-08-21 05:16:26 -04:00
|
|
|
include InternalId
|
2015-02-05 19:49:41 -05:00
|
|
|
include Sortable
|
2013-08-21 05:16:26 -04:00
|
|
|
|
2012-04-08 17:28:58 -04:00
|
|
|
belongs_to :project
|
|
|
|
has_many :issues
|
2012-10-26 09:53:45 -04:00
|
|
|
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) }
|
2014-04-24 13:35:06 -04:00
|
|
|
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
|
|
|
|
2015-08-13 10:48:21 -04:00
|
|
|
class << self
|
|
|
|
def search(query)
|
|
|
|
query = "%#{query}%"
|
|
|
|
where("title like ? or description like ?", query, query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-14 00:34:05 -05:00
|
|
|
def expired?
|
|
|
|
if due_date
|
2012-12-23 11:18:27 -05:00
|
|
|
due_date.past?
|
2012-12-14 00:34:05 -05:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|
2015-08-31 00:51:34 -04:00
|
|
|
|
2012-10-29 17:45:11 -04:00
|
|
|
def open_items_count
|
|
|
|
self.issues.opened.count + self.merge_requests.opened.count
|
2012-10-29 16:40:00 -04:00
|
|
|
end
|
|
|
|
|
2012-10-29 17:45:11 -04:00
|
|
|
def closed_items_count
|
2015-06-18 13:15:17 -04:00
|
|
|
self.issues.closed.count + self.merge_requests.closed_and_merged.count
|
2012-10-29 17:45:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def total_items_count
|
|
|
|
self.issues.count + self.merge_requests.count
|
2012-10-29 16:40:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def percent_complete
|
2012-10-29 17:45:11 -04:00
|
|
|
((closed_items_count * 100) / total_items_count).abs
|
2012-08-25 13:54:38 -04:00
|
|
|
rescue ZeroDivisionError
|
2015-05-26 14:44:04 -04:00
|
|
|
0
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def expires_at
|
2012-12-23 11:18:27 -05:00
|
|
|
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")}"
|
2012-12-23 11:18:27 -05:00
|
|
|
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
|
|
|
|
|
2012-12-14 15:05:10 -05:00
|
|
|
def author_id
|
2014-03-25 08:01:52 -04:00
|
|
|
nil
|
2012-12-14 15:05:10 -05:00
|
|
|
end
|
2012-04-08 17:28:58 -04:00
|
|
|
end
|