##// END OF EJS Templates
added 999 as a maximum for time entry hours...
Jean-Philippe Lang -
r366:76d4ac17a2aa
parent child
Show More
@@ -1,33 +1,33
1 class TimeEntry < ActiveRecord::Base
1 class TimeEntry < ActiveRecord::Base
2 # could have used polymorphic association
2 # could have used polymorphic association
3 # project association here allows easy loading of time entries at project level with one database trip
3 # project association here allows easy loading of time entries at project level with one database trip
4 belongs_to :project
4 belongs_to :project
5 belongs_to :issue
5 belongs_to :issue
6 belongs_to :user
6 belongs_to :user
7 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
7 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
8
8
9 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
9 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
10
10
11 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
11 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
12 validates_numericality_of :hours, :allow_nil => true
12 validates_numericality_of :hours, :allow_nil => true
13 validates_length_of :comment, :maximum => 255
13 validates_length_of :comment, :maximum => 255
14
14
15 def before_validation
15 def before_validation
16 self.project = issue.project if issue && project.nil?
16 self.project = issue.project if issue && project.nil?
17 end
17 end
18
18
19 def validate
19 def validate
20 errors.add :hours, :activerecord_error_invalid if hours && hours < 0
20 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
21 errors.add :project_id, :activerecord_error_invalid if project.nil?
21 errors.add :project_id, :activerecord_error_invalid if project.nil?
22 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
22 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
23 end
23 end
24
24
25 # tyear, tmonth, tweek assigned where setting spent_on attributes
25 # tyear, tmonth, tweek assigned where setting spent_on attributes
26 # these attributes make time aggregations easier
26 # these attributes make time aggregations easier
27 def spent_on=(date)
27 def spent_on=(date)
28 super
28 super
29 self.tyear = spent_on ? spent_on.year : nil
29 self.tyear = spent_on ? spent_on.year : nil
30 self.tmonth = spent_on ? spent_on.month : nil
30 self.tmonth = spent_on ? spent_on.month : nil
31 self.tweek = spent_on ? spent_on.cweek : nil
31 self.tweek = spent_on ? spent_on.cweek : nil
32 end
32 end
33 end
33 end
General Comments 0
You need to be logged in to leave comments. Login now