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

109 lines
2.2 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
# Represents a "No Milestone" state used for filtering Issues and Merge
# Requests that have no milestone assigned.
MilestoneStruct = Struct.new(:title, :name)
None = MilestoneStruct.new('No Milestone', 'No Milestone')
Any = MilestoneStruct.new('Any', '')
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
alias_attribute :name, :title
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
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_and_merged.count
2012-10-29 17:45:11 -04:00
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
0
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