Class: Decidim::ContentParsers::UserGroupParser

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/decidim/content_parsers/user_group_parser.rb

Overview

A parser that searches user groups mentions in content.

A word starting with ‘@` will be considered as a possible mention if they only contains letters, numbers or underscores.

Defined Under Namespace

Classes: Metadata

Constant Summary collapse

MENTION_REGEX =

Matches a nickname if contains letters, numbers or underscores.

/\B@(\w*)\b/

Constants inherited from BaseParser

BaseParser::Metadata

Instance Attribute Summary

Attributes inherited from BaseParser

#content, #context

Instance Method Summary collapse

Methods inherited from BaseParser

#initialize

Constructor Details

This class inherits a constructor from Decidim::ContentParsers::BaseParser

Instance Method Details

#metadataMetadata

This method is abstract.

Subclass is expected to implement it

Collects and returns metadata. This metadata is accessible at parsing time so it can be acted upon (sending emails to the users) or maybe even stored at the DB for later consultation.

Examples:

Implementation for return a counter of prohibited words found

Metadata = Struct.new(:count)

def 
  Metadata.new(content.scan('foo').size)
end

Returns:

  • (Metadata)

    a Metadata object that holds extra information



34
35
36
# File 'lib/decidim/content_parsers/user_group_parser.rb', line 34

def 
  Metadata.new(existing_groups)
end

#rewriteString

Replaces found mentions matching a nickname of an existing group in the current organization with a global id. Other mentions found that doesn’t match an existing group are returned as is.

Returns:

  • (String)

    the content with the valid mentions replaced by a global id



27
28
29
30
31
# File 'lib/decidim/content_parsers/user_group_parser.rb', line 27

def rewrite
  content.gsub(MENTION_REGEX) do |match|
    groups[match[1..-1]]&.to_global_id&.to_s || match
  end
end