##// END OF EJS Templates
Fixes TimeEntry#spent_on= so that datetimes don't get stored in SQLite3 (#7258)....
Jean-Philippe Lang -
r4588:88d847a1d512
parent child
Show More
@@ -1,100 +1,103
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 => 'index', :project_id => o.project, :issue_id => o.issue}},
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :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 self.hours = nil if hours == 0
48 end
48 end
49 end
49 end
50
50
51 def before_validation
51 def before_validation
52 self.project = issue.project if issue && project.nil?
52 self.project = issue.project if issue && project.nil?
53 end
53 end
54
54
55 def validate
55 def validate
56 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
56 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
57 errors.add :project_id, :invalid if project.nil?
57 errors.add :project_id, :invalid if project.nil?
58 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)
59 end
59 end
60
60
61 def hours=(h)
61 def hours=(h)
62 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)
63 end
63 end
64
64
65 # tyear, tmonth, tweek assigned where setting spent_on attributes
65 # tyear, tmonth, tweek assigned where setting spent_on attributes
66 # these attributes make time aggregations easier
66 # these attributes make time aggregations easier
67 def spent_on=(date)
67 def spent_on=(date)
68 super
68 super
69 if spent_on.is_a?(Time)
70 self.spent_on = spent_on.to_date
71 end
69 self.tyear = spent_on ? spent_on.year : nil
72 self.tyear = spent_on ? spent_on.year : nil
70 self.tmonth = spent_on ? spent_on.month : nil
73 self.tmonth = spent_on ? spent_on.month : nil
71 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
74 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
72 end
75 end
73
76
74 # Returns true if the time entry can be edited by usr, otherwise false
77 # Returns true if the time entry can be edited by usr, otherwise false
75 def editable_by?(usr)
78 def editable_by?(usr)
76 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
79 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
77 end
80 end
78
81
79 def self.visible_by(usr)
82 def self.visible_by(usr)
80 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
83 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
81 yield
84 yield
82 end
85 end
83 end
86 end
84
87
85 def self.earilest_date_for_project(project=nil)
88 def self.earilest_date_for_project(project=nil)
86 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
89 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
87 if project
90 if project
88 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
91 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
89 end
92 end
90 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
93 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
91 end
94 end
92
95
93 def self.latest_date_for_project(project=nil)
96 def self.latest_date_for_project(project=nil)
94 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
97 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
95 if project
98 if project
96 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
99 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
97 end
100 end
98 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
101 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
99 end
102 end
100 end
103 end
@@ -1,99 +1,129
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.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
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
47
48 def test_hours_should_default_to_nil
48 def test_hours_should_default_to_nil
49 assert_nil TimeEntry.new.hours
49 assert_nil TimeEntry.new.hours
50 end
50 end
51
52 def test_spent_on_with_blank
53 c = TimeEntry.new
54 c.spent_on = ''
55 assert_nil c.spent_on
56 end
57
58 def test_spent_on_with_nil
59 c = TimeEntry.new
60 c.spent_on = nil
61 assert_nil c.spent_on
62 end
63
64 def test_spent_on_with_string
65 c = TimeEntry.new
66 c.spent_on = "2011-01-14"
67 assert_equal Date.parse("2011-01-14"), c.spent_on
68 end
69
70 def test_spent_on_with_date
71 c = TimeEntry.new
72 c.spent_on = Date.today
73 assert_equal Date.today, c.spent_on
74 end
75
76 def test_spent_on_with_time
77 c = TimeEntry.new
78 c.spent_on = Time.now
79 assert_equal Date.today, c.spent_on
80 end
51
81
52 context "#earilest_date_for_project" do
82 context "#earilest_date_for_project" do
53 setup do
83 setup do
54 User.current = nil
84 User.current = nil
55 @public_project = Project.generate!(:is_public => true)
85 @public_project = Project.generate!(:is_public => true)
56 @issue = Issue.generate_for_project!(@public_project)
86 @issue = Issue.generate_for_project!(@public_project)
57 TimeEntry.generate!(:spent_on => '2010-01-01',
87 TimeEntry.generate!(:spent_on => '2010-01-01',
58 :issue => @issue,
88 :issue => @issue,
59 :project => @public_project)
89 :project => @public_project)
60 end
90 end
61
91
62 context "without a project" do
92 context "without a project" do
63 should "return the lowest spent_on value that is visible to the current user" do
93 should "return the lowest spent_on value that is visible to the current user" do
64 assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
94 assert_equal "2007-03-12", TimeEntry.earilest_date_for_project.to_s
65 end
95 end
66 end
96 end
67
97
68 context "with a project" do
98 context "with a project" do
69 should "return the lowest spent_on value that is visible to the current user for that project and it's subprojects only" do
99 should "return the lowest spent_on value that is visible to the current user for that project and it's subprojects only" do
70 assert_equal "2010-01-01", TimeEntry.earilest_date_for_project(@public_project).to_s
100 assert_equal "2010-01-01", TimeEntry.earilest_date_for_project(@public_project).to_s
71 end
101 end
72 end
102 end
73
103
74 end
104 end
75
105
76 context "#latest_date_for_project" do
106 context "#latest_date_for_project" do
77 setup do
107 setup do
78 User.current = nil
108 User.current = nil
79 @public_project = Project.generate!(:is_public => true)
109 @public_project = Project.generate!(:is_public => true)
80 @issue = Issue.generate_for_project!(@public_project)
110 @issue = Issue.generate_for_project!(@public_project)
81 TimeEntry.generate!(:spent_on => '2010-01-01',
111 TimeEntry.generate!(:spent_on => '2010-01-01',
82 :issue => @issue,
112 :issue => @issue,
83 :project => @public_project)
113 :project => @public_project)
84 end
114 end
85
115
86 context "without a project" do
116 context "without a project" do
87 should "return the highest spent_on value that is visible to the current user" do
117 should "return the highest spent_on value that is visible to the current user" do
88 assert_equal "2010-01-01", TimeEntry.latest_date_for_project.to_s
118 assert_equal "2010-01-01", TimeEntry.latest_date_for_project.to_s
89 end
119 end
90 end
120 end
91
121
92 context "with a project" do
122 context "with a project" do
93 should "return the highest spent_on value that is visible to the current user for that project and it's subprojects only" do
123 should "return the highest spent_on value that is visible to the current user for that project and it's subprojects only" do
94 project = Project.find(1)
124 project = Project.find(1)
95 assert_equal "2007-04-22", TimeEntry.latest_date_for_project(project).to_s
125 assert_equal "2007-04-22", TimeEntry.latest_date_for_project(project).to_s
96 end
126 end
97 end
127 end
98 end
128 end
99 end
129 end
General Comments 0
You need to be logged in to leave comments. Login now