Commit 6e79ce2e authored by Rémy Coutable's avatar Rémy Coutable

Allow link_to_label to take an optional type arg

You can set type: :merge_request to create a link to merge request
lists instead of issues list. Accept both Symbol & String as type
argument for the link_to_label helper
parent 3720b98e
......@@ -13,6 +13,7 @@ v 8.5.0 (unreleased)
set it up
- Fix diff comments loaded by AJAX to load comment with diff in discussion tab
- Whitelist raw "abbr" elements when parsing Markdown (Benedict Etzel)
- Fix label links for a merge request pointing to issues list
- Don't vendor minified JS
- Display 404 error on group not found
- Track project import failure
......
......@@ -7,6 +7,8 @@ module LabelsHelper
# project - Project object which will be used as the context for the label's
# link. If omitted, defaults to `@project`, or the label's own
# project.
# type - The type of item the link will point to (:issue or
# :merge_request). If omitted, defaults to :issue.
# block - An optional block that will be passed to `link_to`, forming the
# body of the link element. If omitted, defaults to
# `render_colored_label`.
......@@ -23,13 +25,18 @@ module LabelsHelper
# # Force the generated link to use a provided project
# link_to_label(label, project: Project.last)
#
# # Force the generated link to point to merge requests instead of issues
# link_to_label(label, type: :merge_request)
#
# # Customize link body with a block
# link_to_label(label) { "My Custom Label Text" }
#
# Returns a String
def link_to_label(label, project: nil, &block)
def link_to_label(label, project: nil, type: :issue, &block)
project ||= @project || label.project
link = namespace_project_issues_path(project.namespace, project,
link = send("namespace_project_#{type.to_s.pluralize}_path",
project.namespace,
project,
label_name: label.name)
if block_given?
......
......@@ -53,7 +53,7 @@
- if merge_request.labels.any?
 
- merge_request.labels.each do |label|
= link_to_label(label, project: merge_request.project)
= link_to_label(label, project: merge_request.project, type: 'merge_request')
- if merge_request.tasks?
 
%span.task-status
......
......@@ -90,7 +90,7 @@
.value.issuable-show-labels
- if issuable.labels.any?
- issuable.labels.each do |label|
= link_to_label(label)
= link_to_label(label, type: issuable.to_ability_name)
- else
.light None
.selectbox
......
......@@ -11,34 +11,31 @@ describe LabelsHelper do
end
it 'uses the instance variable' do
expect(label).not_to receive(:project)
link_to_label(label)
expect(link_to_label(label)).to match %r{<a href="/#{@project.to_reference}/issues\?label_name=#{label.name}">.*</a>}
end
end
context 'without @project set' do
it "uses the label's project" do
expect(label).to receive(:project).and_return(project)
link_to_label(label)
expect(link_to_label(label)).to match %r{<a href="/#{label.project.to_reference}/issues\?label_name=#{label.name}">.*</a>}
end
end
context 'with a named project argument' do
it 'uses the provided project' do
arg = double('project')
expect(arg).to receive(:namespace).and_return('foo')
expect(arg).to receive(:to_param).and_return('foo')
context 'with a project argument' do
let(:another_project) { double('project', namespace: 'foo3', to_param: 'bar3') }
link_to_label(label, project: arg)
it 'links to merge requests page' do
expect(link_to_label(label, project: another_project)).to match %r{<a href="/foo3/bar3/issues\?label_name=#{label.name}">.*</a>}
end
end
it 'takes precedence over other types' do
@project = project
expect(@project).not_to receive(:namespace)
expect(label).not_to receive(:project)
arg = double('project', namespace: 'foo', to_param: 'foo')
link_to_label(label, project: arg)
context 'with a type argument' do
['issue', :issue, 'merge_request', :merge_request].each do |type|
context "set to #{type}" do
it 'links to correct page' do
expect(link_to_label(label, type: type)).to match %r{<a href="/#{label.project.to_reference}/#{type.to_s.pluralize}\?label_name=#{label.name}">.*</a>}
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment