Just for fun and science I tried generating a slice in a namespace like this:
hanami generate slice utils/healthcheck
This is what I found:
- It created a file structure as I expected
Updated config/routes.rb
Created slices/utils/healthcheck/
Created slices/utils/healthcheck/action.rb
Created slices/utils/healthcheck/view.rb
Created slices/utils/healthcheck/views/helpers.rb
Created slices/utils/healthcheck/templates/layouts/app.html.erb
Created slices/utils/healthcheck/operation.rb
Created slices/utils/healthcheck/assets/js/app.js
Created slices/utils/healthcheck/assets/css/app.css
Created slices/utils/healthcheck/assets/images/favicon.ico
Created slices/utils/healthcheck/db/relation.rb
Created slices/utils/healthcheck/relations/.keep
Created slices/utils/healthcheck/db/repo.rb
Created slices/utils/healthcheck/repos/.keep
Created slices/utils/healthcheck/db/struct.rb
Created slices/utils/healthcheck/structs/.keep
Created slices/utils/healthcheck/actions/.keep
Created slices/utils/healthcheck/views/.keep
Created slices/utils/healthcheck/templates/.keep
Created slices/utils/healthcheck/templates/layouts/.keep
Created spec/slices/utils/healthcheck/action_spec.rb
Created spec/slices/utils/healthcheck/actions/.keep
- The generated classes were correctly namespaced too:
# auto_register: false
# frozen_string_literal: true
module Utils::Healthcheck
class Action < HanamiTest::Action
end
end
- The updated router was quite bad:
class Routes < Hanami::Routes
# Add your routes here. See https://guides.hanamirb.org/routing/overview/ for details.
slice :utils/healthcheck, at: "/utils/healthcheck" do
end
end
(this is actually a syntax error)
- It gets registered as
:utils
hanami_test[development]> Hanami.app.slices
=> #<Hanami::SliceRegistrar:0x00007f08a7bd3f38 @parent=HanamiTest::App, @slices={utils: Utils::Slice}>
- Generators for things inside such slice generally do not work.
My questions after this experiment are:
- Should the slice generator forbid slashes for now?
- Is there an appetite to make it work? It seems we are more or less half way to support it, with most difficult thing being how to symbolize such slice names (at least that’s my feeling).
- Do namespaced slices even make sense from Hanami philosophy point of view?
I think it could be useful in growing apps, especially when slices are used to divide by domain boundaries. Such apps likely still would need some “utils” or “dev” slices, would be nice to group them. But it’s also not the end of the world if they are not supported.