With Hanami architecture, we can have multiple Hanami applications running under . 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.

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 (#).

  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.

The generated route is named after the controller name.

  1. 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:

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

Generate an entity and a repository with a single command

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. change do
  3. create_table :books do
  4. primary_key :id
  5. column :created_at, DateTime, null: false
  6. column :updated_at, DateTime, null: false
  7. end
  8. end
  9. 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.

Generate a mailer

  1. $ bundle exec hanami generate mailer welcome

Secret

Generate a HTTP sessions secret for an application.

  1. $ bundle exec hanami generate secret web