debian-mirror-gitlab/app/models/milestone.rb

141 lines
3.1 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# == 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
# state :string(255)
# iid :integer
#
class Milestone < ActiveRecord::Base
2015-09-11 14:41:01 +05:30
# Represents a "No Milestone" state used for filtering Issues and Merge
# Requests that have no milestone assigned.
2015-10-24 18:46:33 +05:30
MilestoneStruct = Struct.new(:title, :name)
None = MilestoneStruct.new('No Milestone', 'No Milestone')
Any = MilestoneStruct.new('Any', '')
2015-09-11 14:41:01 +05:30
2014-09-02 18:07:02 +05:30
include InternalId
2015-04-26 12:48:37 +05:30
include Sortable
2014-09-02 18:07:02 +05:30
belongs_to :project
has_many :issues
has_many :merge_requests
has_many :participants, through: :issues, source: :assignee
scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) }
scope :of_projects, ->(ids) { where(project_id: ids) }
validates :title, presence: true
validates :project, presence: true
state_machine :state, initial: :active do
event :close do
transition active: :closed
end
event :activate do
transition closed: :active
end
state :closed
state :active
end
2015-10-24 18:46:33 +05:30
alias_attribute :name, :title
2015-09-25 12:07:36 +05:30
class << self
def search(query)
query = "%#{query}%"
where("title like ? or description like ?", query, query)
end
end
2014-09-02 18:07:02 +05:30
def expired?
if due_date
due_date.past?
else
false
end
end
def open_items_count
self.issues.opened.count + self.merge_requests.opened.count
end
def closed_items_count
2015-09-11 14:41:01 +05:30
self.issues.closed.count + self.merge_requests.closed_and_merged.count
2014-09-02 18:07:02 +05:30
end
def total_items_count
self.issues.count + self.merge_requests.count
end
def percent_complete
((closed_items_count * 100) / total_items_count).abs
rescue ZeroDivisionError
2015-09-11 14:41:01 +05:30
0
2014-09-02 18:07:02 +05:30
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
end
def can_be_closed?
active? && issues.opened.count.zero?
end
def is_empty?
total_items_count.zero?
end
def author_id
nil
end
2015-11-26 14:37:03 +05:30
# 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
2014-09-02 18:07:02 +05:30
end