Class: TodoFromRichMessage

Inherits:
Object
  • Object
show all
Defined in:
app/services/todo_from_rich_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, default_context_id, description, notes) ⇒ TodoFromRichMessage

Returns a new instance of TodoFromRichMessage.



4
5
6
7
8
9
# File 'app/services/todo_from_rich_message.rb', line 4

def initialize(user, default_context_id, description, notes)
  @user = user
  @default_context_id = default_context_id
  @description = description
  @notes = notes
end

Instance Attribute Details

#default_context_idObject (readonly)

Returns the value of attribute default_context_id.



2
3
4
# File 'app/services/todo_from_rich_message.rb', line 2

def default_context_id
  @default_context_id
end

#descriptionObject (readonly)

Returns the value of attribute description.



2
3
4
# File 'app/services/todo_from_rich_message.rb', line 2

def description
  @description
end

#notesObject (readonly)

Returns the value of attribute notes.



2
3
4
# File 'app/services/todo_from_rich_message.rb', line 2

def notes
  @notes
end

#userObject (readonly)

Returns the value of attribute user.



2
3
4
# File 'app/services/todo_from_rich_message.rb', line 2

def user
  @user
end

Instance Method Details

#constructObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/todo_from_rich_message.rb', line 11

def construct
  extractor   = RichMessageExtractor.new(description)
  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?
    # TODO: Should this use ILIKE on Postgres?
    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
  if project.present?
    if project[0..3].downcase == "new:"
      found_project = user.projects.build
      found_project.name = project[4..259].strip
      found_project.save!
    else
      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.description = description
  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? Time
  todo.due         = due if due.is_a? Time
  todo.tag_with tags unless tags.nil? || tags.empty?
  todo.starred     = star
  todo
end