gitlab-org--gitlab-foss/app/models/concerns/taskable.rb

63 lines
1.7 KiB
Ruby
Raw Normal View History

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
# 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
COMPLETED = 'completed'.freeze
INCOMPLETE = 'incomplete'.freeze
ITEM_PATTERN = /
^
(?:\s*[-+*]|(?:\d+\.))? # optional list prefix
\s* # optional whitespace prefix
(\[\s\]|\[[xX]\]) # checkbox
(\s.+) # followed by whitespace and some text.
/x
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-04-29 17:47:55 -04:00
# Called by `TaskList::Summary`
def task_list_items
return [] if description.blank?
@task_list_items ||= Taskable.get_tasks(description)
2015-04-29 17:47:55 -04:00
end
2015-04-29 17:47:55 -04:00
def tasks
@tasks ||= TaskList.new(self)
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?
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"
def task_status
2015-04-29 17:47:55 -04:00
return '' if description.blank?
2015-04-29 17:47:55 -04:00
sum = tasks.summary
2016-08-27 13:36:08 -04:00
"#{sum.complete_count} of #{sum.item_count} #{'task'.pluralize(sum.item_count)} completed"
end
end