Class: Period

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_active_or_futureObject



12
13
14
# File 'app/models/period.rb', line 12

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)


35
36
37
# File 'app/models/period.rb', line 35

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)


43
44
45
# File 'app/models/period.rb', line 43

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

#backlogsObject



157
158
159
# File 'app/models/period.rb', line 157

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

#completed_tasksObject



16
17
18
# File 'app/models/period.rb', line 16

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

#datesObject



89
90
91
92
93
# File 'app/models/period.rb', line 89

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

#enable_subtasks?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'app/models/period.rb', line 149

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

#estimate_data(date, actual = false) ⇒ Object



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

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)


137
138
139
# File 'app/models/period.rb', line 137

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

#future?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/period.rb', line 39

def future?
  start_on >= Date.today
end

#invoice?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/period.rb', line 153

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

#most_frequent_backlogObject



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

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



85
86
87
# File 'app/models/period.rb', line 85

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

#open_tasksObject



20
21
22
# File 'app/models/period.rb', line 20

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

#passed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/period.rb', line 47

def passed?
  end_on < Date.today
end

#recorded_datesObject



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

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

#required_speedObject



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

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



81
82
83
# File 'app/models/period.rb', line 81

def to_s
  name
end

#track_times?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'app/models/period.rb', line 145

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

#track_work?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/period.rb', line 141

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

#validateObject



8
9
10
# File 'app/models/period.rb', line 8

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

#work_data(date) ⇒ Object



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

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]

]



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

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