Skip to content

Add class customization to reference extractor

What does this MR do and why?

Allows customizing reference classes (use models with namespaces) to reference extractor.

The problem

The reference extractor tries to dynamically determine a parser and a reference class from a referable type:

# original version
# REFERABLES = %i(user issue label merge_request ...).freeze
patterns = REFERABLES.map do |type|
  Banzai::ReferenceParser[type].reference_type.to_s.classify.constantize.try(:reference_pattern)
end.uniq

That was working well because all the existing reference classes are in the "root" namespace. Such as Issue, Label, MergeRequest, etc. The reference_type.to_s.classify.constantize was finding an existing class, until we needed to add alerts to the list.

alert model has a namespace AlertManagement::Alert. That makes :alert.to_s.classify.constantize generate an invalid class (it has no namespace).

Alternative solutions I was looking into

  1. I was trying to change:
class AlertParser < BaseParser
  self.reference_type = :alert
  # to
  self.reference_type = :'alert_management/alert'
end

That let :'alert_management/alert'.to_s.classify.constantize to generate a valid class name, but creates issues in different areas. For example, the reference link on the frontend contains some information in [data-alert] attribute.

That also does not allow the reference extractor to extract an object by its URL.


  1. Adding AlertParser.reference_class method looks like a duplication, because AlertParser#references_relation returns the same value.
def self.reference_class
  AlertManagement::Alert
end

def references_relation
  AlertManagement::Alert
end

but to get the value from #reference_relation we will need to initialize an object, which I was trying to avoid. I've also thought, that values in .reference_class and #references_relation might be different for some parsers. For example, they might use scopes etc.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports