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

162 lines
3.6 KiB
Ruby
Raw Normal View History

2012-11-19 18:24:05 +00: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 13:16:02 +00:00
# state :string(255)
2013-08-21 09:34:02 +00:00
# iid :integer
2012-11-19 18:24:05 +00:00
#
2012-04-08 21:28:58 +00: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, :id)
None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
Any = MilestoneStruct.new('Any Milestone', '', -1)
include InternalId
2015-02-06 00:49:41 +00:00
include Sortable
2015-12-24 13:43:07 +00:00
include Referable
include StripAttribute
2012-04-08 21:28:58 +00:00
belongs_to :project
has_many :issues
has_many :merge_requests
2013-04-02 22:28:12 +00:00
has_many :participants, through: :issues, source: :assignee
2012-04-08 21:28:58 +00:00
2013-02-18 09:10:09 +00:00
scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) }
scope :of_projects, ->(ids) { where(project_id: ids) }
2012-12-14 05:34:05 +00:00
2012-10-09 00:10:04 +00:00
validates :title, presence: true
validates :project, presence: true
2013-02-18 09:10:09 +00:00
strip_attributes :title
2013-02-18 13:22:18 +00:00
state_machine :state, initial: :active do
2013-02-18 09:10:09 +00:00
event :close do
2013-02-18 13:22:18 +00:00
transition active: :closed
2013-02-18 09:10:09 +00:00
end
event :activate do
2013-02-18 13:22:18 +00:00
transition closed: :active
2013-02-18 09:10:09 +00:00
end
state :closed
state :active
end
2012-04-08 21:28:58 +00:00
alias_attribute :name, :title
2015-08-13 14:48:21 +00:00
class << self
def search(query)
query = "%#{query}%"
where("title like ? or description like ?", query, query)
end
end
2015-12-24 13:43:07 +00:00
def self.reference_pattern
nil
end
def self.link_reference_pattern
super("milestones", /(?<milestone>\d+)/)
end
def to_reference(from_project = nil)
h = Gitlab::Application.routes.url_helpers
h.namespace_project_milestone_url(self.project.namespace, self.project, self)
end
def reference_link_text(from_project = nil)
%Q{<i class="fa fa-clock-o"></i> }.html_safe + self.title
end
2012-12-14 05:34:05 +00:00
def expired?
if due_date
due_date.past?
2012-12-14 05:34:05 +00:00
else
false
end
2012-04-08 21:28:58 +00:00
end
2012-10-29 21:45:11 +00:00
def open_items_count
self.issues.opened.count + self.merge_requests.opened.count
end
2012-10-29 21:45:11 +00:00
def closed_items_count
self.issues.closed.count + self.merge_requests.closed_and_merged.count
2012-10-29 21:45:11 +00:00
end
def total_items_count
self.issues.count + self.merge_requests.count
end
def percent_complete
2012-10-29 21:45:11 +00:00
((closed_items_count * 100) / total_items_count).abs
rescue ZeroDivisionError
0
2012-04-08 21:28:58 +00:00
end
def expires_at
if due_date
if due_date.past?
"expired at #{due_date.stamp("Aug 21, 2011")}"
else
2013-03-20 21:46:30 +00:00
"expires at #{due_date.stamp("Aug 21, 2011")}"
end
2013-03-20 21:46:30 +00:00
end
2012-04-08 21:28:58 +00:00
end
2012-12-14 05:34:05 +00:00
def can_be_closed?
2013-02-18 09:10:09 +00:00
active? && issues.opened.count.zero?
2012-12-19 03:14:05 +00:00
end
def is_empty?
total_items_count.zero?
2012-12-14 05:34:05 +00:00
end
def author_id
nil
end
# Sorts the issues for the given IDs.
#
# This method runs a single SQL query using a CASE statement to update the
# position of all issues in the current milestone (scoped to the list of IDs).
#
# Given the ids [10, 20, 30] this method produces a SQL query something like
# the following:
#
# UPDATE issues
# SET position = CASE
# WHEN id = 10 THEN 1
# WHEN id = 20 THEN 2
# WHEN id = 30 THEN 3
# ELSE position
# END
# WHERE id IN (10, 20, 30);
#
# This method expects that the IDs given in `ids` are already Fixnums.
def sort_issues(ids)
pairs = []
ids.each_with_index do |id, index|
pairs << id
pairs << index + 1
end
conditions = 'WHEN id = ? THEN ? ' * ids.length
issues.where(id: ids).
update_all(["position = CASE #{conditions} ELSE position END", *pairs])
end
2012-04-08 21:28:58 +00:00
end