hanami / hanami/hanami-view

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

Open
#264 0 comments 0 reactions 0 assignees View on GitHub
help-wanted
Dominant language
Ruby
Stars
177
Forks
85
Avg merge
8h 27m
Merged PRs (30d)
2

Description

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 '
```

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.

Contributor guide

Open the contributing guide

Research direction

Start with lib/hanami/view/scope_builder.rb, especially resolve_scope_class and the scope_class call path shown in the report. Read spec/integration/scope_builder_spec.rb around the failing example, then run that spec. Done means an unconfigured scope_namespace produces an informative error instead of the NilClass NoMethodError.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.