@@ -0,0 +1,46 | |||||
|
1 | # redMine - project management software | |||
|
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang | |||
|
3 | # | |||
|
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. | |||
|
17 | ||||
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |||
|
19 | ||||
|
20 | class TimeEntryTest < Test::Unit::TestCase | |||
|
21 | fixtures :issues, :projects, :users, :time_entries | |||
|
22 | ||||
|
23 | def test_hours_format | |||
|
24 | assertions = { "2" => 2.0, | |||
|
25 | "21.1" => 21.1, | |||
|
26 | "2,1" => 2.1, | |||
|
27 | "7:12" => 7.2, | |||
|
28 | "10h" => 10.0, | |||
|
29 | "10 h" => 10.0, | |||
|
30 | "45m" => 0.75, | |||
|
31 | "45 m" => 0.75, | |||
|
32 | "3h15" => 3.25, | |||
|
33 | "3h 15" => 3.25, | |||
|
34 | "3 h 15" => 3.25, | |||
|
35 | "3 h 15m" => 3.25, | |||
|
36 | "3 h 15 m" => 3.25, | |||
|
37 | "3 hours" => 3.0, | |||
|
38 | "12min" => 0.2, | |||
|
39 | } | |||
|
40 | ||||
|
41 | assertions.each do |k, v| | |||
|
42 | t = TimeEntry.new(:hours => k) | |||
|
43 | assert_equal v, t.hours | |||
|
44 | end | |||
|
45 | end | |||
|
46 | end |
@@ -1,61 +1,77 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 |
# Copyright (C) 2006-200 |
|
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 => 'Enumeration', :foreign_key => :activity_id |
|
24 | belongs_to :activity, :class_name => 'Enumeration', :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 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on |
|
28 | validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on | |
29 | validates_numericality_of :hours, :allow_nil => true |
|
29 | validates_numericality_of :hours, :allow_nil => true | |
30 | validates_length_of :comments, :maximum => 255 |
|
30 | validates_length_of :comments, :maximum => 255 | |
31 |
|
31 | |||
32 | def before_validation |
|
32 | def before_validation | |
33 | self.project = issue.project if issue && project.nil? |
|
33 | self.project = issue.project if issue && project.nil? | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | def validate |
|
36 | def validate | |
37 | errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000) |
|
37 | errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000) | |
38 | errors.add :project_id, :activerecord_error_invalid if project.nil? |
|
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) |
|
39 | errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project) | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
|
42 | def hours=(h) | |||
|
43 | s = h.dup | |||
|
44 | if s.is_a?(String) | |||
|
45 | s.strip! | |||
|
46 | unless s =~ %r{^[\d\.,]+$} | |||
|
47 | # 2:30 => 2.5 | |||
|
48 | s.gsub!(%r{^(\d+):(\d+)$}) { $1.to_i + $2.to_i / 60.0 } | |||
|
49 | # 2h30, 2h, 30m | |||
|
50 | s.gsub!(%r{^((\d+)\s*(h|hours?))?\s*((\d+)\s*(m|min)?)?$}) { |m| ($1 || $4) ? ($2.to_i + $5.to_i / 60.0) : m[0] } | |||
|
51 | end | |||
|
52 | # 2,5 => 2.5 | |||
|
53 | s.gsub!(',', '.') | |||
|
54 | end | |||
|
55 | write_attribute :hours, s | |||
|
56 | end | |||
|
57 | ||||
42 | # tyear, tmonth, tweek assigned where setting spent_on attributes |
|
58 | # tyear, tmonth, tweek assigned where setting spent_on attributes | |
43 | # these attributes make time aggregations easier |
|
59 | # these attributes make time aggregations easier | |
44 | def spent_on=(date) |
|
60 | def spent_on=(date) | |
45 | super |
|
61 | super | |
46 | self.tyear = spent_on ? spent_on.year : nil |
|
62 | self.tyear = spent_on ? spent_on.year : nil | |
47 | self.tmonth = spent_on ? spent_on.month : nil |
|
63 | 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 |
|
64 | self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil | |
49 | end |
|
65 | end | |
50 |
|
66 | |||
51 | # Returns true if the time entry can be edited by usr, otherwise false |
|
67 | # Returns true if the time entry can be edited by usr, otherwise false | |
52 | def editable_by?(usr) |
|
68 | def editable_by?(usr) | |
53 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) |
|
69 | (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) | |
54 | end |
|
70 | end | |
55 |
|
71 | |||
56 | def self.visible_by(usr) |
|
72 | def self.visible_by(usr) | |
57 | with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do |
|
73 | with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do | |
58 | yield |
|
74 | yield | |
59 | end |
|
75 | end | |
60 | end |
|
76 | end | |
61 | end |
|
77 | end |
General Comments 0
You need to be logged in to leave comments.
Login now