From: Jean-Philippe Lang Date: Fri, 6 Jan 2012 19:50:02 +0000 (+0000) Subject: Extracted some code from #move_to_project to a callback. X-Git-Tag: 1.4.0~766 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b3c678da871332480526df6ec280f57730f09603;p=redmine.git Extracted some code from #move_to_project to a callback. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8524 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/issue.rb b/app/models/issue.rb index 4c7323c6d..f43ff0d83 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -75,6 +75,7 @@ class Issue < ActiveRecord::Base before_create :default_assign before_save :close_duplicates, :update_done_ratio_from_issue_status + after_save {|issue| issue.send :after_project_change if !issue.id_changed? && issue.project_id_changed?} after_save :reschedule_following_issues, :update_nested_set_attributes, :update_parent_attributes, :create_journal after_destroy :update_parent_attributes @@ -153,23 +154,7 @@ class Issue < ActiveRecord::Base end if new_project && issue.project_id != new_project.id - # delete issue relations - unless Setting.cross_project_issue_relations? - issue.relations_from.clear - issue.relations_to.clear - end - # issue is moved to another project - # reassign to the category with same name if any - new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name) - issue.category = new_category - # Keep the fixed_version if it's still valid in the new_project - unless new_project.shared_versions.include?(issue.fixed_version) - issue.fixed_version = nil - end issue.project = new_project - if issue.parent && issue.parent.project_id != issue.project_id - issue.parent_issue_id = nil - end end if new_tracker issue.tracker = new_tracker @@ -192,19 +177,7 @@ class Issue < ActiveRecord::Base issue.init_journal(User.current, options[:notes]) issue.current_journal.notify = false end - if issue.save - unless options[:copy] - # Manually update project_id on related time entries - TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id}) - - issue.children.each do |child| - unless child.move_to_project_without_transaction(new_project) - # Move failed and transaction was rollback'd - return false - end - end - end - else + unless issue.save return false end issue @@ -227,6 +200,32 @@ class Issue < ActiveRecord::Base result end + def project_id=(project_id) + if project_id.to_s != self.project_id.to_s + self.project = (project_id.present? ? Project.find_by_id(project_id) : nil) + end + end + + def project=(project) + project_was = self.project + write_attribute(:project_id, project ? project.id : nil) + association_instance_set('project', project) + if project_was && project && project_was != project + # Reassign to the category with same name if any + if category + self.category = project.issue_categories.find_by_name(category.name) + end + # Keep the fixed_version if it's still valid in the new_project + if fixed_version && fixed_version.project != project && !project.shared_versions.include?(fixed_version) + self.fixed_version = nil + end + if parent && parent.project_id != project_id + self.parent_issue_id = nil + end + @custom_field_values = nil + end + end + def description=(arg) if arg.is_a?(String) arg = arg.gsub(/(\r\n|\n|\r)/, "\r\n") @@ -763,6 +762,25 @@ class Issue < ActiveRecord::Base private + def after_project_change + # Update project_id on related time entries + TimeEntry.update_all(["project_id = ?", project_id], {:issue_id => id}) + + # Delete issue relations + unless Setting.cross_project_issue_relations? + relations_from.clear + relations_to.clear + end + + # Move subtasks + children.each do |child| + child.project = project + unless child.save + raise ActiveRecord::Rollback + end + end + end + def update_nested_set_attributes if root_id.nil? # issue was just created diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index 6d090ed52..dc08bbb00 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -406,6 +406,27 @@ class IssueTest < ActiveSupport::TestCase assert_equal orig.status, issue.status end + def test_should_not_call_after_project_change_on_creation + issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1) + issue.expects(:after_project_change).never + issue.save! + end + + def test_should_not_call_after_project_change_on_update + issue = Issue.find(1) + issue.project = Project.find(1) + issue.subject = 'No project change' + issue.expects(:after_project_change).never + issue.save! + end + + def test_should_call_after_project_change_on_project_change + issue = Issue.find(1) + issue.project = Project.find(2) + issue.expects(:after_project_change).once + issue.save! + end + def test_should_close_duplicates # Create 3 issues project = Project.find(1)