summaryrefslogtreecommitdiff
path: root/lib/banzai/filter/issuable_state_filter.rb
diff options
context:
space:
mode:
authorAdam Buckland <adamjbuckland@gmail.com>2016-08-24 18:11:48 +0100
committerDouwe Maan <douwe@selenight.nl>2017-04-07 14:31:43 -0500
commitace833b31dfac64a8b44242ce7d91c60285bf983 (patch)
tree65868bc5cb28f228c09ad5dccb477e5769a39122 /lib/banzai/filter/issuable_state_filter.rb
parent5d4449151fb576dc927ff1d0ff343fca4645159b (diff)
downloadgitlab-ce-ace833b31dfac64a8b44242ce7d91c60285bf983.tar.gz
Add indication for closed or merged issuables in GFMadam-finish-5993-closed-issuable
Example: for issues that are closed, the links will now show '[closed]' following the issue number. This is done as post-process after the markdown has been loaded from the cache as the status of the issue may change between the cache being populated and the content being displayed. In order to avoid N+1 queries problem when rendering notes ObjectRenderer populates the cache of referenced issuables for all notes at once, before the post processing phase. As a part of this change, the Banzai BaseParser#grouped_objects_for_nodes method has been refactored to return a Hash utilising the node itself as the key, since this was a common pattern of usage for this method.
Diffstat (limited to 'lib/banzai/filter/issuable_state_filter.rb')
-rw-r--r--lib/banzai/filter/issuable_state_filter.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/banzai/filter/issuable_state_filter.rb b/lib/banzai/filter/issuable_state_filter.rb
new file mode 100644
index 00000000000..6b78aa795b4
--- /dev/null
+++ b/lib/banzai/filter/issuable_state_filter.rb
@@ -0,0 +1,35 @@
+module Banzai
+ module Filter
+ # HTML filter that appends state information to issuable links.
+ # Runs as a post-process filter as issuable state might change whilst
+ # Markdown is in the cache.
+ #
+ # This filter supports cross-project references.
+ class IssuableStateFilter < HTML::Pipeline::Filter
+ VISIBLE_STATES = %w(closed merged).freeze
+
+ def call
+ extractor = Banzai::IssuableExtractor.new(project, current_user)
+ issuables = extractor.extract([doc])
+
+ issuables.each do |node, issuable|
+ if VISIBLE_STATES.include?(issuable.state)
+ node.children.last.content += " [#{issuable.state}]"
+ end
+ end
+
+ doc
+ end
+
+ private
+
+ def current_user
+ context[:current_user]
+ end
+
+ def project
+ context[:project]
+ end
+ end
+ end
+end