Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-04-30 00:47:55 +0300
committerRobert Speicher <rspeicher@gmail.com>2015-05-06 19:58:28 +0300
commite167f28549b80141165f42b567073c5dd55f80c5 (patch)
treee91e3786c94ad9e489d5ece6bdd6d9f9fc0337d0 /app
parentce29e5cd8f09212afd3274326917c364272a6e39 (diff)
Update Taskable to use TaskList
Diffstat (limited to 'app')
-rw-r--r--app/models/concerns/taskable.rb45
1 files changed, 15 insertions, 30 deletions
diff --git a/app/models/concerns/taskable.rb b/app/models/concerns/taskable.rb
index bbb3b301a9f..6e04578a7df 100644
--- a/app/models/concerns/taskable.rb
+++ b/app/models/concerns/taskable.rb
@@ -1,51 +1,36 @@
+require 'task_list'
+
# 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
- TASK_PATTERN_MD = /^(?<bullet> *[*-] *)\[(?<checked>[ xX])\]/.freeze
- TASK_PATTERN_HTML = /^<li>(?<p_tag>\s*<p>)?\[(?<checked>[ xX])\]/.freeze
-
- # Change the state of a task list item for this Taskable. Edit the object's
- # description by finding the nth task item and changing its checkbox
- # placeholder to "[x]" if +checked+ is true, or "[ ]" if it's false.
- # Note: task numbering starts with 1
- def update_nth_task(n, checked)
- index = 0
- check_char = checked ? 'x' : ' '
+ # Called by `TaskList::Summary`
+ def task_list_items
+ return [] if description.blank?
- # Do this instead of using #gsub! so that ActiveRecord detects that a field
- # has changed.
- self.description = self.description.gsub(TASK_PATTERN_MD) do |match|
- index += 1
- case index
- when n then "#{$LAST_MATCH_INFO[:bullet]}[#{check_char}]"
- else match
- end
+ @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}")
end
+ end
- save
+ def tasks
+ @tasks ||= TaskList.new(self)
end
# Return true if this object's description has any task list items.
def tasks?
- description && description.match(TASK_PATTERN_MD)
+ tasks.summary.items?
end
# Return a string that describes the current state of this Taskable's task
# list items, e.g. "20 tasks (12 done, 8 unfinished)"
def task_status
- return nil unless description
-
- num_tasks = 0
- num_done = 0
-
- description.scan(TASK_PATTERN_MD) do
- num_tasks += 1
- num_done += 1 unless $LAST_MATCH_INFO[:checked] == ' '
- end
+ return '' if description.blank?
- "#{num_tasks} tasks (#{num_done} done, #{num_tasks - num_done} unfinished)"
+ sum = tasks.summary
+ "#{sum.item_count} tasks (#{sum.complete_count} done, #{sum.incomplete_count} unfinished)"
end
end