]> source.dussan.org Git - redmine.git/commitdiff
Issue macro for flexible linking to issues (#29489).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 20 Jun 2019 14:01:50 +0000 (14:01 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 20 Jun 2019 14:01:50 +0000 (14:01 +0000)
Patch by Jens Krämer.

git-svn-id: http://svn.redmine.org/redmine/trunk@18297 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/wiki_formatting/macros.rb
test/unit/lib/redmine/wiki_formatting/macros_test.rb

index 8afa0f8e72d164a0c6bbebb4a0c1fea8df3b9ea8..bf12cb1b0ca446216195ba4700f92e1699c56785 100644 (file)
@@ -254,6 +254,33 @@ module Redmine
           raise "Attachment #{filename} not found"
         end
       end
+
+      desc "Displays an issue link including additional information. Examples:\n\n" +
+             "{{issue(123)}}                              -- Issue #123: Enhance macro capabilities\n" +
+             "{{issue(123, project=true)}}                -- Andromeda - Issue #123: Enhance macro capabilities\n" +
+             "{{issue(123, tracker=false)}}               -- #123: Enhance macro capabilities\n" +
+             "{{issue(123, subject=false, project=true)}} -- Andromeda - Issue #123\n"
+      macro :issue do |obj, args|
+        args, options = extract_macro_options(args, :project, :subject, :tracker)
+        id = args.first
+        issue = Issue.visible.find_by(id: id)
+
+        if issue
+          # remove invalid options
+          options.delete_if { |k,v| v != 'true' && v != 'false' }
+
+          # turn string values into boolean
+          options.each do |k, v|
+            options[k] = v == 'true'
+          end
+
+          link_to_issue(issue, options)
+        else
+          # Fall back to regular issue link format to indicate, that there
+          # should have been something.
+          "##{id}"
+        end
+      end
     end
   end
 end
index b91638274d96516e3ea3136cefbc4e2891bbceeb..95b886e85bbb85951e9d600013f90d4663602a6c 100644 (file)
@@ -408,4 +408,14 @@ EXPECTED
     text = "*{{hello_world}}*"
     assert_match %r|\A<p><strong>Hello world!.*</strong></p>\z|, textilizable(text)
   end
+
+  def test_issue_macro_should_not_render_link_if_not_visible
+    assert_equal "<p>#123</p>", textilizable('{{issue(123)}}')
+  end
+
+  def test_issue_macro_should_render_link_to_issue
+    issue = Issue.find 1
+    assert_equal %{<p><a class="issue tracker-1 status-1 priority-4 priority-lowest" href="/issues/1">Bug #1</a>: #{issue.subject}</p>}, textilizable("{{issue(1)}}")
+    assert_equal %{<p>eCookbook - <a class="issue tracker-1 status-1 priority-4 priority-lowest" href="/issues/1">Bug #1</a>: #{issue.subject}</p>}, textilizable("{{issue(1, project=true)}}")
+  end
 end