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

207 lines
5.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
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-12-23 02:04:40 +05:30
MilestoneStruct = Struct.new(:title, :name, :id)
None = MilestoneStruct.new('No Milestone', 'No Milestone', 0)
Any = MilestoneStruct.new('Any Milestone', '', -1)
2016-06-02 11:05:42 +05:30
Upcoming = MilestoneStruct.new('Upcoming', '#upcoming', -2)
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
include Referable
2015-12-23 02:04:40 +05:30
include StripAttribute
2016-06-02 11:05:42 +05:30
include Milestoneish
2014-09-02 18:07:02 +05:30
belongs_to :project
has_many :issues
2016-04-02 18:10:28 +05:30
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
2014-09-02 18:07:02 +05:30
has_many :merge_requests
2016-06-02 11:05:42 +05:30
has_many :participants, -> { distinct.reorder('users.name') }, through: :issues, source: :assignee
2014-09-02 18:07:02 +05:30
scope :active, -> { with_state(:active) }
scope :closed, -> { with_state(:closed) }
scope :of_projects, ->(ids) { where(project_id: ids) }
2016-04-02 18:10:28 +05:30
validates :title, presence: true, uniqueness: { scope: :project_id }
2014-09-02 18:07:02 +05:30
validates :project, presence: true
2015-12-23 02:04:40 +05:30
strip_attributes :title
2014-09-02 18:07:02 +05:30
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
2016-06-02 11:05:42 +05:30
# Searches for milestones matching the given query.
#
# This method uses ILIKE on PostgreSQL and LIKE on MySQL.
#
# query - The search query as a String
#
# Returns an ActiveRecord::Relation.
2015-09-25 12:07:36 +05:30
def search(query)
2016-06-02 11:05:42 +05:30
t = arel_table
pattern = "%#{query}%"
where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
2015-09-25 12:07:36 +05:30
end
end
2016-06-02 11:05:42 +05:30
def self.reference_prefix
'%'
end
def self.reference_pattern
2016-06-02 11:05:42 +05:30
# NOTE: The iid pattern only matches when all characters on the expression
# are digits, so it will match %2 but not %2.1 because that's probably a
# milestone name and we want it to be matched as such.
@reference_pattern ||= %r{
(#{Project.reference_pattern})?
#{Regexp.escape(reference_prefix)}
(?:
(?<milestone_iid>
\d+(?!\S\w)\b # Integer-based milestone iid, or
) |
(?<milestone_name>
[^"\s]+\b | # String-based single-word milestone title, or
"[^"]+" # String-based multi-word milestone surrounded in quotes
)
)
}x
end
def self.link_reference_pattern
2016-06-02 11:05:42 +05:30
@link_reference_pattern ||= super("milestones", /(?<milestone>\d+)/)
end
2016-06-02 11:05:42 +05:30
def self.upcoming_ids_by_projects(projects)
rel = unscoped.of_projects(projects).active.where('due_date > ?', Time.now)
2016-06-02 11:05:42 +05:30
if Gitlab::Database.postgresql?
rel.order(:project_id, :due_date).select('DISTINCT ON (project_id) id')
else
rel.
group(:project_id).
having('due_date = MIN(due_date)').
pluck(:id, :project_id, :due_date).
map(&:first)
end
end
2016-06-02 11:05:42 +05:30
##
# Returns the String necessary to reference this Milestone in Markdown
#
# format - Symbol format to use (default: :iid, optional: :name)
#
# Examples:
#
# Milestone.first.to_reference # => "%1"
# Milestone.first.to_reference(format: :name) # => "%\"goal\""
# Milestone.first.to_reference(project) # => "gitlab-org/gitlab-ce%1"
#
def to_reference(from_project = nil, format: :iid)
format_reference = milestone_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
if cross_project_reference?(from_project)
project.to_reference + reference
else
reference
end
end
def reference_link_text(from_project = nil)
self.title
end
2014-09-02 18:07:02 +05:30
def expired?
if due_date
due_date.past?
else
false
end
end
def expires_at
if due_date
if due_date.past?
"expired on #{due_date.to_s(:medium)}"
2014-09-02 18:07:02 +05:30
else
"expires on #{due_date.to_s(:medium)}"
2014-09-02 18:07:02 +05:30
end
end
end
def can_be_closed?
active? && issues.opened.count.zero?
end
2016-06-02 11:05:42 +05:30
def is_empty?(user = nil)
total_items_count(user).zero?
2014-09-02 18:07:02 +05:30
end
def author_id
nil
end
2015-11-26 14:37:03 +05:30
2016-06-02 11:05:42 +05:30
def title=(value)
write_attribute(:title, Sanitize.clean(value.to_s)) if value.present?
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
2016-06-02 11:05:42 +05:30
private
def milestone_format_reference(format = :iid)
raise ArgumentError, 'Unknown format' unless [:iid, :name].include?(format)
if format == :name && !name.include?('"')
%("#{name}")
else
iid
end
end
2014-09-02 18:07:02 +05:30
end