summaryrefslogtreecommitdiff
path: root/app/models/group_label.rb
blob: c7efa29a5f6a3b37d6889fc54571a5f5a5f26281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class GroupLabel < Label
  belongs_to :group

  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
  end
end