Merge branch 'master' into new-gui

Conflicts:
	Gemfile.lock
This commit is contained in:
Reinier Balt 2014-01-07 21:01:55 +01:00
parent fa537fbeb0
commit eb1502d4e0
28 changed files with 385 additions and 221 deletions

View file

@ -1,28 +1,72 @@
require 'date'
class RichMessageExtractor
include ActionView::Helpers::SanitizeHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
RICH_MESSAGE_FIELDS_REGEX = /([^>@]*)@?([^>]*)>?(.*)/
PROJECT_MARKER = '~'
CONTEXT_MARKER = '@'
TICKLER_MARKER = '>'
DUE_MARKER = '<'
TAG_MARKER = '#'
STAR_MARKER = '*'
ALL_MARKERS = [
PROJECT_MARKER,
CONTEXT_MARKER,
TICKLER_MARKER,
DUE_MARKER,
TAG_MARKER,
STAR_MARKER
]
def initialize(message)
@message = message
end
def description
fields[1].strip
desc = select_for('')
desc.blank? ? '' : sanitize(desc[1].strip)
end
def context
fields[2].strip
context = select_for(CONTEXT_MARKER)
context.blank? ? '' : sanitize(context[1].strip)
end
def project
stripped = fields[3].strip
stripped.blank? ? nil : stripped
project = select_for PROJECT_MARKER
project.blank? ? nil : sanitize(project[1].strip)
end
def tags
string = @message.dup
tags = []
# Regex only matches one tag, so recurse until we have them all
while string.match /#(.*?)(?=[#{ALL_MARKERS.join}]|\Z)/
tags << sanitize($1)
string.gsub!(/##{$1}/,'')
end
tags.empty? ? nil : tags
end
def due
due = select_for DUE_MARKER
due.blank? ? nil : Date.parse(due[1].strip)
end
def show_from
show_from = select_for TICKLER_MARKER
show_from.blank? ? nil : Date.parse(show_from[1].strip)
end
def starred?
@message.include? '*'
end
private
def fields
@message.match(RICH_MESSAGE_FIELDS_REGEX)
end
def select_for symbol
@message.match /#{symbol}(.*?)(?=[#{ALL_MARKERS.join}]|\Z)/
end
end

View file

@ -14,6 +14,10 @@ class TodoFromRichMessage
description = extractor.description
context = extractor.context
project = extractor.project
show_from = extractor.show_from
due = extractor.due
tags = extractor.tags
star = extractor.starred?
context_id = default_context_id
if context.present?
@ -33,17 +37,21 @@ class TodoFromRichMessage
found_project.name = project[4..259].strip
found_project.save!
else
found_project = user.projects.active.find_by_namepart(project)
found_project = user.projects.find_by_namepart(project) if found_project.nil?
found_project = user.projects.active.with_namepart(project).first
found_project = user.projects.with_namepart(project).first if found_project.nil?
end
project_id = found_project.id unless found_project.nil?
end
todo = user.todos.build
todo = user.todos.build
todo.description = description
todo.raw_notes = notes
todo.context_id = context_id
todo.project_id = project_id unless project_id.nil?
todo.raw_notes = notes
todo.context_id = context_id
todo.project_id = project_id unless project_id.nil?
todo.show_from = show_from if show_from.is_a? Date
todo.due = due if due.is_a? Date
todo.tag_with tags unless tags.nil? || tags.empty?
todo.starred = star
todo
end
end