ErwinM/acts_as_tenant

ActsAsTenant::Errors::NoTenantSet when model has global records

artplan1 opened this issue · 3 comments

Hi!
We have config.require_tenant = true and models with acts_as_tenant(:tenant, has_global_records: true). When we're trying to build/create such records in tenantless scope (tenant = nil) we're receiving ActsAsTenant::Errors::NoTenantSet. This is expected and unexpected in the same time :)
ActsAsTenant.without_tenant helps, but I'm wondering if this error should really be raised for models with global records?
Thanks.

That makes sense to me and does sound like a bug.

Anyone using has_global_records that expects this to work differently?

In my understanding has_global_records doesn't mean ALL records are global for model, so it makes sense that in strict mode with require_tenant = true it raises exception (I assume it's happening in default scope)

But it doesn't make sense when we're trying to create global records

I will try to reproduce in tests

test to reproduce this:
artplan1@5b24640

summary:

allow(ActsAsTenant.configuration).to receive_messages(require_tenant: true)
...
ActsAsTenant.current_tenant = nil
...
expect(GlobalProject.new(name: "foo new").valid?).to be(true)

error:

1) ActsAsTenant tenant required does not raise an error for global records
     Failure/Error: raise ActsAsTenant::Errors::NoTenantSet
     
     ActsAsTenant::Errors::NoTenantSet:
       ActsAsTenant::Errors::NoTenantSet
     # ./lib/acts_as_tenant/model_extensions.rb:20:in `block in acts_as_tenant'
     # ./spec/models/model_extensions_spec.rb:341:in `block (3 levels) in <top (required)>'

failing code:

default_scope lambda {
          if ActsAsTenant.configuration.require_tenant && ActsAsTenant.current_tenant.nil? && !ActsAsTenant.unscoped?
            raise ActsAsTenant::Errors::NoTenantSet
          end