diff --git a/app/models/issue.rb b/app/models/issue.rb index d6eab02fe..8082e43b7 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -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 diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index 0dce253c7..ddaff2b60 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -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 diff --git a/lib/redmine.rb b/lib/redmine.rb index 5443eef4a..2697e8f5f 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -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 index 000000000..573313e74 --- /dev/null +++ b/lib/redmine/core_ext.rb @@ -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 index 000000000..ce2646fb9 --- /dev/null +++ b/lib/redmine/core_ext/string.rb @@ -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 index 000000000..7444445b0 --- /dev/null +++ b/lib/redmine/core_ext/string/conversions.rb @@ -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 diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 3ceba1851..36ba1fb45 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -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