2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-05-09 19:02:59 -04:00
|
|
|
# == Referable concern
|
|
|
|
#
|
|
|
|
# Contains functionality related to making a model referable in Markdown, such
|
|
|
|
# as "#1", "!2", "~3", etc.
|
|
|
|
module Referable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# Returns the String necessary to reference this object in Markdown
|
|
|
|
#
|
2017-11-22 08:20:35 -05:00
|
|
|
# from - Referring parent object
|
2015-05-09 19:02:59 -04:00
|
|
|
#
|
|
|
|
# This should be overridden by the including class.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# Issue.first.to_reference # => "#1"
|
|
|
|
# Issue.last.to_reference(other_project) # => "cross-project#1"
|
|
|
|
#
|
|
|
|
# Returns a String
|
2017-11-22 08:20:35 -05:00
|
|
|
def to_reference(_from = nil, full:)
|
2015-05-09 19:02:59 -04:00
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2020-01-29 07:09:08 -05:00
|
|
|
# If this referable object can serve as the base for the
|
|
|
|
# reference of child objects (e.g. projects are the base of
|
|
|
|
# issues), but it is formatted differently, then you may wish
|
|
|
|
# to override this method.
|
|
|
|
def to_reference_base(from = nil, full:)
|
|
|
|
to_reference(from, full: full)
|
|
|
|
end
|
|
|
|
|
2017-11-22 08:20:35 -05:00
|
|
|
def reference_link_text(from = nil)
|
|
|
|
to_reference(from)
|
2015-12-01 06:58:45 -05:00
|
|
|
end
|
|
|
|
|
2017-08-16 15:02:38 -04:00
|
|
|
included do
|
|
|
|
alias_method :non_referable_inspect, :inspect
|
|
|
|
alias_method :inspect, :referable_inspect
|
|
|
|
end
|
|
|
|
|
2017-08-01 16:20:22 -04:00
|
|
|
def referable_inspect
|
|
|
|
if respond_to?(:id)
|
|
|
|
"#<#{self.class.name} id:#{id} #{to_reference(full: true)}>"
|
|
|
|
else
|
|
|
|
"#<#{self.class.name} #{to_reference(full: true)}>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 08:35:31 -04:00
|
|
|
class_methods do
|
2015-05-09 19:02:59 -04:00
|
|
|
# The character that prefixes the actual reference identifier
|
|
|
|
#
|
|
|
|
# This should be overridden by the including class.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# Issue.reference_prefix # => '#'
|
|
|
|
# MergeRequest.reference_prefix # => '!'
|
|
|
|
#
|
|
|
|
# Returns a String
|
|
|
|
def reference_prefix
|
|
|
|
''
|
|
|
|
end
|
2015-05-14 16:59:39 -04:00
|
|
|
|
|
|
|
# Regexp pattern used to match references to this object
|
|
|
|
#
|
|
|
|
# This must be overridden by the including class.
|
|
|
|
#
|
2015-05-14 17:09:02 -04:00
|
|
|
# Returns a Regexp
|
2015-05-14 16:59:39 -04:00
|
|
|
def reference_pattern
|
2015-05-15 16:09:17 -04:00
|
|
|
raise NotImplementedError, "#{self} does not implement #{__method__}"
|
2015-05-14 16:59:39 -04:00
|
|
|
end
|
2015-11-30 15:14:46 -05:00
|
|
|
|
2016-06-18 13:55:45 -04:00
|
|
|
def reference_valid?(reference)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-11-30 15:14:46 -05:00
|
|
|
def link_reference_pattern(route, pattern)
|
|
|
|
%r{
|
|
|
|
(?<url>
|
|
|
|
#{Regexp.escape(Gitlab.config.gitlab.url)}
|
|
|
|
\/#{Project.reference_pattern}
|
2019-05-29 07:34:00 -04:00
|
|
|
(?:\/\-)?
|
2020-01-17 19:09:18 -05:00
|
|
|
\/#{route.is_a?(Regexp) ? route : Regexp.escape(route)}
|
2015-11-30 15:14:46 -05:00
|
|
|
\/#{pattern}
|
|
|
|
(?<path>
|
2020-09-24 05:09:35 -04:00
|
|
|
(\/[a-z0-9_=-]+)*\/*
|
2015-11-30 15:14:46 -05:00
|
|
|
)?
|
|
|
|
(?<query>
|
|
|
|
\?[a-z0-9_=-]+
|
|
|
|
(&[a-z0-9_=-]+)*
|
|
|
|
)?
|
|
|
|
(?<anchor>\#[a-z0-9_-]+)?
|
|
|
|
)
|
|
|
|
}x
|
|
|
|
end
|
2015-05-09 19:02:59 -04:00
|
|
|
end
|
|
|
|
end
|