2020-04-30 20:09:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Timebox
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
include CacheMarkdownField
|
2020-05-05 17:09:42 -04:00
|
|
|
include Gitlab::SQL::Pattern
|
2020-05-26 17:07:45 -04:00
|
|
|
include Referable
|
2020-04-30 20:09:59 -04:00
|
|
|
include StripAttribute
|
|
|
|
|
2021-11-09 13:13:13 -05:00
|
|
|
TimeboxStruct = Struct.new(:title, :name, :id, :class_name) do
|
2020-05-05 17:09:42 -04:00
|
|
|
# Ensure these models match the interface required for exporting
|
|
|
|
def serializable_hash(_opts = {})
|
|
|
|
{ title: title, name: name, id: id }
|
|
|
|
end
|
2020-12-14 10:09:40 -05:00
|
|
|
|
|
|
|
def self.declarative_policy_class
|
|
|
|
"TimeboxPolicy"
|
|
|
|
end
|
2021-11-09 13:13:13 -05:00
|
|
|
|
|
|
|
def to_global_id
|
|
|
|
::Gitlab::GlobalId.build(self, model_name: class_name, id: id)
|
|
|
|
end
|
2020-05-05 17:09:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Represents a "No Timebox" state used for filtering Issues and Merge
|
|
|
|
# Requests that have no timeboxes assigned.
|
|
|
|
None = TimeboxStruct.new('No Timebox', 'No Timebox', 0)
|
|
|
|
Any = TimeboxStruct.new('Any Timebox', '', -1)
|
|
|
|
Upcoming = TimeboxStruct.new('Upcoming', '#upcoming', -2)
|
|
|
|
Started = TimeboxStruct.new('Started', '#started', -3)
|
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
included do
|
2020-05-05 17:09:42 -04:00
|
|
|
# Defines the same constants above, but inside the including class.
|
2021-11-09 13:13:13 -05:00
|
|
|
const_set :None, TimeboxStruct.new("No #{self.name}", "No #{self.name}", 0, self.name)
|
|
|
|
const_set :Any, TimeboxStruct.new("Any #{self.name}", '', -1, self.name)
|
|
|
|
const_set :Upcoming, TimeboxStruct.new('Upcoming', '#upcoming', -2, self.name)
|
|
|
|
const_set :Started, TimeboxStruct.new('Started', '#started', -3, self.name)
|
2020-05-05 17:09:42 -04:00
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
alias_method :timebox_id, :id
|
|
|
|
|
|
|
|
validate :start_date_should_be_less_than_due_date, if: proc { |m| m.start_date.present? && m.due_date.present? }
|
|
|
|
validate :dates_within_4_digits
|
|
|
|
|
|
|
|
cache_markdown_field :title, pipeline: :single_line
|
2022-01-26 01:13:36 -05:00
|
|
|
cache_markdown_field :description, issuable_reference_expansion_enabled: true
|
2020-04-30 20:09:59 -04:00
|
|
|
|
|
|
|
has_many :issues
|
|
|
|
has_many :labels, -> { distinct.reorder('labels.title') }, through: :issues
|
|
|
|
has_many :merge_requests
|
|
|
|
|
|
|
|
scope :closed, -> { with_state(:closed) }
|
2020-05-05 17:09:42 -04:00
|
|
|
scope :with_title, -> (title) { where(title: title) }
|
2020-04-30 20:09:59 -04:00
|
|
|
|
2020-10-13 23:08:32 -04:00
|
|
|
# A timebox is within the timeframe (start_date, end_date) if it overlaps
|
|
|
|
# with that timeframe:
|
|
|
|
#
|
|
|
|
# [ timeframe ]
|
|
|
|
# ----| ................ # Not overlapping
|
|
|
|
# |--| ................ # Not overlapping
|
|
|
|
# ------|............... # Overlapping
|
|
|
|
# -----------------------| # Overlapping
|
|
|
|
# ---------|............ # Overlapping
|
|
|
|
# |-----|............ # Overlapping
|
|
|
|
# |--------------| # Overlapping
|
|
|
|
# |--------------------| # Overlapping
|
|
|
|
# ...|-----|...... # Overlapping
|
|
|
|
# .........|-----| # Overlapping
|
|
|
|
# .........|--------- # Overlapping
|
|
|
|
# |-------------------- # Overlapping
|
|
|
|
# .........|--------| # Overlapping
|
|
|
|
# ...............|--| # Overlapping
|
|
|
|
# ............... |-| # Not Overlapping
|
|
|
|
# ............... |-- # Not Overlapping
|
|
|
|
#
|
|
|
|
# where: . = in timeframe
|
|
|
|
# ---| no start
|
|
|
|
# |--- no end
|
|
|
|
# |--| defined start and end
|
|
|
|
#
|
2020-04-30 20:09:59 -04:00
|
|
|
scope :within_timeframe, -> (start_date, end_date) do
|
|
|
|
where('start_date is not NULL or due_date is not NULL')
|
2020-09-08 05:08:31 -04:00
|
|
|
.where('start_date is NULL or start_date <= ?', end_date)
|
|
|
|
.where('due_date is NULL or due_date >= ?', start_date)
|
2020-04-30 20:09:59 -04:00
|
|
|
end
|
|
|
|
|
2021-08-16 20:10:22 -04:00
|
|
|
strip_attributes! :title
|
2020-04-30 20:09:59 -04:00
|
|
|
|
|
|
|
alias_attribute :name, :title
|
|
|
|
end
|
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
class_methods do
|
|
|
|
# Searches for timeboxes with a matching title or description.
|
|
|
|
#
|
|
|
|
# This method uses ILIKE on PostgreSQL
|
|
|
|
#
|
|
|
|
# query - The search query as a String
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
|
|
|
def search(query)
|
|
|
|
fuzzy_search(query, [:title, :description])
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_state(timeboxes, state)
|
|
|
|
case state
|
|
|
|
when 'closed' then timeboxes.closed
|
|
|
|
when 'all' then timeboxes
|
|
|
|
else timeboxes.active
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def predefined_id?(id)
|
|
|
|
[Any.id, None.id, Upcoming.id, Started.id].include?(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def predefined?(timebox)
|
|
|
|
predefined_id?(timebox&.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-30 05:09:43 -04:00
|
|
|
def to_reference
|
|
|
|
raise NotImplementedError
|
2020-05-26 17:07:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reference_link_text(from = nil)
|
|
|
|
self.class.reference_prefix + self.title
|
|
|
|
end
|
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
def title=(value)
|
|
|
|
write_attribute(:title, sanitize_title(value)) if value.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def timebox_name
|
|
|
|
model_name.singular
|
|
|
|
end
|
|
|
|
|
|
|
|
def safe_title
|
|
|
|
title.to_slug.normalize.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def resource_parent
|
2022-09-30 05:09:43 -04:00
|
|
|
raise NotImplementedError
|
2020-04-30 20:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_ability_name
|
|
|
|
model_name.singular
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_requests_enabled?
|
2022-09-30 05:09:43 -04:00
|
|
|
raise NotImplementedError
|
2020-04-30 20:09:59 -04:00
|
|
|
end
|
|
|
|
|
2020-09-08 05:08:31 -04:00
|
|
|
def weight_available?
|
|
|
|
resource_parent&.feature_available?(:issue_weights)
|
|
|
|
end
|
|
|
|
|
2020-04-30 20:09:59 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def start_date_should_be_less_than_due_date
|
|
|
|
if due_date <= start_date
|
|
|
|
errors.add(:due_date, _("must be greater than start date"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def dates_within_4_digits
|
|
|
|
if start_date && start_date > Date.new(9999, 12, 31)
|
|
|
|
errors.add(:start_date, _("date must not be after 9999-12-31"))
|
|
|
|
end
|
|
|
|
|
|
|
|
if due_date && due_date > Date.new(9999, 12, 31)
|
|
|
|
errors.add(:due_date, _("date must not be after 9999-12-31"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sanitize_title(value)
|
|
|
|
CGI.unescape_html(Sanitize.clean(value.to_s))
|
|
|
|
end
|
|
|
|
end
|