forked from scambra/railscollab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_time.rb
322 lines (253 loc) · 8.22 KB
/
project_time.rb
1
2
3
4
5
6
7
8
9
10
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#==
# RailsCollab
# Copyright (C) 2007 - 2008 James S Urquhart
# Portions Copyright (C) René Scheibe
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
class ProjectTime < ActiveRecord::Base
include ActionController::UrlWriter
belongs_to :project
belongs_to :company, :foreign_key => 'assigned_to_company_id'
belongs_to :user, :foreign_key => 'assigned_to_user_id'
belongs_to :project_task_list, :foreign_key => 'task_list_id'
belongs_to :project_task, :foreign_key => 'task_id'
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_id'
belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_id'
has_many :project_messages, :foreign_key => 'milestone_id'
#has_many :tags, :as => 'rel_object', :dependent => :destroy
named_scope :running, :conditions => 'start_date IS NOT NULL AND done_date IS NULL'
before_validation_on_create :process_params
after_create :process_create
before_update :process_update_params
before_destroy :process_destroy
def running?
self.done_date.nil? && !self.start_date.nil?
end
def hours
if self.running?
((Time.now - self.start_date) / 3600.0).round(2)
else
self[:hours]
end
end
def process_params
if self.assigned_to_user_id.nil?
write_attribute("assigned_to_user_id", 0)
end
if self.assigned_to_company_id.nil?
write_attribute("assigned_to_company_id", 0)
end
# set name to task name
if (self.name.nil? or self.name.blank?) and !self.project_task.nil?
self.name = self.project_task.text[0, ProjectTime.columns_hash['name'].limit]
end
end
def process_create
ApplicationLog::new_log(self, self.created_by, :add, self.is_private)
end
def process_update_params
if self.assigned_to_user_id.nil?
write_attribute("assigned_to_user_id", 0)
end
if self.assigned_to_company_id.nil?
write_attribute("assigned_to_company_id", 0)
end
ApplicationLog::new_log(self, self.updated_by, :edit, self.is_private)
end
def process_destroy
Tag.clear_by_object(self)
ApplicationLog.new_log(self, self.updated_by, :delete, self.is_private)
end
def object_name
self.name
end
def object_url(host = nil)
url_for hash_for_time_path(:only_path => host.nil?, :host => host, :id => self.id, :active_project => self.project_id)
end
# Responsible party assignment
def open_task=(obj)
self.project_task_list = obj.try(:task_list)
self.project_task = obj
end
def open_task
self.project_task
end
def open_task_id=(val)
# Set open_task accordingly
if (val.nil? || val == '0')
self.open_task = nil
return
end
self.open_task = ProjectTask.find(val)
end
def open_task_id
if !self.project_task.nil?
self.project_task.id.to_s
else
"0"
end
end
# Task list / task assignment
def assigned_to=(obj)
self.company = obj.class == Company ? obj : nil
self.user = obj.class == User ? obj : nil
end
def assigned_to
if self.company
self.company
elsif self.user
self.user
else
nil
end
end
def assigned_to_id=(val)
# Set assigned_to accordingly
if (val.nil? or val == '0' or val == 'c0')
self.assigned_to = nil
return
end
begin
self.assigned_to = val[0] == 99 ?
Company.find(val[1...val.length]) :
User.find(val)
rescue ActiveRecord::RecordNotFound
self.assigned_to = nil
end
end
def assigned_to_id
if self.company
"c#{self.company.id}"
elsif self.user
self.user.id.to_s
else
"0"
end
end
def tags
return Tag.list_by_object(self).join(',')
end
def tags_with_spaces
return Tag.list_by_object(self).join(' ')
end
def tags=(val)
Tag.clear_by_object(self)
Tag.set_to_object(self, val.split(',')) unless val.nil?
end
def is_today?
return self.done_date.to_date == Date.today
end
def is_yesterday?
return self.done_date.to_date == Date.today-1
end
def last_edited_by_owner?
return (self.created_by.member_of_owner? or (!self.updated_by.nil? and self.updated_by.member_of_owner?))
end
def self.priv_scope(include_private)
if include_private
yield
else
with_scope :find => { :conditions => ['is_private = ?', false] } do
yield
end
end
end
def self.find_by_task_lists(task_lists, time_conds)
lists = []
task_lists.all(:include => {:project_tasks => :project_times}).each do |list|
tasks = []
list.project_tasks.each do |task|
times = task.project_times.select do |time|
time_conds.all? {|attr, value| time.send(attr) == value}
end
total = times.inject(0) {|sum, time| sum + time.hours}
if (total > 0)
total_billable = times.select(&:is_billable).inject(0) {|sum, time| sum + time.hours}
extra_conditions = time_conds.clone.merge({'task_list_id' => list.id, 'task_id' => task.id})
tasks << {:task => task, :hours => total, :billable_hours => total_billable || 0, :times => times}
end
end
lists << {:list => list, :tasks => tasks}
end
return lists
end
def self.find_grouped(group_field, params)
grouped_fields = {}
found_times = ProjectTime.find(:all, params)
group_type = ProjectTime if ['assigned_to','project','project_task','project_task_list'].include?(group_field)
group_type ||= String
found_times.each do |time|
dest_str = nil
if group_type == ProjectTime
dest_str = time[group_field].object_name
else
dest_str = time[group_field].to_s[0..0]
end
grouped_fields[dest_str] ||= []
grouped_fields[dest_str] << file
end
return found_times, grouped_fields
end
def self.all_by_user(user)
projects = user.active_projects
project_ids = projects.collect do |p|
p.id
end.join ','
if project_ids.length == 0
return []
end
time_conditions = user.member_of_owner? ?
["project_id IN (#{project_ids})"] :
["project_id IN (#{project_ids}) AND is_private = ?", false]
return self.find(:all, :conditions => time_conditions)
end
# Core Permissions
def self.can_be_created_by(user, project)
project.is_active? and user.has_permission(project, :can_manage_time)
end
def can_be_edited_by(user)
return false if (!user.member_of(project))
return ((user.is_admin or created_by.id == user.id) and project.is_active?)
end
def can_be_deleted_by(user)
project.is_active? and user.member_of(project) and user.is_admin
end
def can_be_seen_by(user)
if !project.has_member(user)
return false
end
if user.has_permission(project, :can_manage_time)
return true
end
if self.is_private and !user.member_of_owner?
return false
end
return true
end
# Specific Permissions
def can_be_managed_by(user)
project.is_active? and user.has_permission(project, :can_manage_time)
end
# Accesibility
attr_accessible :name, :description, :done_date, :hours, :open_task_id, :assigned_to_id, :is_private, :is_billable
# Validation
validates_presence_of :name
validates_each :is_private, :if => Proc.new { |obj| !obj.last_edited_by_owner? } do |record, attr, value|
record.errors.add attr, :not_allowed.l if value == true
end
validates_each :assigned_to, :allow_nil => true do |record, attr, value|
record.errors.add attr, :not_part_of_project.l if (!value.nil? and !value.is_part_of(record.project))
end
end