Module: Achoo::UI::RegisterHours

Includes:
DateChoosers, ExceptionHandling
Included in:
App
Defined in:
lib/achoo/ui/register_hours.rb

Instance Method Summary collapse

Methods included from ExceptionHandling

#get_exception_reason, #handle_exception, #handle_fatal_exception

Methods included from DateChoosers

#date_chooser, #month_chooser, #optionally_ranged_date_chooser

Instance Method Details

#all_projects_chooser(form) ⇒ Object



127
128
129
130
131
# File 'lib/achoo/ui/register_hours.rb', line 127

def all_projects_chooser(form)
  chooser_helper(form.all_projects, 
                 'All projects', 
                 'Project')
end

#billing_chooser(form) ⇒ Object



79
80
81
82
83
84
# File 'lib/achoo/ui/register_hours.rb', line 79

def billing_chooser(form)
  chooser_helper(form.billing_options,
                 "Billing options", 
                 'Billing [1]',
                 true)
end

#chooser_helper(options, heading, prompt, empty_allowed = false) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/achoo/ui/register_hours.rb', line 133

def chooser_helper(options, heading, prompt, empty_allowed=false)
  puts heading
  extra = empty_allowed ? [''] : []
  answer = Achoo::Term.choose(prompt, options.collect {|p| p[1] }, nil, [''])
  answer = '1' if answer.empty?
  options[answer.to_i-1][0]
end

#hours_chooserObject



67
68
69
70
# File 'lib/achoo/ui/register_hours.rb', line 67

def hours_chooser
  answer = Term::ask 'Hours [7:30]'
  return answer == '' ? '7.5' : answer
end

#phase_chooser(form) ⇒ Object



49
50
51
52
53
# File 'lib/achoo/ui/register_hours.rb', line 49

def phase_chooser(form)
  chooser_helper(form.phases_for_selected_project,
                 "Phases",
                 'Phase')
end


56
57
58
59
60
61
62
63
64
65
# File 'lib/achoo/ui/register_hours.rb', line 56

def print_hours_help(date)
  puts "Awake log:"
  begin
    awake = Awake.new
    awake.at(date)
    puts
  rescue Exception => e
    print handle_exception("Failed to retrieve awake log.", e)
  end
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/achoo/ui/register_hours.rb', line 86

def print_remark_help(date)
  puts "VCS logs for #{date}:"
  begin
    VCS.print_logs_for(date, RC[:vcs_dirs])
  rescue Exception => e
    puts handle_exception("Failed to retrieve VCS logs.", e)
  end
  puts '-' * 80
  puts "Calendar events for #{date}:"
  puts '---'
  begin
    RC[:ical].each do |config|
      ICal.from_http_request(config).print_events(date)
    end
  rescue Exception => e
    puts handle_exception("Failed to retrieve calendar events.", e)
  end
end

#project_chooser(form) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/achoo/ui/register_hours.rb', line 110

def project_chooser(form)
  puts 'Recently used projects'
  projects = form.recent_projects
  answer = Term.choose('Project [1]', projects.collect { |p| p[1] },
                       'Other', [''])
  case answer
  when ''
    projects[0][0]
  when '0'
    return all_projects_chooser(form)
  else
    return projects[answer.to_i-1][0]
  end
end

#register_hours(agent) ⇒ Object



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
# File 'lib/achoo/ui/register_hours.rb', line 15

def register_hours(agent)
  date = optionally_ranged_date_chooser
  
  puts "Fetching data ..."
  form = if date.class == Array
           Achievo::HourRegistrationFormRanged
         else
           Achievo::HourRegistrationForm
         end.new(agent)
  
  form.date    = date
  form.project = project_chooser(form)
  form.phase   = phase_chooser(form)
  print_remark_help(date) unless date.class == Array
  form.remark  = remark_chooser
  print_hours_help(date) unless date.class == Array
  form.hours   = hours_chooser
  
  answer = Term.ask("Do you want to change the defaults for worktime period and/or billing percentage? [N/y]").downcase
  if answer == 'y'
    form.workperiod = workperiod_chooser(form)
    form.billing    = billing_chooser(form)
  end

  form.print_values
  if confirm
    puts "Submitting ..."
    form.submit
  else
    puts "Cancelled"
  end
end

#remark_chooserObject



105
106
107
# File 'lib/achoo/ui/register_hours.rb', line 105

def remark_chooser
  Term::ask 'Remark'
end

#workperiod_chooser(form) ⇒ Object



72
73
74
75
76
77
# File 'lib/achoo/ui/register_hours.rb', line 72

def workperiod_chooser(form)
  chooser_helper(form.worktime_periods,
                 "Worktime periods",
                 'Period [1]',
                 true)
end