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 commentscm_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â
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
mentions_model | Class | Yes | - | The model class for @mentions (e.g., ::User) |
position | Integer | No | 1 | Tab position on the show page |
default_scopes | Array | No | [] | 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â
-
MentionNotifiableis a concern that is included in theCmUserMentionmodel. It provides thesend_email_to_mentioned_usermethod. -
When a user is mentioned,
MentionNotifiable#send_email_to_mentioned_useris called automatically. This sends an email to the mentioned user with:
- Subject: "đŹ New Comment on Model-Name"
- Button: Link to view the comment
- To customize, create a concern with same name
MentionNotifiableon application level and override thesend_email_to_mentioned_usermethod.
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:
| Feature | Description |
|---|---|
| Trigger | Type @ to open autocomplete dropdown |
| Remote Filtering | Searches mentions_model from server as you type |
| Space Support | Handles names with spaces (e.g., "John Doe") |
When a user is mentioned:
- Mention renders as a clickable link in the comment
CmUserMentionrecord is created- Email notification sent (if not overridden)
Models Referenceâ
CmCommentâ
| Association | Type | Description |
|---|---|---|
commenter | Polymorphic | User who created the comment |
commentable | Polymorphic | Model being commented on |
content | Rich Text | Comment content (ActionText) |
CmUserMentionâ
| Association | Type | Description |
|---|---|---|
mentioned | Polymorphic | User who was mentioned |
mentionable | Polymorphic | Comment 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)