Skip to main content

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 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
comments_tab mentions_model: ::User

cm_index do
column :id
# other columns
end
end

Options​

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​

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:

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​

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

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)