Hanami has convenient code generators to speed up our development process.

Applications

With Hanami architecture, we can have multiple Hanami applications running under apps/. The default application is called Web and lives under apps/web.

We can generate new applications for different components that we want to add to our project.

  1. $ bundle exec hanami generate app admin

This generates an application named Admin under apps/admin.

Actions

Generate an action along with the corresponding view, template, route and test code with one command.

  1. $ bundle exec hanami generate action web books#show

This generates the action Web::Controllers::Books::Show.

The first argument, web, is the name of the target application in a Hanami project.

The argument books#show is the name of the controller and the action separated by the number sign (#).

For nested actions, use a slash (/) to separate the modules. Nested actions work in conjunction with nested resourceful routes. For example:

  1. $ bundle exec hanami generate action web books/editions#show

This generates the action Web::Controllers::Books::Editions::Show.

If you wish to generate only the action, without the view and template, you can do that by using the --skip-view.

  1. $ bundle exec hanami generate action web books#show --skip-view

If you wish to generate an action with a specific method, you can do that by using the --method.

  1. $ bundle exec hanami generate action web books#create --method=post

Route

The generated route is named after the controller name.

  1. # apps/web/config/routes.rb
  2. get '/books', to: 'books#show'

If we want to customize the route URL, without editing our routes file, we can specify a --url argument.

  1. $ bundle exec hanami generate action web books#show --url=/books/:id

This will generate the following route:

  1. # apps/web/config/routes.rb
  2. get '/books/:id', to: 'books#show'

The default HTTP method is GET, except for actions named:

Action nameHTTP verb
createPOST
updatePATCH
destroyDELETE

This should help you route using RESTful resources.

You can also set the HTTP method by specifying a --method argument when calling hanami generate action.

Models

Generate an entity and a repository with a single command

  1. $ bundle exec hanami generate model book
  2. create lib/bookshelf/entities/book.rb
  3. create lib/bookshelf/repositories/book_repository.rb
  4. create db/migrations/20170213123250_create_books.rb
  5. create spec/bookshelf/entities/book_spec.rb
  6. create spec/bookshelf/repositories/book_repository_spec.rb

It generates an entity with the corresponding repository, migration, and tests.

The migration will already contain the code for the creation of the table, the primary key and the timestamps:

  1. # db/migrations/20170213123250_create_books.rb
  2. Hanami::Model.migration do
  3. change do
  4. create_table :books do
  5. primary_key :id
  6. column :created_at, DateTime, null: false
  7. column :updated_at, DateTime, null: false
  8. end
  9. end
  10. end

Migrations

Generate a database migration

  1. $ bundle exec hanami generate migration create_books
  2. create db/migrations/20161112113203_create_books.rb

It generates an empty migration with the UTC timestamp and the name we have specified: db/migrations/20161112113203_create_books.rb.

Mailers

Generate a mailer

  1. $ bundle exec hanami generate mailer welcome

It creates the following files:

  1. $ tree lib/
  2. lib
  3. ├── bookshelf
  4. # ...
  5. ├── mailers
  6. ├── templates
  7. ├── welcome.html.erb
  8. └── welcome.txt.erb
  9. └── welcome.rb # Mailers::Welcome
  10. # ...

Secret

Generate a HTTP sessions secret for an application.

  1. $ bundle exec hanami generate secret web
  2. Set the following environment variable to provide the secret token:
  3. WEB_SESSIONS_SECRET="a6aa65a71538a56faffe1b1c9e96c0dc600de5dd14172f03c35cc48c3b27affe"