Quickstart
Build an admin page for an existing model after installing Catalyst. It takes three steps: write a concern, include it in the model and register the model.
module Catalyst
module Admin::BlogCategory
extend ActiveSupport::Concern
included do
catalyst do
actions only: %i[index show new create edit update destroy]
index do
filter %i[name], :search, placeholder: "Search"
column :name
column :color_name
end
show page_title: :name do
tab :profile, "" do
section "Details" do
field :name
field :color_name
field :created_at, field_type: :date, format: "%d %b, %Y"
end
end
end
new do
section "Details" do
form_field :name
form_field :color_id, input_type: :single_select, helper_method: :select_options_for_color
end
end
edit do
section "Details" do
form_field :name
form_field :color_id, input_type: :single_select, helper_method: :select_options_for_color
form_field :slug
end
end
end
end
end
end
1. Write the admin concernβ
Catalyst config lives in a concern under app/models/concerns/catalyst/admin/, named after the model. The catalyst block declares which actions the model supports and what each page shows:
indexβ the listing page:filteradds filters,columnadds table columns. See Index page.showβ the detail page:tabgroups content,sectionadds a titled section,fieldrenders an attribute. See Show page.new/editβ the forms:form_fieldrenders an input. See New page and Edit page.
2. Include it in the modelβ
class BlogCategory < ApplicationRecord
include Catalyst::Admin::BlogCategory
end
3. Register the modelβ
Add the model class to config.included_models in config/initializers/catalyst.rb:
Catalyst.configure do |config|
config.included_models = [BlogCategory, ...]
end
Restart the server and open http://localhost:3000/admin β the model appears in the sidebar with index, show and form pages working out of the box.
A complete exampleβ
A real admin page usually combines filters, badges, custom actions and richer forms. Here's a complete concern β a blog post with a publish workflow, adapted from a production app:
module Catalyst
module Admin::BlogPost
extend ActiveSupport::Concern
STATUS_TAG_COLOR = { draft: "ct-badge-blue", published: "ct-badge-green", archived: "ct-badge-gray" }.freeze
included do
catalyst do
actions only: %i[index show new create edit update destroy]
icon_name "fa-solid fa-newspaper"
sort column: "published_at", display_name: "Publish Date", default_direction: "desc", default: true
index do
page_title "Blog Posts"
# filters: search, selects and a custom scope
filter %i[title description], :search, placeholder: "Search"
filter :status # enum columns get a multi-select with their values automatically
filter :blog_category_id, :single_select, helper_method: :select_options_for_blog_category
filter :is_featured, :single_select, helper_method: :ct_boolean_filter_options, filter_with: :featured
# a row-level action that opens a form modal
custom_action name: "publish", path: ":id/publish", route_type: "member", verb: :patch,
display_type: :form_modal, icon_name: "fa fa-check",
display_if: ->(post) { post.draft? },
modal_configuration: { title: "Publish Post", confirmation_text: "Publish" } do
form do
alert_box body: "Publishing makes the post visible on the live website.", type: :info
section "" do
form_field :published_at, input_type: :date, label: "Publish Date", is_required: true
end
end
on_submit do
post = ::BlogPost.find(params[:id])
post.publish(published_at: params[:blog_post][:published_at])
post
end
end
column :title
column :blog_category_name, header: "Category"
column :status, field_type: :badge, badge_class: STATUS_TAG_COLOR
column :published_at, field_type: :date, format: "%d %b %Y", header: "Publish Date"
end
show page_title: :title do
tab :profile, "" do
section "Blog post details" do
field :title
field :description
field :status, field_type: :badge, badge_class: STATUS_TAG_COLOR
field :featured_image, field_type: :image
field :blog_category_name, label: "Category"
field :published_at, field_type: :date, format: "%d %b, %Y",
display_if: ->(post) { post.published_at.present? }
field :content, field_type: :rich_text
end
section "Log details" do
field :created_at, field_type: :date, format: "%d %b, %Y"
field :updated_at, field_type: :date, format: "%d %b, %Y"
end
end
end
new page_title: "Add Blog Post" do
section "Details" do
form_field :title
form_field :description, input_type: :text
form_field :content, input_type: :rich_text
form_field :featured_image, input_type: :single_file_upload
form_field :is_featured, input_type: :switch, label: "Featured?"
form_field :blog_category_id, input_type: :single_select,
helper_method: :select_options_for_blog_category, can_create_new_entity: true
end
end
edit page_title: "Edit Blog Post" do
section "Details" do
form_field :title
form_field :description, input_type: :text
form_field :featured_image, input_type: :single_file_upload
form_field :is_featured, input_type: :switch, label: "Featured?"
form_field :blog_category_id, input_type: :single_select,
helper_method: :select_options_for_blog_category, can_create_new_entity: true
form_field :published_at, input_type: :date, label: "Publish Date",
html_attrs: { data: { max_date: "today" } }
form_field :content, input_type: :rich_text
end
end
end
end
end
end
The helper_method referenced above returns [label, value] pairs from an admin helper (enum columns like status don't need one β their values are inferred from the model's enum):
def select_options_for_blog_category(_record = nil, _field_name = nil)
BlogCategory.all.map { |c| [c.name, c.id] }
end
Recipesβ
Show a dropdown for an associationβ
Use input_type: :single_select with a helper_method that returns [label, value] pairs, defined in an admin helper (app/helpers/catalyst/). Add can_create_new_entity: true to let admins create the associated record inline.
See Select options.
Add a status badge columnβ
column :status, field_type: :badge, badge_class: { draft: "ct-badge-yellow", published: "ct-badge-green" }
See Status badges.
Filter by a fieldβ
filter %i[status], :single_select # helper_method is optional for enum columns
filter %i[created_at], :date
See Filters.
Add a namespaced modelβ
A model inside a module, such as Billing::Invoice, works the same way. Its concern mirrors the namespace:
module Catalyst
module Admin::Billing
module Invoice
extend ActiveSupport::Concern
included do
catalyst do
# ...
end
end
end
end
end
module Billing
class Invoice < ApplicationRecord
include Catalyst::Admin::Billing::Invoice
end
end
Register it as Billing::Invoice in config.included_models. Everything Catalyst derives from the model name folds the namespace in with underscores:
| What | Name |
|---|---|
| Admin URL | /admin/billing_invoices |
| Route helpers | ct_index_billing_invoice_path, ct_show_billing_invoice_path, β¦ |
| Form params | billing_invoice[...] |
| Policy override | Catalyst::BillingInvoicePolicy in app/policies/catalyst/billing_invoice_policy.rb |
| Custom views | app/views/catalyst/billing_invoice/ |
| MCP tools | billing_invoice_list, billing_invoice_get, β¦ |
| Factory for app tests | :billing_invoice |
Two models can't fold to the same name: registering both Billing::Invoice and BillingInvoice raises an error at boot. Permissions and associated_model_name keep the full class name, "Billing::Invoice".
Next stepsβ
- Views β every page type and its DSL
- Filters & Search
- Custom actions
- Roles & Permissions