view icon indicating copy to clipboard operation
view copied to clipboard

Raise error when trying to build a named scope, but no scope_namespace is configured

Open timriley opened this issue 9 months ago • 0 comments

Right now if you try and build a named scope via scope("my_scope_name", **some_locals) but you haven't configured scope_namespace for the view, you'll get an inscrutable error:

     NoMethodError:
       undefined method 'const_defined?' for nil
     # ./lib/hanami/view/scope_builder.rb:51:in 'Hanami::View::ScopeBuilder.resolve_scope_class'
     # ./lib/hanami/view/scope_builder.rb:35:in 'block in Hanami::View::ScopeBuilder.scope_class'
     # ./lib/hanami/view/scope_builder.rb:34:in 'Hanami::View::ScopeBuilder.scope_class'
     # ./lib/hanami/view/scope_builder.rb:21:in 'Hanami::View::ScopeBuilder.call'
     # ./lib/hanami/view/rendering.rb:55:in 'Hanami::View::Rendering#scope'
     # ./spec/integration/scope_builder_spec.rb:50:in 'block (4 levels) in <top (required)>'

This is because ScopeBuilder tries to resolve the name to a class using namespace.const_defined? and namespace.const_get. These methods (of course) don't exist on NilClass.

It's not our intention to support resolving of scope classes from the top-level namespace. A scope_namespace must be configured.

To address this, ScopeBuilder should check whether this is configured before trying to resolve a scope class, and raise an informative error.

timriley avatar Mar 17 '25 01:03 timriley