class Decidim::ContentParsers::UserGroupParser
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.
@see BaseParser
Examples of how to use a content parser
Constants
- MENTION_REGEX
Matches a nickname if contains letters, numbers or underscores.
- Metadata
Class used as a container for metadata
@!attribute groups
@return [Array] an array of Decidim::UserGroup mentioned in content
Public Instance Methods
metadata()
click to toggle source
(see BaseParser#metadata
)
# File lib/decidim/content_parsers/user_group_parser.rb, line 34 def metadata Metadata.new(existing_groups) end
rewrite()
click to toggle source
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.
@return [String] the content with the valid mentions replaced by a global id
# 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
Private Instance Methods
content_nicknames()
click to toggle source
# File lib/decidim/content_parsers/user_group_parser.rb, line 49 def content_nicknames @content_nicknames ||= content.scan(MENTION_REGEX).flatten.uniq end
current_organization()
click to toggle source
# File lib/decidim/content_parsers/user_group_parser.rb, line 53 def current_organization @current_organization ||= context[:current_organization] end
existing_groups()
click to toggle source
# File lib/decidim/content_parsers/user_group_parser.rb, line 45 def existing_groups @existing_groups ||= Decidim::UserGroup.where(organization: current_organization, nickname: content_nicknames) end
groups()
click to toggle source
# File lib/decidim/content_parsers/user_group_parser.rb, line 40 def groups @groups ||= existing_groups.index_by(&:nickname) end