Ruby namespaces in hanami-router 2.0.0

I have a non-hanami app which uses hanami-controller and router. I’m in the process if upgrading it to use ruby 3, which means I need to update hanami-router from 1.3.2 to one of the 2.0.0 beta versions.

In hanami-router 1.3.2, you could specify a ruby namespace where the router would look for your actions. I don’t see this behavior anymore. Does that mean I have to manually specify every route now?

1 Like

This functionality changed a bit in 2.0 but you can still achieve the same result. It’s encapsulated in a route resolver object than can be passed into Hanami::Router:

# Resolve a route identity to an Action instance.
# Infers the `actions` prefix for container keys to reduce duplication.
class RouteResolver
  include Dry::Initializer.define -> do
    param :container
  end

  def call(_, ident)
    case ident
    when String, Symbol
      String(ident)
        .then { |str| str.start_with?("actions") ? str : "actions.#{str}" }
        .then { |str| container.resolve(str) }
    when Class
      ident.respond_to?(:call) ? ident : ident.new
    else
      ident
    end
  end
end

Hanami::Router.new(resolver: RouteResolver.new(Application))
1 Like