diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-11-12 15:50:31 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-11-12 15:50:31 +0300 |
commit | 9aa705dd38045b1121557faddf789ce8e114fc7d (patch) | |
tree | f5915719950036b47bd5c75e2dcaac3a5e8c380c /app/models/concerns/mentionable | |
parent | f5e3ce5ed612c9b247e362ed7c667557331e5bfd (diff) |
Turn reference regex constants into methods
`Mentionable::ReferenceRegexes` used to define the following two
constants:
1. DEFAULT_PATTERN
2. EXTERNAL_PATTERN
These two constants were built using some of the class methods that
reside in this same module. In EE we redefine one of these methods by
using `prepend` at the start of the `ReferenceRegexes` module. This
poses a problem: we can not move the `prepend` to the end of the file,
because the constants later on depend on it.
To resolve this problem, this commit turns these constants into class
methods that memoize their results. This allows EE to redefine the
appropriate methods before these two class methods are used, in turn
allowing us to move the `prepend` to the end of the file.
See https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/8198 for more
information.
Diffstat (limited to 'app/models/concerns/mentionable')
-rw-r--r-- | app/models/concerns/mentionable/reference_regexes.rb | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/app/models/concerns/mentionable/reference_regexes.rb b/app/models/concerns/mentionable/reference_regexes.rb index fe8fbb71184..b8fb3f71925 100644 --- a/app/models/concerns/mentionable/reference_regexes.rb +++ b/app/models/concerns/mentionable/reference_regexes.rb @@ -2,6 +2,8 @@ module Mentionable module ReferenceRegexes + extend Gitlab::Utils::StrongMemoize + def self.reference_pattern(link_patterns, issue_pattern) Regexp.union(link_patterns, issue_pattern, @@ -15,16 +17,20 @@ module Mentionable ] end - DEFAULT_PATTERN = begin - issue_pattern = Issue.reference_pattern - link_patterns = Regexp.union([Issue, Commit, MergeRequest, Epic].map(&:link_reference_pattern).compact) - reference_pattern(link_patterns, issue_pattern) + def self.default_pattern + strong_memoize(:default_pattern) do + issue_pattern = Issue.reference_pattern + link_patterns = Regexp.union([Issue, Commit, MergeRequest, Epic].map(&:link_reference_pattern).compact) + reference_pattern(link_patterns, issue_pattern) + end end - EXTERNAL_PATTERN = begin - issue_pattern = IssueTrackerService.reference_pattern - link_patterns = URI.regexp(%w(http https)) - reference_pattern(link_patterns, issue_pattern) + def self.external_pattern + strong_memoize(:external_pattern) do + issue_pattern = IssueTrackerService.reference_pattern + link_patterns = URI.regexp(%w(http https)) + reference_pattern(link_patterns, issue_pattern) + end end end end |