ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.53k stars 263 forks source link

Refactor model extensions to use klass instead of constantize #267

Closed Bramjetten closed 2 years ago

Bramjetten commented 2 years ago

This PR is a small refactor to use a built-in Rails method instead of using constantize.

The acts_as_tenant method used constantize on the association name to infer class names. This is fine for most Rails apps, but breaks down in engines with isolated namespaces. Instead, the reflect_on_all_associations(:belongs_to) method already returns an array of AssociationReflection, which has a method klass that returns the target association's class.

There are two other instances of constantize in this gem:

The default tenant is :account, which is constantized as Account. I thought about using const_get instead, but I think in this case it's better to explicitly use the namespaced version of the class you want to use as a tenant. For example: set_current_tenant_by_subdomain_or_domain "spina/account", :subdomain, :domain

Sidenote: I think @excid3's last commits fixed specs of this gem.