##// END OF EJS Templates
Fixes Issue#save_issue_with_child_records so that time entry do not get saved if issue save fails....
Jean-Philippe Lang -
r3550:f35921d3080f
parent child
Show More
@@ -484,34 +484,35 class Issue < ActiveRecord::Base
484 # Saves an issue, time_entry, attachments, and a journal from the parameters
484 # Saves an issue, time_entry, attachments, and a journal from the parameters
485 # Returns false if save fails
485 # Returns false if save fails
486 def save_issue_with_child_records(params, existing_time_entry=nil)
486 def save_issue_with_child_records(params, existing_time_entry=nil)
487 if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
487 Issue.transaction do
488 @time_entry = existing_time_entry || TimeEntry.new
488 if params[:time_entry] && params[:time_entry][:hours].present? && User.current.allowed_to?(:log_time, project)
489 @time_entry.project = project
489 @time_entry = existing_time_entry || TimeEntry.new
490 @time_entry.issue = self
490 @time_entry.project = project
491 @time_entry.user = User.current
491 @time_entry.issue = self
492 @time_entry.spent_on = Date.today
492 @time_entry.user = User.current
493 @time_entry.attributes = params[:time_entry]
493 @time_entry.spent_on = Date.today
494 self.time_entries << @time_entry
494 @time_entry.attributes = params[:time_entry]
495 end
495 self.time_entries << @time_entry
496
496 end
497 if valid?
497
498 attachments = Attachment.attach_files(self, params[:attachments])
498 if valid?
499
499 attachments = Attachment.attach_files(self, params[:attachments])
500 attachments[:files].each {|a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
500
501 # TODO: Rename hook
501 attachments[:files].each {|a| @current_journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
502 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
502 # TODO: Rename hook
503 begin
503 Redmine::Hook.call_hook(:controller_issues_edit_before_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
504 if save
504 begin
505 # TODO: Rename hook
505 if save
506 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
506 # TODO: Rename hook
507 return true
507 Redmine::Hook.call_hook(:controller_issues_edit_after_save, { :params => params, :issue => self, :time_entry => @time_entry, :journal => @current_journal})
508 else
508 else
509 return false
509 raise ActiveRecord::Rollback
510 end
511 rescue ActiveRecord::StaleObjectError
512 attachments[:files].each(&:destroy)
513 errors.add_to_base l(:notice_locking_conflict)
514 raise ActiveRecord::Rollback
510 end
515 end
511 rescue ActiveRecord::StaleObjectError
512 attachments[:files].each(&:destroy)
513 errors.add_to_base l(:notice_locking_conflict)
514 return false
515 end
516 end
516 end
517 end
517 end
518 end
@@ -827,7 +827,7 class IssuesControllerTest < ActionController::TestCase
827 put :update,
827 put :update,
828 :id => 1,
828 :id => 1,
829 :notes => '2.5 hours added',
829 :notes => '2.5 hours added',
830 :time_entry => { :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first }
830 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first }
831 end
831 end
832 assert_redirected_to :action => 'show', :id => '1'
832 assert_redirected_to :action => 'show', :id => '1'
833
833
@@ -837,7 +837,7 class IssuesControllerTest < ActionController::TestCase
837 assert_equal '2.5 hours added', j.notes
837 assert_equal '2.5 hours added', j.notes
838 assert_equal 0, j.details.size
838 assert_equal 0, j.details.size
839
839
840 t = issue.time_entries.find(:first, :order => 'id DESC')
840 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
841 assert_not_nil t
841 assert_not_nil t
842 assert_equal 2.5, t.hours
842 assert_equal 2.5, t.hours
843 assert_equal spent_hours_before + 2.5, issue.spent_hours
843 assert_equal spent_hours_before + 2.5, issue.spent_hours
@@ -985,15 +985,18 class IssuesControllerTest < ActionController::TestCase
985 @request.session[:user_id] = 2
985 @request.session[:user_id] = 2
986
986
987 assert_no_difference 'Journal.count' do
987 assert_no_difference 'Journal.count' do
988 assert_no_difference 'Attachment.count' do
988 assert_no_difference 'TimeEntry.count' do
989 put :update,
989 assert_no_difference 'Attachment.count' do
990 :id => issue.id,
990 put :update,
991 :issue => {
991 :id => issue.id,
992 :fixed_version_id => 4,
992 :issue => {
993 :lock_version => (issue.lock_version - 1)
993 :fixed_version_id => 4,
994 },
994 :lock_version => (issue.lock_version - 1)
995 :notes => '',
995 },
996 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
996 :notes => '',
997 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}},
998 :time_entry => { :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first }
999 end
997 end
1000 end
998 end
1001 end
999
1002
General Comments 0
You need to be logged in to leave comments. Login now