Comments & Mentions
comments_tab adds a Comments tab to a model's show page. Users write rich text comments (Lexxy) and @mention other users, who are notified by email.
Quick exampleβ
rails g cm_admin:add_comments_with_mentions
rails db:migrate
class Order < ApplicationRecord
has_many :comments, as: :commentable, dependent: :destroy, class_name: "CmComment"
end
cm_admin do
comments_tab mentions_model: ::User
end
Setupβ
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
comments_tab mentions_model: ::User
cm_index do
column :id
# other columns
end
end
Optionsβ
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β
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
How it 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β
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 |
Recipesβ
Custom mention partialβ
Override to_attachable_partial_path to change how a mention renders inside a comment:
class User < ApplicationRecord
include CmActionTextAttachable
def to_attachable_partial_path
"users/mentioned" # renders app/views/users/_mentioned.html.slim
end
end
Default partial path: cm_admin/main/mentioned.
Override email notificationsβ
When a user is mentioned, MentionNotifiable#send_email_to_mentioned_user (on CmUserMention) sends an email with subject "π¬ New Comment on Model-Name" and a link to the comment. To customize it, define your own MentionNotifiable concern in the host app and override the 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 in the host app:
app/views/cm_admin/mailers/comments/mention_notification.html.slim
Requirementsβ
- ActionText must be installed
- Current.user must be set (used as commenter)
- paper_trail gem (both models include
has_paper_trail)