Skip to main content

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​

config/initializers/cm_admin.rb
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​

  1. Open Cm Prompts in the admin panel and click New Cm Prompt.
  2. Fill in Name (slug is auto-generated), Categories and Prompt (rich text).
  3. 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
FieldDescription
nameTemplate name (required, unique).
slugURL-friendly identifier, from the name.
promptRich text content (ActionText).
created_byUser who created the template.
updated_byUser 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_trgm PostgreSQL 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_category constants.
  • Use the _only suffix: 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)).