Added
-
Reload an app's code in place via
Hanami.app.reload, without restarting the Ruby process. (@afomera and @timriley in #1625)This unloads the app and all its slices, then prepares them again, picking up changes to code, settings, providers, routes and slices themselves. The app class itself is kept, so
run Hanami.appinconfig.rucontinues to work, which means thatconfig/app.rbitself is not reloaded.Configure this with
config.code_reloading, which defaults totruein development andfalsein all other environments. -
When
hanamiis required, check that first-party gems (hanami-action, hanami-view, etc.) share the hanami gem's major.minor version, raising aHanami::GemVersionErrorwhen they don't. SetHANAMI_SKIP_GEM_VERSION_CHECK=1to skip the check. (@parndt in #1661) -
Add
collectionblocks toresourcesin routes, for routes on the resource's collection rather than a single member. For example,resources :books do collection do get "/latest", to: "books.latest", as: :latest end endgivesGET /books/latest, namedbooks_latest. (@timriley in #1646) -
Add
memberblocks toresourcesin routes, for routes on a single member that receive the resource's ID as:id, the same as itsshowroute. Other routes inside aresourcesblock receive it as:<name>_id, which suits nested child resources. For example,resources :books do member do post "/publish", to: "books.publish", as: :publish end endgivesPOST /books/:id/publish, namedbook_publish. (@timriley in #1669) -
Add
#slicetoHanami::Router::NotFoundErrorandHanami::Router::NotAllowedError, returning the slice whose router could not match the request. Error handling such as the hanami-webconsole detailed error page uses this to list the slice's routes. (@afomera in #1624) -
Translate dry-validation contract error messages using the slice's i18n, following its translation files, default locale and current locale. Contracts defined in a slice namespace, as well as contracts given to actions via
params doorcontract doblocks, use the samedry_validation.errorskeys as dry-validation's own I18n backend, falling back to dry-validation's bundled English messages. Requires dry-schema 1.17 or later. Setconfig.messages.backendon a contract to opt out. (@parndt in #1662) -
Load settings from
config/settings/default.ymlandconfig/settings/[HANAMI_ENV].yml, via the newHanami::Settings::YAMLFileStore. Values inENVcontinue to take precedence. (@aaronmallen in #1627) -
Make
Hanami::Settings::EnvStorepublic API, for use when building your ownconfig.settings_store. (@timriley in #1670) -
Set the
HANAMI_APP_PATHenv var to load the Hanami app class from a file other thanconfig/app.rb, such asHANAMI_APP_PATH=app/hanami.rb. This is useful for apps that combine Hanami with another framework. (@sandbergja in #1665) -
Build a database URL from separate env vars when no
DATABASE_URLis given:DATABASE_SCHEME(required),DATABASE_USER,DATABASE_PASSWORD,DATABASE_HOST(defaulting tolocalhost),DATABASE_PORTandDATABASE_NAME. These follow the same slice prefix and gateway suffix rules asDATABASE_URL, such asMAIN__DATABASE_HOSTorDATABASE_HOST__EXTRA. For SQLite,DATABASE_NAMEis the path to the database file. (@aaronmallen in #1621) -
Apply sensible SQLite pragmas to every connection for apps using SQLite, via hanami-db's
Hanami::DB::SQLite::Pragmas:journal_mode = wal,synchronous = normal,mmap_sizeof 128MiB,journal_size_limitof 64MiB andcache_size = 2000. (@cllns in #1637, @timriley via hanami/hanami-db#28)These are given to Sequel as
connect_sqls. Anyconnect_sqlsyou provide via a gateway'sconnection_optionsrun after these, so your own pragmas take precedence. To opt out, callskip_defaults :connect_sqls(orskip_defaultsfor all defaults) in your SQL adapter config:# config/providers/db.rb Hanami.app.configure_provider :db do config.adapter :sql do |a| a.skip_defaults :connect_sqls end end
Existing SQLite apps will switch to WAL mode on upgrade. WAL mode is persisted in the database file, and SQLite creates
-waland-shmfiles beside the database while it is in use.Switching to WAL mode writes to the database file, which a read-only connection cannot do. For gateways using
connection_options readonly: true, Hanami leaves out thejournal_modepragma. Hanami does not detect?readonly=truein a database URL, however. A gateway made read-only this way, on a database that is not already in WAL mode, will fail to connect withSQLite3::ReadOnlyException: attempt to write a readonly database. Givereadonly: trueinconnection_optionsinstead, or skip the defaults for that gateway:# config/providers/db.rb Hanami.app.configure_provider :db do config.gateway :archive do |gw| gw.adapter :sql do |adapter| adapter.skip_defaults :connect_sqls end end end
Note: this skips all of the default pragmas. You may want to add back the ones that help reads, like
mmap_sizeandcache_size, which the example above does not set. Add them viaconnect_sqlsin the gateway'sconnection_options. -
Support JRuby. Recognize JDBC database URLs (
jdbc:sqlite:,jdbc:postgresql:,jdbc:mysql:) in the db provider, and raise a helpful error when the matching JDBC gem (jdbc-sqlite3,jdbc-postgresorjdbc-mysql) is missing. (@parndt in #1617, @timriley in #1641)
Changed
-
In routes, pass the
as:option ofresourcesthrough the inflector, in the same way as the resource name. Previouslyas:was used exactly as given. Now, member routes and routes nested under the resource use its singular form, and the index and create routes use its plural form. (@timriley in #1643)For example,
resources :items, as: :datanow givesdatum,edit_datumanddatum_reviewsfor member and nested routes, anddatafor the index route. To change how a name is inflected, add a custom inflection withconfig.inflectionsin your app. -
dry-validation contracts in a slice, and those given to actions as
params do/contract doblocks, now give error messages in the slice's current locale (falling back to English) rather than always in English. Setconfig.messages.backend = :yamlon a contract to keep the previous behaviour. (@parndt in #1662) -
In
form_for, read values from the view's locals one key at a time, rather than converting them to a Hash first. (@timriley in #1644)With the lazy exposures in Hanami View 3.1, this ensures form rendering no longer runs every exposure in the view, only the ones the form reads.
-
Give the slice, rather than its config, to
Hanami::Webconsole::Middleware. This requires the matching version of hanami-webconsole. (@timriley in #1658) -
Require dry-system 1.3 or later, for reloading code in place.
Removed
- Remove the
inspector:argument fromHanami::Slice.router, since Hanami Router no longer uses an inspector. To list a slice's routes, call#routeson its router. (@afomera in #1624)
Fixed
-
Use the
as:option ofresourcesfor all route names. Member routes now use its singular form (resources :books, as: :publicationsgivespublication,new_publicationandedit_publication), and the index route keeps its ownpublicationsname instead of being overwritten by the show route. Routes nested under the resource now use it too, givingpublication_reviewsrather thanbook_reviews. (@timriley in #1643) -
Name the params for routes nested under
resourcesafter the resource name, rather than itspath:.resources :cafes, path: "coffee-shops" do resources :reviews endnow gives/coffee-shops/:cafe_id/reviews. Previously, apath:with a dash or a slash gave an invalid param name, and the nested routes did not match any request. Apps that use apath:and read the old param name (for example,article_idforresources :posts, path: "articles") will need to read the new name (post_id). (@timriley in #1643) -
Give the index and create routes of
resourceswith an uncountable name (likenews) a name with an_indexsuffix (news_index). Previously these shared thenewsname with the show route, which left the index route without a usable name. (@timriley in #1643) -
Name routes nested under a singular
resourceusing the resource's name as given, matching the resource's own routes.resource :settings do resources :items endnow givessettings_itemsrather thansetting_items. (@timriley in #1643) -
Resolve the
routeshelper in slice view contexts from the app, so views and actions in a slice generate the same named paths. (@aaronmallen in #1630) -
Load only a slice's configured translation files into its default
i18nbackend. Previously, the backend also loaded the files in the globalI18n.load_pathon its first lookup, and these could override the slice's own translations. (@timriley in #1664)If you added translation files to
I18n.load_pathand used them via thei18ncomponent, add them to your i18n config instead. Useconfig.i18n.load_pathfor a single slice, orconfig.i18n.shared_load_pathfor every slice:# config/app.rb config.i18n.shared_load_path += ["config/locales/**/*.yml"]
Calling
I18n.tdirectly on the globalI18nmodule still usesI18n.load_path, as before.Calling
reload!on thei18ncomponent also now reloads the slice's translation files, rather than replacing them with the files in the globalI18n.load_path. To use files from a gem, add their paths toconfig.i18n.load_pathorconfig.i18n.shared_load_path. -
Detect already-activated gems in
Hanami.bundled?when running outside of Bundler. Previously, gems likerackthat were activated as a dependency of another gem were reported as missing, leavingrack.monitorunregistered and causing every request to return a 500 error. (@maksim-romanov in #1639) -
In request logs, include body params parsed by actions (such as from JSON request bodies). Parsed body params were missing from logs in Hanami 3.0 when body parsing moved into Hanami Action. (@timriley in #1649)
-
Instrument and correctly handle errors in views in templates when auto-render is used. (@katafrakt in #1660)
-
Apply slice config to actions and views in a slice that inherit directly from
Hanami::ActionorHanami::View. Previously these missed every setting the slice shared with the app, such as default headers and accepted formats. (@parndt in #1656) -
Generate content security policy nonces from the config of the slice whose router serves the request, rather than the app's. Previously a slice served through its own
rack_appused the app's nonce generator. (@parndt in #1653) -
Build
loggerandinflectorproviders registered on a slice from the slice's own config, rather than the app's. (@parndt in #1613) -
Allow subclasses of
Hanami::Action,Hanami::View,Dry::Operationand other Hanami-integrated classes to be defined before the Hanami app is defined. Previously, this raised aHanami::ComponentLoadError, which could break gems that define such subclasses when they are required. Hanami now leaves these early subclasses unconfigured, and continues to configure any classes later defined within a slice's namespace. (@timriley in #1666) -
Register Hanami's first-party provider sources when
Hanami::Sliceloads, rather than when a slice is prepared. This allowsconfigure_providerto be called within an app or slice class body. (@timriley in #1636) -
Configure a slice's provider registrar when the slice class is defined rather than when it is prepared. Calling
register_providerfrom within a slice class body previously left the slice with Dry System's default registrar, leading to errors when the slice was prepared. (@timriley in #1636) -
Return
falsefromHanami::Slice.app?, rather than raisingHanami::AppLoadError, when no app is defined. (@parndt in #1612) -
Register only ROM classes from the
relations,db/commandsanddb/mappersdirectories, leaving any other classes in those directories untouched. (@aaronmallen in #1629) -
Raise a helpful error when the
pggem is missing forpostgresql://database URLs, as already happened forpostgres://URLs. (@parndt in #1647)