@@ -1,118 +1,119 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 | include Redmine::SafeAttributes |
|
19 | include Redmine::SafeAttributes | |
20 | # could have used polymorphic association |
|
20 | # could have used polymorphic association | |
21 | # project association here allows easy loading of time entries at project level with one database trip |
|
21 | # project association here allows easy loading of time entries at project level with one database trip | |
22 | belongs_to :project |
|
22 | belongs_to :project | |
23 | belongs_to :issue |
|
23 | belongs_to :issue | |
24 | belongs_to :user |
|
24 | belongs_to :user | |
25 | belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' |
|
25 | belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id' | |
26 |
|
26 | |||
27 | attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek |
|
27 | attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek | |
28 |
|
28 | |||
29 | acts_as_customizable |
|
29 | acts_as_customizable | |
30 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, |
|
30 | acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"}, | |
31 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, |
|
31 | :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}}, | |
32 | :author => :user, |
|
32 | :author => :user, | |
33 | :group => :issue, |
|
33 | :group => :issue, | |
34 | :description => :comments |
|
34 | :description => :comments | |
35 |
|
35 | |||
36 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", |
|
36 | acts_as_activity_provider :timestamp => "#{table_name}.created_on", | |
37 | :author_key => :user_id, |
|
37 | :author_key => :user_id, | |
38 | :find_options => {:include => :project} |
|
38 | :find_options => {:include => :project} | |
39 |
|
39 | |||
40 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
|
40 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on | |
41 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid |
|
41 | validates_numericality_of :hours, :allow_nil => true, :message => :invalid | |
42 | validates_length_of :comments, :maximum => 255, :allow_nil => true |
|
42 | validates_length_of :comments, :maximum => 255, :allow_nil => true | |
|
43 | validates :spent_on, :date => true | |||
43 | before_validation :set_project_if_nil |
|
44 | before_validation :set_project_if_nil | |
44 | validate :validate_time_entry |
|
45 | validate :validate_time_entry | |
45 |
|
46 | |||
46 | scope :visible, lambda {|*args| |
|
47 | scope :visible, lambda {|*args| | |
47 | includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)) |
|
48 | includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)) | |
48 | } |
|
49 | } | |
49 | scope :on_issue, lambda {|issue| |
|
50 | scope :on_issue, lambda {|issue| | |
50 | includes(:issue).where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") |
|
51 | includes(:issue).where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}") | |
51 | } |
|
52 | } | |
52 | scope :on_project, lambda {|project, include_subprojects| |
|
53 | scope :on_project, lambda {|project, include_subprojects| | |
53 | includes(:project).where(project.project_condition(include_subprojects)) |
|
54 | includes(:project).where(project.project_condition(include_subprojects)) | |
54 | } |
|
55 | } | |
55 | scope :spent_between, lambda {|from, to| |
|
56 | scope :spent_between, lambda {|from, to| | |
56 | if from && to |
|
57 | if from && to | |
57 | where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to) |
|
58 | where("#{TimeEntry.table_name}.spent_on BETWEEN ? AND ?", from, to) | |
58 | elsif from |
|
59 | elsif from | |
59 | where("#{TimeEntry.table_name}.spent_on >= ?", from) |
|
60 | where("#{TimeEntry.table_name}.spent_on >= ?", from) | |
60 | elsif to |
|
61 | elsif to | |
61 | where("#{TimeEntry.table_name}.spent_on <= ?", to) |
|
62 | where("#{TimeEntry.table_name}.spent_on <= ?", to) | |
62 | else |
|
63 | else | |
63 | where(nil) |
|
64 | where(nil) | |
64 | end |
|
65 | end | |
65 | } |
|
66 | } | |
66 |
|
67 | |||
67 | safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' |
|
68 | safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields' | |
68 |
|
69 | |||
69 | def initialize(attributes=nil, *args) |
|
70 | def initialize(attributes=nil, *args) | |
70 | super |
|
71 | super | |
71 | if new_record? && self.activity.nil? |
|
72 | if new_record? && self.activity.nil? | |
72 | if default_activity = TimeEntryActivity.default |
|
73 | if default_activity = TimeEntryActivity.default | |
73 | self.activity_id = default_activity.id |
|
74 | self.activity_id = default_activity.id | |
74 | end |
|
75 | end | |
75 | self.hours = nil if hours == 0 |
|
76 | self.hours = nil if hours == 0 | |
76 | end |
|
77 | end | |
77 | end |
|
78 | end | |
78 |
|
79 | |||
79 | def set_project_if_nil |
|
80 | def set_project_if_nil | |
80 | self.project = issue.project if issue && project.nil? |
|
81 | self.project = issue.project if issue && project.nil? | |
81 | end |
|
82 | end | |
82 |
|
83 | |||
83 | def validate_time_entry |
|
84 | def validate_time_entry | |
84 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) |
|
85 | errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) | |
85 | errors.add :project_id, :invalid if project.nil? |
|
86 | errors.add :project_id, :invalid if project.nil? | |
86 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) |
|
87 | errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) | |
87 | end |
|
88 | end | |
88 |
|
89 | |||
89 | def hours=(h) |
|
90 | def hours=(h) | |
90 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) |
|
91 | write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) | |
91 | end |
|
92 | end | |
92 |
|
93 | |||
93 | def hours |
|
94 | def hours | |
94 | h = read_attribute(:hours) |
|
95 | h = read_attribute(:hours) | |
95 | if h.is_a?(Float) |
|
96 | if h.is_a?(Float) | |
96 | h.round(2) |
|
97 | h.round(2) | |
97 | else |
|
98 | else | |
98 | h |
|
99 | h | |
99 | end |
|
100 | end | |
100 | end |
|
101 | end | |
101 |
|
102 | |||
102 | # tyear, tmonth, tweek assigned where setting spent_on attributes |
|
103 | # tyear, tmonth, tweek assigned where setting spent_on attributes | |
103 | # these attributes make time aggregations easier |
|
104 | # these attributes make time aggregations easier | |
104 | def spent_on=(date) |
|
105 | def spent_on=(date) | |
105 | super |
|
106 | super | |
106 | if spent_on.is_a?(Time) |
|
107 | if spent_on.is_a?(Time) | |
107 | self.spent_on = spent_on.to_date |
|
108 | self.spent_on = spent_on.to_date | |
108 | end |
|
109 | end | |
109 | self.tyear = spent_on ? spent_on.year : nil |
|
110 | self.tyear = spent_on ? spent_on.year : nil | |
110 | self.tmonth = spent_on ? spent_on.month : nil |
|
111 | self.tmonth = spent_on ? spent_on.month : nil | |
111 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil |
|
112 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil | |
112 | end |
|
113 | end | |
113 |
|
114 | |||
114 | # Returns true if the time entry can be edited by usr, otherwise false |
|
115 | # Returns true if the time entry can be edited by usr, otherwise false | |
115 | def editable_by?(usr) |
|
116 | def editable_by?(usr) | |
116 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
|
117 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) | |
117 | end |
|
118 | end | |
118 | end |
|
119 | end |
@@ -1,128 +1,135 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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 | :members, :roles, :member_roles, |
|
22 | :members, :roles, :member_roles, | |
23 | :trackers, :issue_statuses, |
|
23 | :trackers, :issue_statuses, | |
24 | :projects_trackers, |
|
24 | :projects_trackers, | |
25 | :journals, :journal_details, |
|
25 | :journals, :journal_details, | |
26 | :issue_categories, :enumerations, |
|
26 | :issue_categories, :enumerations, | |
27 | :groups_users, |
|
27 | :groups_users, | |
28 | :enabled_modules |
|
28 | :enabled_modules | |
29 |
|
29 | |||
30 | def test_hours_format |
|
30 | def test_hours_format | |
31 | assertions = { "2" => 2.0, |
|
31 | assertions = { "2" => 2.0, | |
32 | "21.1" => 21.1, |
|
32 | "21.1" => 21.1, | |
33 | "2,1" => 2.1, |
|
33 | "2,1" => 2.1, | |
34 | "1,5h" => 1.5, |
|
34 | "1,5h" => 1.5, | |
35 | "7:12" => 7.2, |
|
35 | "7:12" => 7.2, | |
36 | "10h" => 10.0, |
|
36 | "10h" => 10.0, | |
37 | "10 h" => 10.0, |
|
37 | "10 h" => 10.0, | |
38 | "45m" => 0.75, |
|
38 | "45m" => 0.75, | |
39 | "45 m" => 0.75, |
|
39 | "45 m" => 0.75, | |
40 | "3h15" => 3.25, |
|
40 | "3h15" => 3.25, | |
41 | "3h 15" => 3.25, |
|
41 | "3h 15" => 3.25, | |
42 | "3 h 15" => 3.25, |
|
42 | "3 h 15" => 3.25, | |
43 | "3 h 15m" => 3.25, |
|
43 | "3 h 15m" => 3.25, | |
44 | "3 h 15 m" => 3.25, |
|
44 | "3 h 15 m" => 3.25, | |
45 | "3 hours" => 3.0, |
|
45 | "3 hours" => 3.0, | |
46 | "12min" => 0.2, |
|
46 | "12min" => 0.2, | |
47 | "12 Min" => 0.2, |
|
47 | "12 Min" => 0.2, | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | assertions.each do |k, v| |
|
50 | assertions.each do |k, v| | |
51 | t = TimeEntry.new(:hours => k) |
|
51 | t = TimeEntry.new(:hours => k) | |
52 | assert_equal v, t.hours, "Converting #{k} failed:" |
|
52 | assert_equal v, t.hours, "Converting #{k} failed:" | |
53 | end |
|
53 | end | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def test_hours_should_default_to_nil |
|
56 | def test_hours_should_default_to_nil | |
57 | assert_nil TimeEntry.new.hours |
|
57 | assert_nil TimeEntry.new.hours | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def test_spent_on_with_blank |
|
60 | def test_spent_on_with_blank | |
61 | c = TimeEntry.new |
|
61 | c = TimeEntry.new | |
62 | c.spent_on = '' |
|
62 | c.spent_on = '' | |
63 | assert_nil c.spent_on |
|
63 | assert_nil c.spent_on | |
64 | end |
|
64 | end | |
65 |
|
65 | |||
66 | def test_spent_on_with_nil |
|
66 | def test_spent_on_with_nil | |
67 | c = TimeEntry.new |
|
67 | c = TimeEntry.new | |
68 | c.spent_on = nil |
|
68 | c.spent_on = nil | |
69 | assert_nil c.spent_on |
|
69 | assert_nil c.spent_on | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def test_spent_on_with_string |
|
72 | def test_spent_on_with_string | |
73 | c = TimeEntry.new |
|
73 | c = TimeEntry.new | |
74 | c.spent_on = "2011-01-14" |
|
74 | c.spent_on = "2011-01-14" | |
75 | assert_equal Date.parse("2011-01-14"), c.spent_on |
|
75 | assert_equal Date.parse("2011-01-14"), c.spent_on | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def test_spent_on_with_invalid_string |
|
78 | def test_spent_on_with_invalid_string | |
79 | c = TimeEntry.new |
|
79 | c = TimeEntry.new | |
80 | c.spent_on = "foo" |
|
80 | c.spent_on = "foo" | |
81 | assert_nil c.spent_on |
|
81 | assert_nil c.spent_on | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def test_spent_on_with_date |
|
84 | def test_spent_on_with_date | |
85 | c = TimeEntry.new |
|
85 | c = TimeEntry.new | |
86 | c.spent_on = Date.today |
|
86 | c.spent_on = Date.today | |
87 | assert_equal Date.today, c.spent_on |
|
87 | assert_equal Date.today, c.spent_on | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | def test_spent_on_with_time |
|
90 | def test_spent_on_with_time | |
91 | c = TimeEntry.new |
|
91 | c = TimeEntry.new | |
92 | c.spent_on = Time.now |
|
92 | c.spent_on = Time.now | |
93 | assert_equal Date.today, c.spent_on |
|
93 | assert_equal Date.today, c.spent_on | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def test_validate_time_entry |
|
96 | def test_validate_time_entry | |
97 | anon = User.anonymous |
|
97 | anon = User.anonymous | |
98 | project = Project.find(1) |
|
98 | project = Project.find(1) | |
99 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, |
|
99 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, | |
100 | :priority => IssuePriority.all.first, :subject => 'test_create', |
|
100 | :priority => IssuePriority.all.first, :subject => 'test_create', | |
101 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
101 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') | |
102 | assert issue.save |
|
102 | assert issue.save | |
103 | activity = TimeEntryActivity.find_by_name('Design') |
|
103 | activity = TimeEntryActivity.find_by_name('Design') | |
104 | te = TimeEntry.create(:spent_on => '2010-01-01', |
|
104 | te = TimeEntry.create(:spent_on => '2010-01-01', | |
105 | :hours => 100000, |
|
105 | :hours => 100000, | |
106 | :issue => issue, |
|
106 | :issue => issue, | |
107 | :project => project, |
|
107 | :project => project, | |
108 | :user => anon, |
|
108 | :user => anon, | |
109 | :activity => activity) |
|
109 | :activity => activity) | |
110 | assert_equal 1, te.errors.count |
|
110 | assert_equal 1, te.errors.count | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
|
113 | def test_spent_on_with_2_digits_year_should_not_be_valid | |||
|
114 | entry = TimeEntry.new(:project => Project.find(1), :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1) | |||
|
115 | entry.spent_on = "09-02-04" | |||
|
116 | assert !entry.valid? | |||
|
117 | assert_include I18n.translate('activerecord.errors.messages.not_a_date'), entry.errors[:spent_on] | |||
|
118 | end | |||
|
119 | ||||
113 | def test_set_project_if_nil |
|
120 | def test_set_project_if_nil | |
114 | anon = User.anonymous |
|
121 | anon = User.anonymous | |
115 | project = Project.find(1) |
|
122 | project = Project.find(1) | |
116 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, |
|
123 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, | |
117 | :priority => IssuePriority.all.first, :subject => 'test_create', |
|
124 | :priority => IssuePriority.all.first, :subject => 'test_create', | |
118 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
125 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') | |
119 | assert issue.save |
|
126 | assert issue.save | |
120 | activity = TimeEntryActivity.find_by_name('Design') |
|
127 | activity = TimeEntryActivity.find_by_name('Design') | |
121 | te = TimeEntry.create(:spent_on => '2010-01-01', |
|
128 | te = TimeEntry.create(:spent_on => '2010-01-01', | |
122 | :hours => 10, |
|
129 | :hours => 10, | |
123 | :issue => issue, |
|
130 | :issue => issue, | |
124 | :user => anon, |
|
131 | :user => anon, | |
125 | :activity => activity) |
|
132 | :activity => activity) | |
126 | assert_equal project.id, te.project.id |
|
133 | assert_equal project.id, te.project.id | |
127 | end |
|
134 | end | |
128 | end |
|
135 | end |
General Comments 0
You need to be logged in to leave comments.
Login now