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