BigW Consortium Gitlab

taskable.rb 1.04 KB
Newer Older
1
require 'task_list'
Robert Speicher committed
2
require 'task_list/filter'
3

4 5 6 7 8 9
# 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
10 11 12
  # Called by `TaskList::Summary`
  def task_list_items
    return [] if description.blank?
13

14 15 16
    @task_list_items ||= description.scan(TaskList::Filter::ItemPattern).collect do |item|
      # ItemPattern strips out the hyphen, but Item requires it. Rabble rabble.
      TaskList::Item.new("- #{item}")
17
    end
18
  end
19

20 21
  def tasks
    @tasks ||= TaskList.new(self)
22 23 24 25
  end

  # Return true if this object's description has any task list items.
  def tasks?
26
    tasks.summary.items?
27 28 29
  end

  # Return a string that describes the current state of this Taskable's task
30
  # list items, e.g. "20 tasks (12 completed, 8 remaining)"
31
  def task_status
32
    return '' if description.blank?
33

34
    sum = tasks.summary
35
    "#{sum.item_count} tasks (#{sum.complete_count} completed, #{sum.incomplete_count} remaining)"
36 37
  end
end