Fix: Add support for rendering multiple scopes by passing string as names
Background
Resolves: #252
Bug 1.
When scope name is passed as string (either scope class name, or the path to the file), the name resolving goes through the cache, with a hardcoded key scope_class, not allowing mutliple scopes to be stored and fetched.
Bug 2.
If namespace is not defined (nil), const_defined? is called on nil, causing Undefined method error to be risen.
Design
- Add spec covering both issues detected.
- Change the hardcoded cache key to the one taking passed name into the consideration
- Add support to
nilnamespace, falling back to the Object, so we can useconst_getandconst_defined?methods.
I ran into this too. Without this patch, use of multiple scopes is essentially useless which defeats the power they provide. It would be good to get this folded in and a patch released shortly after.
Hey @cllns :wave: Any chance this could be folded in as this is becoming a very big issue because without this fix, multiple scopes are effectively useless in Hanami. I've been using this patch for quite some time and haven't hit issues as of yet. I also use this heavily in Milestoner. Here's the full implementation (plus spec) which is nearly identical to the above:
@bkuhlmann Sorry for the delay. I'll check this one out today. Please stand by.
Thanks for doing this @swilgosz! And I'm really sorry for the delay in handling it.
I've merged this now and will release it shortly.
Before merging this, I:
- Expanded and tweaked the tests a bit
- Reduced the scope of this change. Stop falling back to
Objectfor scope class lookups ifconfig.scope_namespaceis not defined. This is not the design intention here; custom scopes should always come from a namespace, so we expect ascope_namespaceto be configured. I've filed #264 so we can improve the error messaging in this situation.
Thanks also for all the helpful info here, @bkuhlmann 😄
Have released this as v2.2.1!