view icon indicating copy to clipboard operation
view copied to clipboard

Fix: Add support for rendering multiple scopes by passing string as names

Open swilgosz opened this issue 2 years ago • 1 comments

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 nil namespace, falling back to the Object, so we can use const_get and const_defined? methods.

swilgosz avatar Dec 07 '23 15:12 swilgosz

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.

bkuhlmann avatar Sep 20 '24 01:09 bkuhlmann

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 avatar Mar 16 '25 13:03 bkuhlmann

@bkuhlmann Sorry for the delay. I'll check this one out today. Please stand by.

timriley avatar Mar 17 '25 00:03 timriley

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 Object for scope class lookups if config.scope_namespace is not defined. This is not the design intention here; custom scopes should always come from a namespace, so we expect a scope_namespace to be configured. I've filed #264 so we can improve the error messaging in this situation.

timriley avatar Mar 17 '25 01:03 timriley

Thanks also for all the helpful info here, @bkuhlmann 😄

timriley avatar Mar 17 '25 01:03 timriley

Have released this as v2.2.1!

timriley avatar Mar 17 '25 01:03 timriley