From 1f9e1ca3181b77388e88456354c72b2d85c080a3 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Sun, 19 May 2013 02:09:39 +0000 Subject: [PATCH] add journal after creating/deleting issue relation (#1005) git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11885 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/issues_helper.rb | 13 ++++++- app/models/issue_relation.rb | 28 +++++++++++++++ test/unit/helpers/issues_helper_test.rb | 32 ++++++++++++++++++ test/unit/issue_relation_test.rb | 45 +++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 39b24fe04..04c40d98d 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -306,6 +306,17 @@ module IssuesHelper end when 'attachment' label = l(:label_attachment) + when 'relation' + if detail.value && !detail.old_value + rel_issue = Issue.find_by_id(detail.value) + value = rel_issue.nil? ? "#{l(:label_issue)} #{detail.value}" : + (no_html ? rel_issue : link_to_issue(rel_issue)) + elsif detail.old_value && !detail.value + rel_issue = Issue.find_by_id(detail.old_value) + old_value = rel_issue.nil? ? "#{l(:label_issue)} #{detail.old_value}" : + (no_html ? rel_issue : link_to_issue(rel_issue)) + end + label = l(detail.prop_key.to_sym) end call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value }) @@ -353,7 +364,7 @@ module IssuesHelper else l(:text_journal_set_to, :label => label, :value => value).html_safe end - when 'attachment' + when 'attachment', 'relation' l(:text_journal_added, :label => label, :value => value).html_safe end else diff --git a/app/models/issue_relation.rb b/app/models/issue_relation.rb index 9e5015644..1de565011 100644 --- a/app/models/issue_relation.rb +++ b/app/models/issue_relation.rb @@ -72,6 +72,8 @@ class IssueRelation < ActiveRecord::Base attr_protected :issue_from_id, :issue_to_id before_save :handle_issue_order + after_create :create_journal_after_create + after_destroy :create_journal_after_delete def visible?(user=User.current) (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user)) @@ -179,4 +181,30 @@ class IssueRelation < ActiveRecord::Base self.relation_type = TYPES[relation_type][:reverse] end end + + def create_journal_after_create + journal = issue_from.init_journal(User.current) + journal.details << JournalDetail.new(:property => 'relation', + :prop_key => label_for(issue_from).to_s, + :value => issue_to.id) + journal.save + journal = issue_to.init_journal(User.current) + journal.details << JournalDetail.new(:property => 'relation', + :prop_key => label_for(issue_to).to_s, + :value => issue_from.id) + journal.save + end + + def create_journal_after_delete + journal = issue_from.init_journal(User.current) + journal.details << JournalDetail.new(:property => 'relation', + :prop_key => label_for(issue_from).to_s, + :old_value => issue_to.id) + journal.save + journal = issue_to.init_journal(User.current) + journal.details << JournalDetail.new(:property => 'relation', + :prop_key => label_for(issue_to).to_s, + :old_value => issue_from.id) + journal.save + end end diff --git a/test/unit/helpers/issues_helper_test.rb b/test/unit/helpers/issues_helper_test.rb index 327ca0710..c775bc865 100644 --- a/test/unit/helpers/issues_helper_test.rb +++ b/test/unit/helpers/issues_helper_test.rb @@ -210,4 +210,36 @@ class IssuesHelperTest < ActionView::TestCase :old_value => 'error281.txt', :value => nil) assert_match 'error281.txt', show_detail(detail, true) end + + def test_show_detail_create_relation + detail = JournalDetail.new(:property => 'relation', + :prop_key => 'label_precedes', + :value => 1) + assert_equal "Precedes Bug #1: Can't print recipes added", show_detail(detail, true) + assert_match %r{Precedes Bug #1: Can't print recipes added}, + show_detail(detail, false) + non_existed_issue_number = 9999 + assert_nil Issue.find_by_id(non_existed_issue_number) + detail = JournalDetail.new(:property => 'relation', + :prop_key => 'label_precedes', + :value => non_existed_issue_number) + assert_equal "Precedes Issue #{non_existed_issue_number} added", show_detail(detail, true) + assert_equal "Precedes Issue #{non_existed_issue_number} added", show_detail(detail, false) + end + + def test_show_detail_delete_relation + detail = JournalDetail.new(:property => 'relation', + :prop_key => 'label_precedes', + :old_value => 1) + assert_equal "Precedes deleted (Bug #1: Can't print recipes)", show_detail(detail, true) + assert_match %r{Precedes deleted \(Bug #1: Can't print recipes\)}, + show_detail(detail, false) + non_existed_issue_number = 9999 + assert_nil Issue.find_by_id(non_existed_issue_number) + detail = JournalDetail.new(:property => 'relation', + :prop_key => 'label_precedes', + :old_value => non_existed_issue_number) + assert_equal "Precedes deleted (Issue 9999)", show_detail(detail, true) + assert_equal "Precedes deleted (Issue 9999)", show_detail(detail, false) + end end diff --git a/test/unit/issue_relation_test.rb b/test/unit/issue_relation_test.rb index f0e74faf7..1b76056ee 100644 --- a/test/unit/issue_relation_test.rb +++ b/test/unit/issue_relation_test.rb @@ -167,4 +167,49 @@ class IssueRelationTest < ActiveSupport::TestCase assert !r.save assert_not_nil r.errors[:base] end + + def test_create_should_make_journal_entry + from = Issue.find(1) + to = Issue.find(2) + from_journals = from.journals.size + to_journals = to.journals.size + relation = IssueRelation.new(:issue_from => from, :issue_to => to, + :relation_type => IssueRelation::TYPE_PRECEDES) + assert relation.save + from.reload + to.reload + relation.reload + assert_equal from.journals.size, (from_journals + 1) + assert_equal to.journals.size, (to_journals + 1) + assert_equal 'relation', from.journals.last.details.last.property + assert_equal 'label_precedes', from.journals.last.details.last.prop_key + assert_equal '2', from.journals.last.details.last.value + assert_nil from.journals.last.details.last.old_value + assert_equal 'relation', to.journals.last.details.last.property + assert_equal 'label_follows', to.journals.last.details.last.prop_key + assert_equal '1', to.journals.last.details.last.value + assert_nil to.journals.last.details.last.old_value + end + + def test_delete_should_make_journal_entry + relation = IssueRelation.find(1) + id = relation.id + from = relation.issue_from + to = relation.issue_to + from_journals = from.journals.size + to_journals = to.journals.size + assert relation.destroy + from.reload + to.reload + assert_equal from.journals.size, (from_journals + 1) + assert_equal to.journals.size, (to_journals + 1) + assert_equal 'relation', from.journals.last.details.last.property + assert_equal 'label_blocks', from.journals.last.details.last.prop_key + assert_equal '9', from.journals.last.details.last.old_value + assert_nil from.journals.last.details.last.value + assert_equal 'relation', to.journals.last.details.last.property + assert_equal 'label_blocked_by', to.journals.last.details.last.prop_key + assert_equal '10', to.journals.last.details.last.old_value + assert_nil to.journals.last.details.last.value + end end