citusdata / citusdata/activerecord-multi-tenant

Multi-tenant class not found if defined inside a module

Open
#105 1 comment 0 reactions 0 assignees View on GitHub
2.0
Dominant language
Ruby
Stars
759
Forks
113
PR merge metrics
No merged PRs in 30d

Description

The following code works as expected and the call to `multi_tenant :tenant` automatically calls `belongs_to :tenant` and defines both `tenant` and `tenant=` methods.

```ruby
class Tenant < ApplicationRecord; end

class Foo < ApplicationRecord
multi_tenant :tenant
end
```

However, if both classes are inside a module

```ruby
module MyModule
class Tenant < ApplicationRecord; end

class Foo < ApplicationRecord
multi_tenant :tenant
end
end
```

the `multi_tenant :tenant` call does not automatically call `belongs_to :tenant` or define `tenant` and `tenant=` methods.
This is due to the implementation of `tenant_klass_defined?`

```ruby
def self.tenant_klass_defined?(tenant_name)
!!tenant_name.to_s.classify.safe_constantize
end
```

so only the top-level constant `::Tenant` is expected to be the tenant class for tenant name `:tenant`, `::MyModule::Tenant` is not considered.

I thought about copying Rails' implementation of `compute_type`

https://github.com/rails/rails/blob/6ef39975d60cc9dafd1728c49e394dad11d12327/activerecord/lib/active_record/inheritance.rb#L224-L235

that in our example would search for the following 3 classes:

`::MyModule::Foo::Tenant`, `::MyModule::Tenant`, `::Tenant`

in that order.

Another solution would be to pass `class_name` just like we do in Rails.

`multi_tenant :tenant, class_name: "::MyModule::Tenant"`

What do you guys think?

I'm willing to implement this if we can agree on a solution.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.