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) ...@@ -13,6 +13,7 @@ v 8.5.0 (unreleased)
set it up set it up
- Fix diff comments loaded by AJAX to load comment with diff in discussion tab - Fix diff comments loaded by AJAX to load comment with diff in discussion tab
- Whitelist raw "abbr" elements when parsing Markdown (Benedict Etzel) - 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 - Don't vendor minified JS
- Display 404 error on group not found - Display 404 error on group not found
- Track project import failure - Track project import failure
......
...@@ -7,6 +7,8 @@ module LabelsHelper ...@@ -7,6 +7,8 @@ module LabelsHelper
# project - Project object which will be used as the context for the label's # 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 # link. If omitted, defaults to `@project`, or the label's own
# project. # 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 # block - An optional block that will be passed to `link_to`, forming the
# body of the link element. If omitted, defaults to # body of the link element. If omitted, defaults to
# `render_colored_label`. # `render_colored_label`.
...@@ -23,13 +25,18 @@ module LabelsHelper ...@@ -23,13 +25,18 @@ module LabelsHelper
# # Force the generated link to use a provided project # # Force the generated link to use a provided project
# link_to_label(label, project: Project.last) # 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 # # Customize link body with a block
# link_to_label(label) { "My Custom Label Text" } # link_to_label(label) { "My Custom Label Text" }
# #
# Returns a String # 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 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) label_name: label.name)
if block_given? if block_given?
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
- if merge_request.labels.any? - if merge_request.labels.any?
   
- merge_request.labels.each do |label| - 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? - if merge_request.tasks?
   
%span.task-status %span.task-status
......
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
.value.issuable-show-labels .value.issuable-show-labels
- if issuable.labels.any? - if issuable.labels.any?
- issuable.labels.each do |label| - issuable.labels.each do |label|
= link_to_label(label) = link_to_label(label, type: issuable.to_ability_name)
- else - else
.light None .light None
.selectbox .selectbox
......
...@@ -11,34 +11,31 @@ describe LabelsHelper do ...@@ -11,34 +11,31 @@ describe LabelsHelper do
end end
it 'uses the instance variable' do it 'uses the instance variable' do
expect(label).not_to receive(:project) expect(link_to_label(label)).to match %r{<a href="/#{@project.to_reference}/issues\?label_name=#{label.name}">.*</a>}
link_to_label(label)
end end
end end
context 'without @project set' do context 'without @project set' do
it "uses the label's project" do it "uses the label's project" do
expect(label).to receive(:project).and_return(project) expect(link_to_label(label)).to match %r{<a href="/#{label.project.to_reference}/issues\?label_name=#{label.name}">.*</a>}
link_to_label(label)
end end
end end
context 'with a named project argument' do context 'with a project argument' do
it 'uses the provided project' do let(:another_project) { double('project', namespace: 'foo3', to_param: 'bar3') }
arg = double('project')
expect(arg).to receive(:namespace).and_return('foo')
expect(arg).to receive(:to_param).and_return('foo')
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 end
it 'takes precedence over other types' do context 'with a type argument' do
@project = project ['issue', :issue, 'merge_request', :merge_request].each do |type|
expect(@project).not_to receive(:namespace) context "set to #{type}" do
expect(label).not_to receive(:project) 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>}
arg = double('project', namespace: 'foo', to_param: 'foo') end
link_to_label(label, project: arg) end
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