debian-mirror-gitlab/app/models/concerns/taskable.rb

87 lines
2.6 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require 'task_list'
require 'task_list/filter'
2015-04-26 12:48:37 +05:30
# 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-12-04 20:38:33 +05:30
COMPLETED = 'completed'
INCOMPLETE = 'incomplete'
2019-03-02 22:35:43 +05:30
COMPLETE_PATTERN = /(\[[xX]\])/.freeze
INCOMPLETE_PATTERN = /(\[[\s]\])/.freeze
ITEM_PATTERN = %r{
2015-12-23 02:04:40 +05:30
^
2020-03-13 15:44:24 +05:30
(?:(?:>\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-07-31 22:56:46 +05:30
}x.freeze
2015-12-23 02:04:40 +05:30
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:)
old_tasks, new_tasks = get_tasks(old_content), get_tasks(new_content)
new_tasks.select.with_index do |new_task, i|
old_task = old_tasks[i]
next unless old_task
new_task.source == old_task.source && new_task.complete? != old_task.complete?
end
end
2015-09-11 14:41:01 +05:30
# Called by `TaskList::Summary`
def task_list_items
return [] if description.blank?
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
@task_list_items ||= Taskable.get_tasks(description) # rubocop:disable Gitlab/ModuleWithInstanceVariables
2015-09-11 14:41:01 +05:30
end
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
def tasks
@tasks ||= TaskList.new(self)
2015-04-26 12:48:37 +05:30
end
# Return true if this object's description has any task list items.
def tasks?
2015-09-11 14:41:01 +05:30
tasks.summary.items?
2015-04-26 12:48:37 +05:30
end
# Return a string that describes the current state of this Taskable's task
2016-09-29 09:46:39 +05:30
# list items, e.g. "12 of 20 tasks completed"
2017-08-17 22:00:37 +05:30
def task_status(short: false)
2015-09-11 14:41:01 +05:30
return '' if description.blank?
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
prep, completed = if short
['/', '']
else
[' of ', ' completed']
end
2015-09-11 14:41:01 +05:30
sum = tasks.summary
2017-08-17 22:00:37 +05:30
"#{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)
2015-04-26 12:48:37 +05:30
end
2019-09-04 21:01:54 +05:30
def task_completion_status
@task_completion_status ||= {
count: tasks.summary.item_count,
completed_count: tasks.summary.complete_count
}
end
2015-04-26 12:48:37 +05:30
end