Commit 5c31f4f4 authored by Brett Walker's avatar Brett Walker

Change several class methods to instance methods

to remove unused code
parent e036c814
......@@ -11,7 +11,6 @@ module EE
module EpicReferenceFilter
extend ActiveSupport::Concern
class_methods do
def references_in(text, pattern = object_class.reference_pattern)
text.gsub(pattern) do |match|
symbol = $~[object_sym]
......@@ -22,7 +21,6 @@ module EE
end
end
end
end
def url_for_object(epic, group)
urls = ::Gitlab::Routing.url_helpers
......
......@@ -11,7 +11,6 @@ module EE
module VulnerabilityReferenceFilter
extend ActiveSupport::Concern
class_methods do
def references_in(text, pattern = object_class.reference_pattern)
text.gsub(pattern) do |match|
symbol = $~[object_sym]
......@@ -22,7 +21,6 @@ module EE
end
end
end
end
def unescape_link(href)
return href if href =~ object_class.reference_pattern
......
......@@ -16,22 +16,9 @@ module Banzai
REFERENCE_PLACEHOLDER = "_reference_#{SecureRandom.hex(16)}_"
REFERENCE_PLACEHOLDER_PATTERN = %r{#{REFERENCE_PLACEHOLDER}(\d+)}.freeze
def self.object_class
# Implement in child class
# Example: MergeRequest
end
def self.object_name
@object_name ||= object_class.name.underscore
end
def self.object_sym
@object_sym ||= object_name.to_sym
end
# Public: Find references in text (like `!123` for merge requests)
#
# AnyReferenceFilter.references_in(text) do |match, id, project_ref, matches|
# references_in(text) do |match, id, project_ref, matches|
# object = find_object(project_ref, id)
# "<a href=...>#{object.to_reference}</a>"
# end
......@@ -42,7 +29,7 @@ module Banzai
# of the external project reference, and all of the matchdata.
#
# Returns a String replaced with the return of the block.
def self.references_in(text, pattern = object_class.reference_pattern)
def references_in(text, pattern = object_class.reference_pattern)
text.gsub(pattern) do |match|
if ident = identifier($~)
yield match, ident, $~[:project], $~[:namespace], $~
......@@ -52,17 +39,13 @@ module Banzai
end
end
def self.identifier(match_data)
def identifier(match_data)
symbol = symbol_from_match(match_data)
parse_symbol(symbol, match_data) if object_class.reference_valid?(symbol)
end
def identifier(match_data)
self.class.identifier(match_data)
end
def self.symbol_from_match(match)
def symbol_from_match(match)
key = object_sym
match[key] if match.names.include?(key.to_s)
end
......@@ -72,7 +55,7 @@ module Banzai
#
# This method has the contract that if a string `ref` refers to a
# record `record`, then `parse_symbol(ref) == record_identifier(record)`.
def self.parse_symbol(symbol, match_data)
def parse_symbol(symbol, match_data)
symbol.to_i
end
......@@ -84,21 +67,10 @@ module Banzai
record.id
end
def object_class
self.class.object_class
end
def object_sym
self.class.object_sym
end
def references_in(*args, &block)
self.class.references_in(*args, &block)
end
# Implement in child class
# Example: project.merge_requests.find
def find_object(parent_object, id)
raise NotImplementedError, "#{self.class} must implement method: #{__callee__}"
end
# Override if the link reference pattern produces a different ID (global
......@@ -110,6 +82,7 @@ module Banzai
# Implement in child class
# Example: project_merge_request_url
def url_for_object(object, parent_object)
raise NotImplementedError, "#{self.class} must implement method: #{__callee__}"
end
def find_object_cached(parent_object, id)
......@@ -425,14 +398,6 @@ module Banzai
group_ref
end
def unescape_html_entities(text)
CGI.unescapeHTML(text.to_s)
end
def escape_html_entities(text)
CGI.escapeHTML(text.to_s)
end
def escape_with_placeholders(text, placeholder_data)
escaped = escape_html_entities(text)
......
......@@ -5,12 +5,9 @@ module Banzai
module References
class AlertReferenceFilter < IssuableReferenceFilter
self.reference_type = :alert
self.object_class = AlertManagement::Alert
def self.object_class
AlertManagement::Alert
end
def self.object_sym
def object_sym
:alert
end
......
......@@ -8,12 +8,9 @@ module Banzai
# This filter supports cross-project references.
class CommitRangeReferenceFilter < AbstractReferenceFilter
self.reference_type = :commit_range
self.object_class = CommitRange
def self.object_class
CommitRange
end
def self.references_in(text, pattern = CommitRange.reference_pattern)
def references_in(text, pattern = object_reference_pattern)
text.gsub(pattern) do |match|
yield match, $~[:commit_range], $~[:project], $~[:namespace], $~
end
......
......@@ -8,12 +8,9 @@ module Banzai
# This filter supports cross-project references.
class CommitReferenceFilter < AbstractReferenceFilter
self.reference_type = :commit
self.object_class = Commit
def self.object_class
Commit
end
def self.references_in(text, pattern = Commit.reference_pattern)
def references_in(text, pattern = object_reference_pattern)
text.gsub(pattern) do |match|
yield match, $~[:commit], $~[:project], $~[:namespace], $~
end
......@@ -39,7 +36,7 @@ module Banzai
end
# The default behaviour is `#to_i` - we just pass the hash through.
def self.parse_symbol(sha_hash, _match)
def parse_symbol(sha_hash, _match)
sha_hash
end
......
......@@ -33,6 +33,7 @@ module Banzai
end
self.reference_type = :design
self.object_class = ::DesignManagement::Design
def find_object(project, identifier)
records_per_parent[project][identifier]
......@@ -76,15 +77,11 @@ module Banzai
super.merge(issue: design.issue_id)
end
def self.object_class
::DesignManagement::Design
end
def self.object_sym
def object_sym
:design
end
def self.parse_symbol(raw, match_data)
def parse_symbol(raw, match_data)
filename = match_data[:url_filename]
iid = match_data[:issue].to_i
Identifier.new(filename: CGI.unescape(filename), issue_iid: iid)
......
......@@ -14,7 +14,7 @@ module Banzai
# Public: Find `JIRA-123` issue references in text
#
# ExternalIssueReferenceFilter.references_in(text, pattern) do |match, issue|
# references_in(text, pattern) do |match, issue|
# "<a href=...>##{issue}</a>"
# end
#
......
......@@ -5,12 +5,9 @@ module Banzai
module References
class FeatureFlagReferenceFilter < IssuableReferenceFilter
self.reference_type = :feature_flag
self.object_class = Operations::FeatureFlag
def self.object_class
Operations::FeatureFlag
end
def self.object_sym
def object_sym
:feature_flag
end
......
......@@ -13,10 +13,7 @@ module Banzai
# to reference issues from other GitLab projects.
class IssueReferenceFilter < IssuableReferenceFilter
self.reference_type = :issue
def self.object_class
Issue
end
self.object_class = Issue
def url_for_object(issue, project)
return issue_path(issue, project) if only_path?
......
......@@ -6,10 +6,7 @@ module Banzai
# The actual filter is implemented in the EE mixin
class IterationReferenceFilter < AbstractReferenceFilter
self.reference_type = :iteration
def self.object_class
Iteration
end
self.object_class = Iteration
end
end
end
......
......@@ -6,10 +6,7 @@ module Banzai
# HTML filter that replaces label references with links.
class LabelReferenceFilter < AbstractReferenceFilter
self.reference_type = :label
def self.object_class
Label
end
self.object_class = Label
def find_object(parent_object, id)
find_labels(parent_object).find(id)
......
......@@ -9,10 +9,7 @@ module Banzai
# This filter supports cross-project references.
class MergeRequestReferenceFilter < IssuableReferenceFilter
self.reference_type = :merge_request
def self.object_class
MergeRequest
end
self.object_class = MergeRequest
def url_for_object(mr, project)
h = Gitlab::Routing.url_helpers
......
......@@ -8,10 +8,7 @@ module Banzai
include Gitlab::Utils::StrongMemoize
self.reference_type = :milestone
def self.object_class
Milestone
end
self.object_class = Milestone
# Links to project milestones contain the IID, but when we're handling
# 'regular' references, we need to use the global ID to disambiguate
......
......@@ -10,7 +10,7 @@ module Banzai
# Public: Find `namespace/project>` project references in text
#
# ProjectReferenceFilter.references_in(text) do |match, project|
# references_in(text) do |match, project|
# "<a href=...>#{project}></a>"
# end
#
......
......@@ -16,7 +16,12 @@ module Banzai
include OutputSafety
class << self
# Implement in child class
# Example: self.reference_type = :merge_request
attr_accessor :reference_type
# Implement in child class
# Example: self.object_class = MergeRequest
attr_accessor :object_class
def call(doc, context = nil, result = nil)
......@@ -57,6 +62,19 @@ module Banzai
doc
end
# Public: Find references in text (like `!123` for merge requests)
#
# references_in(text) do |match, id, project_ref, matches|
# object = find_object(project_ref, id)
# "<a href=...>#{object.to_reference}</a>"
# end
#
# text - String text to search.
#
# Yields the String match, the Integer referenced object ID, an optional String
# of the external project reference, and all of the matchdata.
#
# Returns a String replaced with the return of the block.
def references_in(text, pattern = object_reference_pattern)
raise NotImplementedError, "#{self.class} must implement method: #{__callee__}"
end
......@@ -161,6 +179,14 @@ module Banzai
CGI.unescape(href)
end
def unescape_html_entities(text)
CGI.unescapeHTML(text.to_s)
end
def escape_html_entities(text)
CGI.escapeHTML(text.to_s)
end
def replace_text_when_pattern_matches(node, index, pattern)
return unless node.text =~ pattern
......@@ -190,12 +216,16 @@ module Banzai
node.is_a?(Nokogiri::XML::Element)
end
def object_class
self.class.object_class
end
def object_reference_pattern
@object_reference_pattern ||= self.class.object_class.reference_pattern
@object_reference_pattern ||= object_class.reference_pattern
end
def object_name
@object_name ||= self.class.object_class.name.underscore
@object_name ||= object_class.name.underscore
end
def object_sym
......
......@@ -9,10 +9,7 @@ module Banzai
# This filter supports cross-project references.
class SnippetReferenceFilter < AbstractReferenceFilter
self.reference_type = :snippet
def self.object_class
Snippet
end
self.object_class = Snippet
def find_object(project, id)
return unless project.is_a?(Project)
......
......@@ -12,7 +12,7 @@ module Banzai
# Public: Find `@user` user references in text
#
# UserReferenceFilter.references_in(text) do |match, username|
# references_in(text) do |match, username|
# "<a href=...>@#{user}</a>"
# end
#
......
......@@ -6,16 +6,7 @@ module Banzai
# The actual filter is implemented in the EE mixin
class VulnerabilityReferenceFilter < IssuableReferenceFilter
self.reference_type = :vulnerability
def self.object_class
Vulnerability
end
private
def project
context[:project]
end
self.object_class = Vulnerability
end
end
end
......
......@@ -61,13 +61,13 @@ RSpec.describe Banzai::Filter::References::AbstractReferenceFilter do
.to eq([project])
end
context "when no project with that path exists" do
it "returns no value" do
context 'when no project with that path exists' do
it 'returns no value' do
expect(filter.find_for_paths(['nonexistent/project']))
.to eq([])
end
it "adds the ref to the project refs cache" do
it 'adds the ref to the project refs cache' do
project_refs_cache = {}
allow(filter).to receive(:refs_cache).and_return(project_refs_cache)
......@@ -99,4 +99,18 @@ RSpec.describe Banzai::Filter::References::AbstractReferenceFilter do
expect(filter.current_parent_path).to eq(project.full_path)
end
end
context 'abstract methods' do
describe '#find_object' do
it 'raises NotImplementedError' do
expect { filter.find_object(nil, nil) }.to raise_error(NotImplementedError)
end
end
describe '#url_for_object' do
it 'raises NotImplementedError' do
expect { filter.url_for_object(nil, nil) }.to raise_error(NotImplementedError)
end
end
end
end
......@@ -104,7 +104,7 @@ RSpec.describe Banzai::Filter::References::DesignReferenceFilter do
let(:pattern) { described_class.object_class.link_reference_pattern }
let(:parsed) do
m = pattern.match(url)
described_class.identifier(m) if m
described_class.new('', project: nil).identifier(m) if m
end
it 'can parse the reference' do
......@@ -119,9 +119,11 @@ RSpec.describe Banzai::Filter::References::DesignReferenceFilter do
describe 'static properties' do
specify do
expect(described_class).to have_attributes(
object_sym: :design,
reference_type: :design,
object_class: ::DesignManagement::Design
)
expect(described_class.new('', project: nil).object_sym).to eq :design
end
end
......
......@@ -493,19 +493,19 @@ RSpec.describe Banzai::Filter::References::IssueReferenceFilter do
it 'yields valid references' do
expect do |b|
described_class.references_in(issue.to_reference, &b)
described_class.new('', project: nil).references_in(issue.to_reference, &b)
end.to yield_with_args(issue.to_reference, issue.iid, nil, nil, MatchData)
end
it "doesn't yield invalid references" do
expect do |b|
described_class.references_in('#0', &b)
described_class.new('', project: nil).references_in('#0', &b)
end.not_to yield_control
end
it "doesn't yield unsupported references" do
expect do |b|
described_class.references_in(merge_request.to_reference, &b)
described_class.new('', project: nil).references_in(merge_request.to_reference, &b)
end.not_to yield_control
end
end
......
......@@ -183,12 +183,12 @@ RSpec.describe Banzai::Filter::References::ReferenceFilter do
end
end
describe "#call_and_update_nodes" do
describe '#call_and_update_nodes' do
include_context 'new nodes'
let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') }
let(:filter) { described_class.new(document, project: project) }
it "updates all new nodes", :aggregate_failures do
it 'updates all new nodes', :aggregate_failures do
filter.instance_variable_set('@nodes', nodes)
expect(filter).to receive(:call) { filter.instance_variable_set('@new_nodes', new_nodes) }
......@@ -201,14 +201,14 @@ RSpec.describe Banzai::Filter::References::ReferenceFilter do
end
end
describe ".call" do
describe '.call' do
include_context 'new nodes'
let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') }
let(:result) { { reference_filter_nodes: nodes } }
it "updates all nodes", :aggregate_failures do
it 'updates all nodes', :aggregate_failures do
expect_next_instance_of(described_class) do |filter|
expect(filter).to receive(:call_and_update_nodes).and_call_original
expect(filter).to receive(:with_update_nodes).and_call_original
......@@ -221,4 +221,21 @@ RSpec.describe Banzai::Filter::References::ReferenceFilter do
expect(result[:reference_filter_nodes]).to eq(expected_nodes)
end
end
context 'abstract methods' do
let(:document) { Nokogiri::HTML.fragment('<a href="foo">foo</a>') }
let(:filter) { described_class.new(document, project: project) }
describe '#references_in' do
it 'raises NotImplementedError' do
expect { filter.references_in('foo', %r{(?<!\w)}) }.to raise_error(NotImplementedError)
end
end
describe '#object_link_filter' do
it 'raises NotImplementedError' do
expect { filter.send(:object_link_filter, 'foo', %r{(?<!\w)}) }.to raise_error(NotImplementedError)
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