Skip to content

Scaffold ​

sh
rails g trek:scaffold Book title intro:text

Akin to Rails', Trek's scaffold generates, with a single command:

  • the Book model and migration (calling the Rails model generator behind the scenes)
  • the Admin::BooksController, inheriting from Trek::ResourceController
  • the Slim views: index, show, new, edit and the _form partial
  • the Admin::BookPolicy, inheriting from Trek::ResourcePolicy
  • the resources :books route in the admin namespace
  • the locale files for the model and its admin panel, in every configured locale (auto-translated via DeepL when DEEPL_AUTH_KEY is set)
  • the entry in the admin dashboard menu

Field types ​

Standard Rails attribute types are supported (string, text, integer, float, decimal, boolean, date, datetime, references…) and mapped to the matching form helpers: text renders the content editor, boolean a switch box, references a collection select, and so on.

Namespaced models ​

sh
rails g trek:scaffold Shop::Product name price:decimal

The controller and policy are nested accordingly (Admin::Shop::ProductsController, Admin::Shop::ProductPolicy), the routes go under namespace :shop, and everything else follows the namespace.

The generated code references the model as ::Shop::Product, top-level qualified, and the dashboard menu entry is ::Shop::Product too. This is deliberate: once Admin::Shop exists, a bare Shop written anywhere under module Admin resolves to that namespace instead of the Shop model. Existing admin code that references such a model — an Admin::ShopsController#model returning Shop, or a Shop entry in Admin::Dashboard::MODELS_IN_MENU — must be updated to ::Shop once a scaffold introduces a namespace with the same name (the menu would otherwise silently list the module).

Also note that rails g model Shop::Product (which the scaffold runs) writes app/models/shop.rb as a module Shop namespace file; if a Shop model already lives there, the generator warns and Rails asks before overwriting it — move the model or answer n.

Opt-in concerns ​

The generated model ships with commented-out hooks for Trek's most useful concerns, ready to be enabled when needed:

ruby
class Book < ApplicationRecord
  # include Trek::Formattable
  # format_attributes Trek::TypographyFormatter, :title

  # include Trek::Translatable
  # translate_attributes :title

  # include RecordImageUploader::Attachment(:image)

  strip_attributes
end

Released under the MIT License.