2016-01-14 18:37:52 +05:30
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that replaces milestone references with links.
|
|
|
|
class MilestoneReferenceFilter < AbstractReferenceFilter
|
2016-06-16 23:09:34 +05:30
|
|
|
self.reference_type = :milestone
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def self.object_class
|
|
|
|
Milestone
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_object(project, id)
|
|
|
|
project.milestones.find_by(iid: id)
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def references_in(text, pattern = Milestone.reference_pattern)
|
|
|
|
# We'll handle here the references that follow the `reference_pattern`.
|
|
|
|
# Other patterns (for example, the link pattern) are handled by the
|
|
|
|
# default implementation.
|
|
|
|
return super(text, pattern) if pattern != Milestone.reference_pattern
|
|
|
|
|
|
|
|
text.gsub(pattern) do |match|
|
|
|
|
milestone = find_milestone($~[:project], $~[:milestone_iid], $~[:milestone_name])
|
|
|
|
|
|
|
|
if milestone
|
|
|
|
yield match, milestone.iid, $~[:project], $~
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_milestone(project_ref, milestone_id, milestone_name)
|
|
|
|
project = project_from_ref(project_ref)
|
|
|
|
return unless project
|
|
|
|
|
|
|
|
milestone_params = milestone_params(milestone_id, milestone_name)
|
|
|
|
project.milestones.find_by(milestone_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def milestone_params(iid, name)
|
|
|
|
if name
|
|
|
|
{ name: name.tr('"', '') }
|
|
|
|
else
|
|
|
|
{ iid: iid.to_i }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_object(milestone, project)
|
|
|
|
h = Gitlab::Routing.url_helpers
|
2016-01-14 18:37:52 +05:30
|
|
|
h.namespace_project_milestone_url(project.namespace, project, milestone,
|
|
|
|
only_path: context[:only_path])
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
def object_link_text(object, matches)
|
|
|
|
if context[:project] == object.project
|
|
|
|
super
|
|
|
|
else
|
|
|
|
"#{escape_once(super)} <i>in #{escape_once(object.project.path_with_namespace)}</i>".
|
|
|
|
html_safe
|
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
def object_link_title(object)
|
|
|
|
nil
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|