Class: Tempo::Controllers::Update

Inherits:
Base
  • Object
show all
Defined in:
lib/tempo/controllers/update_controller.rb

Class Method Summary collapse

Methods inherited from Base

filter_projects_by_title, fuzzy_match, reassemble_the

Class Method Details

.parse(options, args) ⇒ Object



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tempo/controllers/update_controller.rb', line 11

def parse options, args

  reassemble_the args

  return Views.project_assistance if Model::Project.index.empty?

  if options[:on]
    day = Time.parse options[:on]
    return Views.no_match_error( "valid timeframe", options[:from], false ) if day.nil?
    @time_records.load_day_record day
  else
    day = @time_records.load_last_day
  end

  if options[:id]
    record = @time_records.find_by_id( options[:id], day )
    return Views.no_match_error( "time record on #{day.strftime('%m/%d/%Y')}", "id = #{options[:id]}", false ) if !record
  else
    record = @time_records.index.last
    return Views.no_items( "time records on #{day.strftime('%m/%d/%Y')}", :error ) if ! record
  end


  if options[:delete]
    record.delete
    @time_records.save_to_file
    Views.delete_time_record_view record

  else
    if options[:start]
      start_time = Time.parse options[:start]
      return Views.no_match_error( "valid timeframe", options[:at], false ) if start_time.nil?

      # TODO: add "today " to start time and try again if not valid
      if record.valid_start_time? start_time
        record.start_time = start_time
      else
        return Views::ViewRecords::Message.new "cannot change start time to #{start_time.strftime('%H:%M')}", category: :error
      end
    end

    if options[:end]
      end_time = Time.parse options[:end]
      return Views.no_match_error( "valid timeframe", options[:at], false ) if end_time.nil?

      # TODO: add "today " to end time and try again if not valid
      if record.valid_end_time? end_time
        record.end_time = end_time
      else
        return Views::ViewRecords::Message.new "cannot change end time to #{end_time.strftime('%H:%M')}", category: :error
      end
    end

    if options[:project]
      record.project = @projects.current.id
    end

    options[:description] = reassemble_the args
    record.description = options[:description] if options[:description] && !options[:description].empty?

    @time_records.save_to_file
    Views.update_time_record_view record
  end
end