Move common logic to reference group/project label to Label#to_reference

parent 247859c8
......@@ -3,32 +3,7 @@ class GroupLabel < Label
validates :group, presence: true
##
# Returns the String necessary to reference this GroupLabel in Markdown
#
# format - Symbol format to use (default: :id, optional: :name)
#
# Examples:
#
# GroupLabel.first.to_reference # => "~1"
# GroupLabel.first.to_reference(format: :name) # => "~\"bug\""
#
# Returns a String
#
def to_reference(source_project = nil, target_project = nil, format: :id)
format_reference = label_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
if cross_project_reference?(source_project, target_project)
source_project.to_reference + reference
else
reference
end
end
private
def cross_project_reference?(source_project, target_project)
source_project && target_project && source_project != target_project
super(source_project, target_project, format: format)
end
end
......@@ -97,8 +97,36 @@ class Label < ActiveRecord::Base
write_attribute(:title, sanitize_title(value)) if value.present?
end
##
# Returns the String necessary to reference this Label in Markdown
#
# format - Symbol format to use (default: :id, optional: :name)
#
# Examples:
#
# Label.first.to_reference # => "~1"
# Label.first.to_reference(format: :name) # => "~\"bug\""
# Label.first.to_reference(project1, project2) # => "gitlab-org/gitlab-ce~1"
#
# Returns a String
#
def to_reference(source_project = nil, target_project = nil, format: :id)
format_reference = label_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
if cross_project_reference?(source_project, target_project)
source_project.to_reference + reference
else
reference
end
end
private
def cross_project_reference?(source_project, target_project)
source_project && target_project && source_project != target_project
end
def issues_count(user, params = {})
IssuesFinder.new(user, { label_name: title, scope: 'all' }.merge(params))
.execute
......
......@@ -7,28 +7,8 @@ class ProjectLabel < Label
delegate :group, to: :project, allow_nil: true
##
# Returns the String necessary to reference this ProjectLabel in Markdown
#
# format - Symbol format to use (default: :id, optional: :name)
#
# Examples:
#
# ProjectLabel.first.to_reference # => "~1"
# ProjectLabel.first.to_reference(format: :name) # => "~\"bug\""
# ProjectLabel.first.to_reference(project) # => "gitlab-org/gitlab-ce~1"
#
# Returns a String
#
def to_reference(from_project = nil, format: :id)
format_reference = label_format_reference(format)
reference = "#{self.class.reference_prefix}#{format_reference}"
if cross_project_reference?(from_project)
project.to_reference + reference
else
reference
end
def to_reference(target_project = nil, format: :id)
super(project, target_project, format: format)
end
private
......
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