2013-07-18 16:35:51 -05:00
|
|
|
class TodoFromRichMessage
|
|
|
|
|
|
|
|
attr_reader :user, :default_context_id, :description, :notes
|
|
|
|
|
|
|
|
def initialize(user, default_context_id, description, notes)
|
|
|
|
@user = user
|
|
|
|
@default_context_id = default_context_id
|
|
|
|
@description = description
|
|
|
|
@notes = notes
|
|
|
|
end
|
|
|
|
|
|
|
|
def construct
|
|
|
|
extractor = RichMessageExtractor.new(description)
|
|
|
|
description = extractor.description
|
|
|
|
context = extractor.context
|
|
|
|
project = extractor.project
|
2013-10-13 22:11:55 +01:00
|
|
|
show_from = extractor.show_from
|
|
|
|
due = extractor.due
|
|
|
|
tags = extractor.tags
|
|
|
|
star = extractor.starred?
|
2013-07-18 16:35:51 -05:00
|
|
|
|
|
|
|
context_id = default_context_id
|
2013-09-13 15:40:09 +03:00
|
|
|
if context.present?
|
2013-07-18 16:35:51 -05:00
|
|
|
found_context = user.contexts.active.where("name like ?", "%#{context}%").first
|
|
|
|
found_context = user.contexts.where("name like ?", "%#{context}%").first if !found_context
|
|
|
|
context_id = found_context.id if found_context
|
|
|
|
end
|
|
|
|
|
|
|
|
unless user.context_ids.include? context_id
|
|
|
|
raise(CannotAccessContext, "Cannot access a context that does not belong to this user.")
|
|
|
|
end
|
|
|
|
|
|
|
|
project_id = nil
|
2013-09-13 15:40:09 +03:00
|
|
|
if project.present?
|
2013-07-18 16:35:51 -05:00
|
|
|
if project[0..3].downcase == "new:"
|
|
|
|
found_project = user.projects.build
|
|
|
|
found_project.name = project[4..259].strip
|
|
|
|
found_project.save!
|
|
|
|
else
|
2013-10-13 22:11:55 +01:00
|
|
|
found_project = user.projects.active.with_namepart(project).first
|
|
|
|
found_project = user.projects.with_namepart(project).first if found_project.nil?
|
2013-07-18 16:35:51 -05:00
|
|
|
end
|
|
|
|
project_id = found_project.id unless found_project.nil?
|
|
|
|
end
|
|
|
|
|
2013-10-13 22:11:55 +01:00
|
|
|
todo = user.todos.build
|
2013-07-18 16:35:51 -05:00
|
|
|
todo.description = description
|
2013-10-13 22:11:55 +01:00
|
|
|
todo.raw_notes = notes
|
|
|
|
todo.context_id = context_id
|
|
|
|
todo.project_id = project_id unless project_id.nil?
|
2014-01-26 15:05:27 +01:00
|
|
|
todo.show_from = show_from if show_from.is_a? Time
|
|
|
|
todo.due = due if due.is_a? Time
|
2013-10-13 22:11:55 +01:00
|
|
|
todo.tag_with tags unless tags.nil? || tags.empty?
|
|
|
|
todo.starred = star
|
2013-07-18 16:35:51 -05:00
|
|
|
todo
|
|
|
|
end
|
|
|
|
end
|