Rich Text Templates
Admins save reusable rich text templates (CmPrompt), grouped by category, and insert them into any rich text field by typing /.
Quick exampleβ
rails generate cm_admin:add_prompts_with_constants
rails db:migrate
Add CmPrompt to config.included_models, create a template under Cm Prompts, then type / in any rich text field:
form_field :body, input_type: :rich_text
Setupβ
1. Run the generator and migrateβ
rails generate cm_admin:add_prompts_with_constants
rails db:migrate
Creates two migrations: create_cm_prompts (the templates table) and create_constant_maps (the polymorphic category association).
2. Add CmPrompt to included modelsβ
CmAdmin.configure do |config|
config.included_models = [User, CmRole, CmPlatformSetting, CmPrompt]
# ... other configuration
end
3. Set the Lexxy templateβ
The generator adds a platform setting for the default Lexxy template. Configure it under Platform Settings β RichText Templates.
Using templatesβ
Create a templateβ
- Open Cm Prompts in the admin panel and click New Cm Prompt.
- Fill in Name (slug is auto-generated), Categories and Prompt (rich text).
- Save.
Insert a template in a rich text fieldβ
Type / in any rich text editor and pick a template from the dropdown. The content is inserted as editable text.
Filter templates by categoryβ
CmPrompt.with_category('Estimation')
# dynamic scope, auto-generated from each category name
CmPrompt.estimation_only
CmPrompt.documentation_only
The _only suffix is added to category names to create scope methods.
Policy scopingβ
Restrict which templates a role sees with a policy scope:
class CmPromptPolicy < ApplicationPolicy
class RichTextTemplateScope < Scope
def resolve
scope.all # your access logic here
end
end
end
The scope class must be named CmAdmin::{ModelName}PolicyRichTextTemplateScope.
How it worksβ
CmPrompt modelβ
class CmPrompt < ApplicationRecord
has_rich_text :prompt
has_many :categories, as: :mappable, dependent: :destroy, class_name: 'ConstantMap'
has_many :constants, through: :categories
validates :name, :slug, presence: true, uniqueness: { case_sensitive: false }
end
| Field | Description |
|---|---|
name | Template name (required, unique). |
slug | URL-friendly identifier, from the name. |
prompt | Rich text content (ActionText). |
created_by | User who created the template. |
updated_by | User who last updated the template. |
ConstantMap is a polymorphic join (constant + mappable) that links categories to any model.
Lexxy integrationβ
Rich text fields render a <lexxy-prompt trigger="/"> element populated by build_lexxy_prompt, which fetches templates through the rich_text_templates policy scope. The editor calls the GET rich_text_templates endpoint to list them.
Dynamic scope methodsβ
CmPrompt.method_missing turns any *_only call into with_category(name.titleize), so CmPrompt.estimation_only means CmPrompt.with_category('Estimation').
Category assignmentβ
Categories are assigned through the category_ids virtual attribute: setting it rebuilds the ConstantMap rows for that prompt.
Database schemaβ
create_table :cm_prompts do |t|
t.string :name, null: false
t.citext :slug, null: false
t.references :created_by, foreign_key: { to_table: :users }
t.references :updated_by, foreign_key: { to_table: :users }
t.timestamps
end
add_index :cm_prompts, :name, unique: true
add_index :cm_prompts, :slug, unique: true
create_table :constant_maps do |t|
t.references :constant, null: false
t.references :mappable, polymorphic: true, null: false
t.timestamps
end
add_index :constant_maps, %i[constant_id mappable_id mappable_type], unique: true
Recipesβ
Create a template with categoriesβ
CmPrompt.create!(
name: 'Project Estimation Template',
prompt: '<h1>Project Estimation</h1><p>Enter your estimation details...</p>',
category_ids: [1, 2] # IDs of cm_prompt_category constants
)
Filter templates by categoryβ
CmPrompt.estimation_only
CmPrompt.with_category('Documentation')
CmPrompt.estimation_only.order(created_at: :desc).limit(10)
Read a template's categoriesβ
template = CmPrompt.first
template.category_names # => "Estimation, Documentation"
template.category_ids # => [1, 2]
Requirementsβ
- Lexxy (
~> 0.8) for the rich text editor - ActionText
pg_trgmPostgreSQL extension (for citext)
Troubleshootingβ
Templates don't appear in the editorβ
- Check the policy scope returns templates for the current user.
- Check the template has at least one category.
- Check the Lexxy template platform setting is configured.
*_only scope methods don't workβ
- Check the category exists in
cm_prompt_categoryconstants. - Use the
_onlysuffix:estimation_only. - Check the template is associated with the category.
Migration errorsβ
If constant_maps already exists, the migration skips creating it (return if table_exists?(:constant_maps)).