Skip to main content

Comment and Mention 💬

Overview​

The Comment and Mention feature allows users to add comments on any model's show page and mention users using the @ trigger. Powered by Lexxy, a rich text editor with @-mention autocomplete support.


Quick Start​

1. Generate Migrations​

rails g cm_admin:add_comments_with_mentions
rails db:migrate

Creates two tables:

  • cm_comments - Stores comments
  • cm_user_mentions - Tracks mentions

2. Set Up Commentable Model​

Add the comments association to any model:

class Order < ApplicationRecord
has_many :comments, as: :commentable, dependent: :destroy, class_name: 'CmComment'
end

3. Configure Comments Tab​

cm_admin do
self.comments_tab = { mentions_model: ::User }

cm_index do
column :id
# other columns
end
end

Configuration​

comments_tab Options​

OptionTypeRequiredDefaultDescription
mentions_modelClassYes-The model class for @mentions (e.g., ::User)
positionIntegerNo1Tab position on the show page
default_scopesArrayNo[]Scopes to apply on mentions_model query

Example with All Options​

self.comments_tab = { mentions_model: ::User, position: 2, default_scopes: %i[active verified] }

Display Name​

This is used to display the name of the user in the comment and mentionable dropdown.

The system looks for cm_display_name on the mentionable model. Falls back to full_name if not defined.

class User < ApplicationRecord
def cm_display_name
"#{first_name} #{last_name}"
end
end

Customization​

Custom Mention Partial (Optional)​

This is used to customize how mentions render in comments.

The CmActionTextAttachable concern is automatically included for mention functionality. To customize how mentions render in comments, override to_attachable_partial_path:

class User < ApplicationRecord
include CmActionTextAttachable

def to_attachable_partial_path
"users/mentioned" # renders app/views/users/_mentioned.html.slim
end
end

NOTES: Default partial path: cm_admin/main/mentioned

Overriding Email Notifications​

  1. MentionNotifiable is a concern that is included in the CmUserMention model. It provides the send_email_to_mentioned_user method.

  2. When a user is mentioned, MentionNotifiable#send_email_to_mentioned_user is called automatically. This sends an email to the mentioned user with:

  • Subject: "💬 New Comment on Model-Name"
  • Button: Link to view the comment
  1. To customize, create a concern with same name MentionNotifiable on application level and override the send_email_to_mentioned_user method.
module MentionNotifiable
extend ActiveSupport::Concern

included do
def send_email_to_mentioned_user
subject = "💬 New Comment on Task [#{mentionable.commentable.formatted_id}]"
CmAdmin.send_email(
to: [mentioned.email, mentionable.commentable.assignees.map(&:email)].flatten.uniq,
subject: subject,
partial_file_path: 'cm_admin/mailers/comments/mention_notification',
partial_locals: { comment: mentionable },
button_text: 'View Comments',
button_link: mentionable.cm_link
)
end
end
end

To disable email notifications entirely:

module MentionNotifiable
extend ActiveSupport::Concern

included do
def send_email_to_mentioned_user
end
end
end

Custom Email Template​

Override the default partial at on application level: app/views/cm_admin/mailers/comments/mention_notification.html.slim


How Lexxy Works​

Lexxy provides the @-mention functionality:

FeatureDescription
TriggerType @ to open autocomplete dropdown
Remote FilteringSearches mentions_model from server as you type
Space SupportHandles names with spaces (e.g., "John Doe")

When a user is mentioned:

  1. Mention renders as a clickable link in the comment
  2. CmUserMention record is created
  3. Email notification sent (if not overridden)

Models Reference​

CmComment​

AssociationTypeDescription
commenterPolymorphicUser who created the comment
commentablePolymorphicModel being commented on
contentRich TextComment content (ActionText)

CmUserMention​

AssociationTypeDescription
mentionedPolymorphicUser who was mentioned
mentionablePolymorphicComment containing the mention

Requirements​

  • ActionText must be installed
  • Current.user must be set (used as commenter)
  • paper_trail gem (both models include has_paper_trail)