2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-29 17:47:55 -04:00
|
|
|
require 'task_list'
|
2015-06-10 01:38:45 -04:00
|
|
|
require 'task_list/filter'
|
2015-04-29 17:47:55 -04:00
|
|
|
|
2014-10-05 01:53:44 -04:00
|
|
|
# Contains functionality for objects that can have task lists in their
|
|
|
|
# descriptions. Task list items can be added with Markdown like "* [x] Fix
|
|
|
|
# bugs".
|
|
|
|
#
|
|
|
|
# Used by MergeRequest and Issue
|
|
|
|
module Taskable
|
2019-08-31 15:57:00 -04:00
|
|
|
COMPLETED = 'completed'
|
|
|
|
INCOMPLETE = 'incomplete'
|
2019-01-22 19:05:38 -05:00
|
|
|
COMPLETE_PATTERN = /(\[[xX]\])/.freeze
|
2021-04-26 14:09:45 -04:00
|
|
|
INCOMPLETE_PATTERN = /(\[\s\])/.freeze
|
2019-01-22 19:05:38 -05:00
|
|
|
ITEM_PATTERN = %r{
|
2015-10-22 11:18:59 -04:00
|
|
|
^
|
2019-12-19 13:07:48 -05:00
|
|
|
(?:(?:>\s{0,4})*) # optional blockquote characters
|
|
|
|
(?:\s*(?:[-+*]|(?:\d+\.)))+ # list prefix (one or more) required - task item has to be always in a list
|
|
|
|
\s+ # whitespace prefix has to be always presented for a list item
|
|
|
|
(\[\s\]|\[[xX]\]) # checkbox
|
|
|
|
(\s.+) # followed by whitespace and some text.
|
2019-05-05 06:19:14 -04:00
|
|
|
}x.freeze
|
2015-10-22 11:18:59 -04:00
|
|
|
|
|
|
|
def self.get_tasks(content)
|
|
|
|
content.to_s.scan(ITEM_PATTERN).map do |checkbox, label|
|
|
|
|
# ITEM_PATTERN strips out the hyphen, but Item requires it. Rabble rabble.
|
|
|
|
TaskList::Item.new("- #{checkbox}", label.strip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_updated_tasks(old_content:, new_content:)
|
2021-04-19 17:09:27 -04:00
|
|
|
old_tasks = get_tasks(old_content)
|
|
|
|
new_tasks = get_tasks(new_content)
|
2015-10-22 11:18:59 -04:00
|
|
|
|
|
|
|
new_tasks.select.with_index do |new_task, i|
|
|
|
|
old_task = old_tasks[i]
|
|
|
|
next unless old_task
|
|
|
|
|
2015-11-20 13:58:45 -05:00
|
|
|
new_task.source == old_task.source && new_task.complete? != old_task.complete?
|
2015-10-22 11:18:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-29 17:47:55 -04:00
|
|
|
# Called by `TaskList::Summary`
|
|
|
|
def task_list_items
|
|
|
|
return [] if description.blank?
|
2014-10-05 01:53:44 -04:00
|
|
|
|
2017-11-22 02:50:36 -05:00
|
|
|
@task_list_items ||= Taskable.get_tasks(description) # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
2015-04-29 17:47:55 -04:00
|
|
|
end
|
2014-10-05 01:53:44 -04:00
|
|
|
|
2015-04-29 17:47:55 -04:00
|
|
|
def tasks
|
|
|
|
@tasks ||= TaskList.new(self)
|
2014-10-05 01:53:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return true if this object's description has any task list items.
|
|
|
|
def tasks?
|
2015-04-29 17:47:55 -04:00
|
|
|
tasks.summary.items?
|
2014-10-05 01:53:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return a string that describes the current state of this Taskable's task
|
2016-08-27 13:36:08 -04:00
|
|
|
# list items, e.g. "12 of 20 tasks completed"
|
2016-09-26 17:36:11 -04:00
|
|
|
def task_status(short: false)
|
2015-04-29 17:47:55 -04:00
|
|
|
return '' if description.blank?
|
2014-10-05 01:53:44 -04:00
|
|
|
|
2016-09-26 17:36:11 -04:00
|
|
|
prep, completed = if short
|
|
|
|
['/', '']
|
|
|
|
else
|
|
|
|
[' of ', ' completed']
|
|
|
|
end
|
|
|
|
|
2015-04-29 17:47:55 -04:00
|
|
|
sum = tasks.summary
|
2016-09-26 17:36:11 -04:00
|
|
|
"#{sum.complete_count}#{prep}#{sum.item_count} #{'task'.pluralize(sum.item_count)}#{completed}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return a short string that describes the current state of this Taskable's
|
|
|
|
# task list items -- for small screens
|
|
|
|
def task_status_short
|
|
|
|
task_status(short: true)
|
2014-10-05 01:53:44 -04:00
|
|
|
end
|
2019-06-07 05:15:55 -04:00
|
|
|
|
|
|
|
def task_completion_status
|
|
|
|
@task_completion_status ||= {
|
|
|
|
count: tasks.summary.item_count,
|
|
|
|
completed_count: tasks.summary.complete_count
|
|
|
|
}
|
|
|
|
end
|
2014-10-05 01:53:44 -04:00
|
|
|
end
|