1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 class TimeEntry < ActiveRecord::Base
19 # could have used polymorphic association
20 # project association here allows easy loading of time entries at project level with one database trip
24 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
28 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
29 validates_numericality_of :hours, :allow_nil => true
30 validates_length_of :comments, :maximum => 255
33 self.project = issue.project if issue && project.nil?
37 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
38 errors.add :project_id, :activerecord_error_invalid if project.nil?
39 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
42 # tyear, tmonth, tweek assigned where setting spent_on attributes
43 # these attributes make time aggregations easier
46 self.tyear = spent_on ? spent_on.year : nil
47 self.tmonth = spent_on ? spent_on.month : nil
48 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil