##// END OF EJS Templates
Force TimeEntry#hours default to nil (#3075, #4449)....
Jean-Philippe Lang -
r3118:d63784569f49
parent child
Show More
@@ -1,83 +1,84
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimeEntry < ActiveRecord::Base
18 class TimeEntry < ActiveRecord::Base
19 # could have used polymorphic association
19 # could have used polymorphic association
20 # project association here allows easy loading of time entries at project level with one database trip
20 # project association here allows easy loading of time entries at project level with one database trip
21 belongs_to :project
21 belongs_to :project
22 belongs_to :issue
22 belongs_to :issue
23 belongs_to :user
23 belongs_to :user
24 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
24 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
25
25
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27
27
28 acts_as_customizable
28 acts_as_customizable
29 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
29 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'details', :project_id => o.project, :issue_id => o.issue}},
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'details', :project_id => o.project, :issue_id => o.issue}},
31 :author => :user,
31 :author => :user,
32 :description => :comments
32 :description => :comments
33
33
34 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
34 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
35 :author_key => :user_id,
35 :author_key => :user_id,
36 :find_options => {:include => :project}
36 :find_options => {:include => :project}
37
37
38 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
38 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
39 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
39 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
40 validates_length_of :comments, :maximum => 255, :allow_nil => true
40 validates_length_of :comments, :maximum => 255, :allow_nil => true
41
41
42 def after_initialize
42 def after_initialize
43 if new_record? && self.activity.nil?
43 if new_record? && self.activity.nil?
44 if default_activity = TimeEntryActivity.default
44 if default_activity = TimeEntryActivity.default
45 self.activity_id = default_activity.id
45 self.activity_id = default_activity.id
46 end
46 end
47 self.hours = nil if hours == 0
47 end
48 end
48 end
49 end
49
50
50 def before_validation
51 def before_validation
51 self.project = issue.project if issue && project.nil?
52 self.project = issue.project if issue && project.nil?
52 end
53 end
53
54
54 def validate
55 def validate
55 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
56 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
56 errors.add :project_id, :invalid if project.nil?
57 errors.add :project_id, :invalid if project.nil?
57 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
58 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
58 end
59 end
59
60
60 def hours=(h)
61 def hours=(h)
61 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
62 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
62 end
63 end
63
64
64 # tyear, tmonth, tweek assigned where setting spent_on attributes
65 # tyear, tmonth, tweek assigned where setting spent_on attributes
65 # these attributes make time aggregations easier
66 # these attributes make time aggregations easier
66 def spent_on=(date)
67 def spent_on=(date)
67 super
68 super
68 self.tyear = spent_on ? spent_on.year : nil
69 self.tyear = spent_on ? spent_on.year : nil
69 self.tmonth = spent_on ? spent_on.month : nil
70 self.tmonth = spent_on ? spent_on.month : nil
70 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
71 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
71 end
72 end
72
73
73 # Returns true if the time entry can be edited by usr, otherwise false
74 # Returns true if the time entry can be edited by usr, otherwise false
74 def editable_by?(usr)
75 def editable_by?(usr)
75 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
76 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
76 end
77 end
77
78
78 def self.visible_by(usr)
79 def self.visible_by(usr)
79 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
80 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
80 yield
81 yield
81 end
82 end
82 end
83 end
83 end
84 end
@@ -1,47 +1,51
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class TimeEntryTest < ActiveSupport::TestCase
20 class TimeEntryTest < ActiveSupport::TestCase
21 fixtures :issues, :projects, :users, :time_entries
21 fixtures :issues, :projects, :users, :time_entries
22
22
23 def test_hours_format
23 def test_hours_format
24 assertions = { "2" => 2.0,
24 assertions = { "2" => 2.0,
25 "21.1" => 21.1,
25 "21.1" => 21.1,
26 "2,1" => 2.1,
26 "2,1" => 2.1,
27 "1,5h" => 1.5,
27 "1,5h" => 1.5,
28 "7:12" => 7.2,
28 "7:12" => 7.2,
29 "10h" => 10.0,
29 "10h" => 10.0,
30 "10 h" => 10.0,
30 "10 h" => 10.0,
31 "45m" => 0.75,
31 "45m" => 0.75,
32 "45 m" => 0.75,
32 "45 m" => 0.75,
33 "3h15" => 3.25,
33 "3h15" => 3.25,
34 "3h 15" => 3.25,
34 "3h 15" => 3.25,
35 "3 h 15" => 3.25,
35 "3 h 15" => 3.25,
36 "3 h 15m" => 3.25,
36 "3 h 15m" => 3.25,
37 "3 h 15 m" => 3.25,
37 "3 h 15 m" => 3.25,
38 "3 hours" => 3.0,
38 "3 hours" => 3.0,
39 "12min" => 0.2,
39 "12min" => 0.2,
40 }
40 }
41
41
42 assertions.each do |k, v|
42 assertions.each do |k, v|
43 t = TimeEntry.new(:hours => k)
43 t = TimeEntry.new(:hours => k)
44 assert_equal v, t.hours, "Converting #{k} failed:"
44 assert_equal v, t.hours, "Converting #{k} failed:"
45 end
45 end
46 end
46 end
47
48 def test_hours_should_default_to_nil
49 assert_nil TimeEntry.new.hours
50 end
47 end
51 end
General Comments 0
You need to be logged in to leave comments. Login now