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.
$ 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 (#
).
$ 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
.
$ 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.
get '/books', to: 'books#show'
If we want to customize the route URL, without editing our routes file, we can specify a --url
argument.
$ bundle exec hanami generate action web books#show --url=/books/:id
This will generate the following route:
# apps/web/config/routes.rb
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:
# db/migrations/20170213123250_create_books.rb
change do
create_table :books do
primary_key :id
column :created_at, DateTime, null: false
column :updated_at, DateTime, null: false
end
end
end
Migrations
Generate a database migration
$ bundle exec hanami generate migration create_books
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
$ bundle exec hanami generate mailer welcome
Secret
Generate a HTTP sessions secret for an application.
$ bundle exec hanami generate secret web