Class: Period

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Localization
Defined in:
app/models/period.rb

Constant Summary

Constants included from Localization

Localization::LOCALIZED_STRINGS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Localization

#l, load_localized_strings, #valid_language?

Class Method Details

.find_active_or_futureObject



14
15
16
# File 'app/models/period.rb', line 14

def self.find_active_or_future
  find(:all, :conditions => "end_on >= '#{Date.today.strftime '%Y-%m-%d'}'")
end

Instance Method Details

#active?(check_tasks = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/period.rb', line 37

def active?(check_tasks = false)
  start_on <= Date.today && (end_on >= Date.today || (check_tasks && tasks.select {|task| task.active?}.size > 0))
end

#active_or_future?(check_tasks = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/period.rb', line 45

def active_or_future?(check_tasks = false)
  end_on >= Date.today || (check_tasks && tasks.select {|task| task.active?}.size > 0)
end

#backlogsObject



159
160
161
# File 'app/models/period.rb', line 159

def backlogs
  tasks.map {|task| task.backlog}.uniq
end

#burn_down_graph(size) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/models/period.rb', line 163

def burn_down_graph(size)
  g = Gruff::Line.new(size)
  g.theme_37signals
  g.title = l(:burn_down_chart) + " " + name
  g.font = '/usr/share/fonts/bitstream-vera/Vera.ttf'
  g.legend_font_size = 14
  g.hide_dots = true
  g.colors = %w{blue orange}
  if track_work?
    g.colors += %w{green}
  end
  if previous_period = higher_item
    g.colors += %w{grey grey}
  end
  if active?
    g.colors += %w{lightblue #d7a790}
  end
  
  recorded_dates = self.recorded_dates
  observed_todo_data = get_todo_data(recorded_dates)
  actual_todo_data = get_todo_data(recorded_dates, true)
  g.data("#{l :todo} (#{l :obs})", observed_todo_data)
  g.data("#{l :calc}", actual_todo_data)
  g.data(l(:done), get_work_data(recorded_dates)) if track_work?
  if previous_period = higher_item
    g.data("#{l :previous} #{l :obs}", previous_period.get_todo_data(previous_period.dates))
    g.data("#{l :calc}", previous_period.get_todo_data(previous_period.dates, true))
  end
  
  if active?
    g.data(l(:projection), projection_data(observed_todo_data))
    g.data(l(:projection), projection_data(actual_todo_data))
  end
  
  g.minimum_value = 0
  
  all_dates = dates
  labels = {1 => all_dates[1].to_s, all_dates.length-1 => all_dates.last.to_s}
  labels.merge({all_dates.index(Date.today) => Date.today.to_s}) if all_dates.index(Date.today) && (all_dates.index(Date.today) / all_dates.length) > 0.10
  g.labels = labels
  
  # g.draw_vertical_legend
  
  g.maximum_value = (g.maximum_value.to_s[0..0].to_i + 1) * (10**Math::log10(g.maximum_value.to_i).to_i) if g.maximum_value > 0
  g
end

#completed_tasksObject



18
19
20
# File 'app/models/period.rb', line 18

def completed_tasks
  tasks.select {|task| task.completed?}
end

#datesObject



91
92
93
94
95
# File 'app/models/period.rb', line 91

def dates
  all_dates = []
  (start_on-1).upto(end_on) {|date| all_dates << date}
  return all_dates
end

#enable_subtasks?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'app/models/period.rb', line 151

def enable_subtasks?
  not backlogs.find {|backlog| backlog.enable_subtasks?}.nil?
end

#estimate_data(date, actual = false) ⇒ Object



53
54
55
56
57
58
59
# File 'app/models/period.rb', line 53

def estimate_data(date, actual=false)
  total = BigDecimal('0')
  tasks.each do |task|
    total += task.estimate_data(date, actual)
  end
  total
end

#estimates?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/period.rb', line 139

def estimates?
  not backlogs.find {|backlog| backlog.track_todo?}.nil?
end

#future?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/period.rb', line 41

def future?
  start_on >= Date.today
end

#get_todo_data(dates, actual = false) ⇒ Object



210
211
212
# File 'app/models/period.rb', line 210

def get_todo_data(dates, actual=false)
  dates.map { |date| estimate_data(date, actual) }
end

#get_work_data(dates) ⇒ Object



214
215
216
# File 'app/models/period.rb', line 214

def get_work_data(dates)
  dates.map { |date| work_data(date) }
end

#invoice?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'app/models/period.rb', line 155

def invoice?
  not backlogs.find {|backlog| backlog.enable_invoicing?}.nil?
end

#most_frequent_backlogObject



26
27
28
29
30
31
32
33
34
35
# File 'app/models/period.rb', line 26

def most_frequent_backlog
  all_backlogs = tasks.map {|t| t.backlog}.compact
  return nil if all_backlogs.empty?
  freq = {}
  all_backlogs.each do |b|
    freq[b.id] ||= 0
    freq[b.id] += 1
  end
  freq.to_a.sort_by {|backlog, count| count}.first[0]
end

#nameObject



87
88
89
# File 'app/models/period.rb', line 87

def name
  "#{party.name}##{position}"
end

#open_tasksObject



22
23
24
# File 'app/models/period.rb', line 22

def open_tasks
  tasks.select {|task| task.active?}
end

#passed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/period.rb', line 49

def passed?
  end_on < Date.today
end

#projection_data(observed_todo_data) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/models/period.rb', line 218

def projection_data(observed_todo_data)
  if observed_todo_data.length <= 1
    velocity = 0
  else
    velocity = (observed_todo_data[-1] - observed_todo_data[0]).to_f / (observed_todo_data.length-1)
  end
  projection_data = dates.map do |date|
    if date < Date.today 
      nil
    else
      if observed_todo_data.length == 1
        observed_todo_data[0]
      else
        value = observed_todo_data[0] + (date-start_on+1).to_f*velocity
        value >= 0 ? value : 0
      end 
    end
  end
end

#recorded_datesObject



97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/period.rb', line 97

def recorded_dates
  dates = []
  if start_on > Date.today
    dates << (start_on-1)
  elsif end_on < Date.today
    (start_on-1).upto(end_on) {|date| dates << date}
  else
    (start_on-1).upto(Date.today) {|date| dates << date}
  end  
  return dates
end

#required_speedObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/period.rb', line 69

def required_speed
  todo = estimate_data(Date.today)
  remaining_days = end_on - Date.today
  if todo == 0
    0
  elsif remaining_days > 0
    todo / remaining_days
  elsif remaining_days == 0
    todo
  else
    todo * (-remaining_days)
  end
end

#to_sObject



83
84
85
# File 'app/models/period.rb', line 83

def to_s
  name
end

#track_times?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/period.rb', line 147

def track_times?
  not backlogs.find {|backlog| backlog.track_times?}.nil?
end

#track_work?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'app/models/period.rb', line 143

def track_work?
  not backlogs.find {|backlog| backlog.track_done?}.nil?
end

#validateObject



10
11
12
# File 'app/models/period.rb', line 10

def validate
  errors.add "A period cannot end before it starts" unless end_on >= start_on
end

#work_data(date) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/period.rb', line 61

def work_data(date)
  total = BigDecimal('0')
  tasks.each do |task|
    total += task.work_data(date)
  end
  total
end

#works_for_week(week_no, with_empty_works = false, user_id = nil) ⇒ Object

Return an array with a work object per day: [

[<work>, <work>, <work>, <work>, <work>, nil, nil],
[<work>, nil,    <work>, <work>, <work>, nil, nil]

]



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/period.rb', line 114

def works_for_week(week_no, with_empty_works = false, user_id = nil)
  first = Date.commercial(Date.today.year, week_no, 1)
  last = first + 6
  works = tasks.map {|t| t.works_with_children}.flatten
  works.reject! {|work| work.hours == 0} unless with_empty_works
  works.reject! {|work| work.user_id != user_id} if user_id && backlog.enable_users
  by_day = (0..6).map do |i|
    works.select {|w| w.completed_at && w.completed_at.to_date == first + i}.sort do |w1, w2|
      if w1.completed_at != w2.completed_at
        w1.completed_at <=> w2.completed_at
      elsif w1.started_at && w2.started_at
        w1.started_at <=> w2.started_at
      elsif w1.started_at
        1
      else
        -1
      end
    end
  end
  by_day.each {|d| d[works.size] = d[works.size]}
   = by_day.transpose
   = .select {|l| l.compact.size > 0}
  
end