@@ -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,5 +1,5 | |||||
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 | |
@@ -39,6 +39,22 class TimeEntry < ActiveRecord::Base | |||||
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) |
General Comments 0
You need to be logged in to leave comments.
Login now