initial commit
[gitredmine.git] / app / models / .svn / text-base / time_entry.rb.svn-base
blob90585707303656321259078913b7723be0d76520
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.
8
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.
13
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
21   belongs_to :project
22   belongs_to :issue
23   belongs_to :user
24   belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
25   
26   attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27   
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
32   def before_validation
33     self.project = issue.project if issue && project.nil?
34   end
35   
36   def validate
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)
40   end
41   
42   # tyear, tmonth, tweek assigned where setting spent_on attributes
43   # these attributes make time aggregations easier
44   def spent_on=(date)
45     super
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
49   end  
50 end