]> source.dussan.org Git - redmine.git/commitdiff
Estimated time recognizes improved time formats (#1092).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 26 Apr 2008 11:59:51 +0000 (11:59 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 26 Apr 2008 11:59:51 +0000 (11:59 +0000)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1361 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/issue.rb
app/models/time_entry.rb
lib/redmine.rb
lib/redmine/core_ext.rb [new file with mode: 0644]
lib/redmine/core_ext/string.rb [new file with mode: 0644]
lib/redmine/core_ext/string/conversions.rb [new file with mode: 0644]
test/unit/issue_test.rb

index d6eab02fe0abf54a2b108b96eebaec2fc94d342d..8082e43b798733f6ff2097d15c3b7122590eb0a6 100644 (file)
@@ -93,7 +93,11 @@ class Issue < ActiveRecord::Base
     self.priority = nil
     write_attribute(:priority_id, pid)
   end
-
+  
+  def estimated_hours=(h)
+    write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
+  end
+  
   def validate
     if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
       errors.add :due_date, :activerecord_error_not_a_date
index 0dce253c7f4539561bc452908b6411ac80e4be6f..ddaff2b6061f4b5390775597221e4bfd041394fe 100644 (file)
@@ -40,19 +40,7 @@ class TimeEntry < ActiveRecord::Base
   end
   
   def hours=(h)
-    s = h.dup
-    if s.is_a?(String)
-      s.strip!
-      unless s =~ %r{^[\d\.,]+$}
-        # 2:30 => 2.5
-        s.gsub!(%r{^(\d+):(\d+)$}) { $1.to_i + $2.to_i / 60.0 }
-        # 2h30, 2h, 30m
-        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] }
-      end
-      # 2,5 => 2.5
-      s.gsub!(',', '.')
-    end
-    write_attribute :hours, s
+    write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
   end
   
   # tyear, tmonth, tweek assigned where setting spent_on attributes
index 5443eef4a6cfd9ee73c5a191bf82d593fdb495dd..2697e8f5fbd01f42eff2aaabdd678b1984fea408 100644 (file)
@@ -1,6 +1,7 @@
 require 'redmine/access_control'
 require 'redmine/menu_manager'
 require 'redmine/mime_type'
+require 'redmine/core_ext'
 require 'redmine/themes'
 require 'redmine/plugin'
 
diff --git a/lib/redmine/core_ext.rb b/lib/redmine/core_ext.rb
new file mode 100644 (file)
index 0000000..573313e
--- /dev/null
@@ -0,0 +1 @@
+Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each { |file| require(file) }
diff --git a/lib/redmine/core_ext/string.rb b/lib/redmine/core_ext/string.rb
new file mode 100644 (file)
index 0000000..ce2646f
--- /dev/null
@@ -0,0 +1,5 @@
+require File.dirname(__FILE__) + '/string/conversions'
+
+class String #:nodoc:
+  include Redmine::CoreExtensions::String::Conversions
+end
diff --git a/lib/redmine/core_ext/string/conversions.rb b/lib/redmine/core_ext/string/conversions.rb
new file mode 100644 (file)
index 0000000..7444445
--- /dev/null
@@ -0,0 +1,40 @@
+# redMine - project management software
+# Copyright (C) 2008  Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+module Redmine #:nodoc:
+  module CoreExtensions #:nodoc:
+    module String #:nodoc:
+      # Custom string conversions
+      module Conversions
+        # Parses hours format and returns a float
+        def to_hours
+          s = self.dup
+          s.strip!
+          unless s =~ %r{^[\d\.,]+$}
+            # 2:30 => 2.5
+            s.gsub!(%r{^(\d+):(\d+)$}) { $1.to_i + $2.to_i / 60.0 }
+            # 2h30, 2h, 30m => 2.5, 2, 0.5
+            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] }
+          end
+          # 2,5 => 2.5
+          s.gsub!(',', '.')
+          s.to_f
+        end
+      end
+    end
+  end
+end
index 3ceba185103db678fe81a1d33fe4b59cf84f2327..36ba1fb4544d852bcae35879f60a72a00abbcfcb 100644 (file)
@@ -20,6 +20,13 @@ require File.dirname(__FILE__) + '/../test_helper'
 class IssueTest < Test::Unit::TestCase
   fixtures :projects, :users, :members, :trackers, :projects_trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :time_entries
 
+  def test_create
+    issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
+    assert issue.save
+    issue.reload
+    assert_equal 1.5, issue.estimated_hours
+  end
+  
   def test_category_based_assignment
     issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
     assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to