:description => :comments
validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
- validates_numericality_of :hours, :allow_nil => true
+ validates_numericality_of :hours, :allow_nil => true, :message => :activerecord_error_invalid
validates_length_of :comments, :maximum => 255, :allow_nil => true
def after_initialize
end
def hours=(h)
- write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
+ write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
end
# tyear, tmonth, tweek assigned where setting spent_on attributes
def to_hours
s = self.dup
s.strip!
- unless s =~ %r{^[\d\.,]+$}
+ if s =~ %r{^(\d+([.,]\d+)?)h?$}
+ s = $1
+ else
# 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
# No email should be sent
assert ActionMailer::Base.deliveries.empty?
end
+
+ def test_post_edit_with_invalid_spent_time
+ @request.session[:user_id] = 2
+ notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
+
+ assert_no_difference('Journal.count') do
+ post :edit,
+ :id => 1,
+ :notes => notes,
+ :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
+ end
+ assert_response :success
+ assert_template 'edit'
+
+ assert_tag :textarea, :attributes => { :name => 'notes' },
+ :content => notes
+ assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
+ end
def test_bulk_edit
@request.session[:user_id] = 2
assertions = { "2" => 2.0,
"21.1" => 21.1,
"2,1" => 2.1,
+ "1,5h" => 1.5,
"7:12" => 7.2,
"10h" => 10.0,
"10 h" => 10.0,
assertions.each do |k, v|
t = TimeEntry.new(:hours => k)
- assert_equal v, t.hours
+ assert_equal v, t.hours, "Converting #{k} failed:"
end
end
end