Skip to content

GlobalID assumes model name exists as constant in the top-level namespace #80

@bdragon

Description

@bdragon

Currently GlobalID assumes that the GID model name exists as a constant defined in the top-level namespace. This is fine as a default, but I should be able to lookup models however I want if I'm using a customer locator.

Real world use case:

I have a monolithic application that I plan to break up into smaller services in the future. We are experimenting with GIDs to facilitate that transition.

Most GIDs look like 'gid://my-app/Person/1'.

For resources in namespaced modules that will eventually be refactored out, GIDs look like 'gid://my-module/Customer/1'. I have defined a custom locator for each module to handle this:

GlobalID::Locator.use :'my-module', MyModuleLocator.new

class MyModuleLocator
  def locate(gid)
    MyModule.const_get(gid.model_name).find(gid.model_id)
  end
end

However, this does not work—GlobalID.find('gid://my-module/Customer/1') raises a LoadError.

This is because GlobalID::Locator::find invokes GlobalID#model_class (which tries to Object.cont_get() the model name) before my custom locator is ever invoked [1]. Of course this raises a LoadError, because 'Customer' lives in the MyModule namespace.

[1] https://github.com/rails/globalid/blob/v0.3.6/lib/global_id/locator.rb#L17

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions