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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/achoo/ui/register_hours.rb', line 100

def all_projects_chooser(form)
  projects = form.all_projects

  p projects

  # FIX move readline stuff to the term modules
  original_readline_comp_proc = Readline.completion_proc
  original_readline_comp_append_char = Readline.completion_append_character
  Readline.completion_append_character = ''
  Readline.completion_proc = proc do |s|
    projects.collect {|p| p[1] }.grep(/^#{Regexp.escape(s)}/)
  end


  answer = chooser_helper(projects, 
                          'All projects', 
                          'Project')

  Readline.completion_proc = original_readline_comp_proc
  Readline.completion_append_character = original_readline_comp_append_char

  answer
end

#billing_chooser(form) ⇒ Object



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

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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/achoo/ui/register_hours.rb', line 124

def chooser_helper(options, heading, prompt, empty_allowed=false)
  puts heading

  extra = empty_allowed ? [''] : []
  project_names = options.collect {|p| p[1] }
  if heading == 'All projects'
    # FIX ugly conditional
    extra += project_names
  end
  answer = Achoo::Term.choose(prompt, options.collect {|p| p[1] }, nil, extra)
  answer = '1' if answer.empty?
  if heading == 'All projects'
    # FIX ugly conditional
    index = project_names.find_index(answer) + 1
    answer = index unless index.nil?
  end
  options[answer.to_i-1][0]
end

#hours_chooserObject



59
60
61
62
# File 'lib/achoo/ui/register_hours.rb', line 59

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

#initializeObject



13
14
# File 'lib/achoo/ui/register_hours.rb', line 13

def initialize
end

#phase_chooser(form) ⇒ Object



52
53
54
55
56
# File 'lib/achoo/ui/register_hours.rb', line 52

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

#project_chooser(form) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/achoo/ui/register_hours.rb', line 83

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

#register_hoursObject



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

def register_hours
  date = optionally_ranged_date_chooser
  
  puts "Fetching data ..."
  form = if date.class == Array
           Achievo::HourRegistrationFormRanged
         else
           Achievo::HourRegistrationForm
         end.new
  
  form.date    = date
  form.project = project_chooser(form)
  form.phase   = phase_chooser(form)
  PLUGINS.send_before_register_hour_remark(date) unless date.class == Array
  form.remark  = remark_chooser
  PLUGINS.send_before_register_hour_hours(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

  date
end

#remark_chooserObject



78
79
80
# File 'lib/achoo/ui/register_hours.rb', line 78

def remark_chooser
  Term::ask 'Remark'
end

#workperiod_chooser(form) ⇒ Object



64
65
66
67
68
69
# File 'lib/achoo/ui/register_hours.rb', line 64

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