Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
artplan1 opened this issue Dec 10, 2020 · 3 comments
Open

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

artplan1 opened this issue Dec 10, 2020 · 3 comments

Comments

@artplan1
Copy link
Contributor

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.

@excid3
Copy link
Collaborator

excid3 commented Dec 10, 2020

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

Anyone using has_global_records that expects this to work differently?

@artplan1
Copy link
Contributor Author

artplan1 commented Dec 10, 2020

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

@artplan1
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants