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

fix polymorphic tenant #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hazelsparrow
Copy link

This PR fixes an issue with polymorphic tenants where the polymorphic tenant id would not be set correctly.

  1. In the before_validation callback defined in lib/acts_as_tenant/model_extensions.rb:136, the original implementation of this gem (before polymorphic tenant was introduced in Allow polymorphic tenant #169) would set the tenant id to the current tenant id. For polymorphic tenants, a similar assignment would only take place if the fields #{fkey} and #{polymorphic_type} are nil, which is not consistent with the previous implementation. This PR removes the if conditionals.

  2. In the same callback on line 139 there is a bug:

m.send("#{fkey}=".to_sym, ActsAsTenant.current_tenant.class.to_s)

should read

m.send("#{fkey}=".to_sym, ActsAsTenant.current_tenant.id)

This is fixed in this PR.

  1. Test in spec/acts_as_tenant/model_extensions_spec.rb:226 creates two models under the same tenant (@project), and yet expects only one model in the scope of the current tenant. The reason that test is (in my opinion) a false positive is because of the way the second model is created (through an association on @article) and the if conditionals mentioned above. To solve this problem, this PR wraps creating the second model in an ActsAsTenant.with_tenant(@article) block.

A few additional comments:

  • if conditionals mentioned above, and the test that passes because of them, basically allow you to keep a cached instance of an Article scoped to tenant A, switch to tenant B, and save a new PolymorphicTenantComment scoped to tenant A while tenant B is active. It seems to me that this defeats the whole purpose of this gem, please correct me if I'm wrong.

  • You might wonder why the bug mentioned above never manifested itself. It's because the assignments in the before_validation callback are never executed, since the #{fkey} and #{polymorphic_type} are already present (they come from default_scope that gets executed as soon as you do .new on an ActiveRecord), and there's those if conditionals that would not override them if they're set.

@excid3
Copy link
Collaborator

excid3 commented Nov 17, 2020

Not sure if this is still an issue. I copied in the tests to master and wasn't able to recreate the issue.

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

Successfully merging this pull request may close these issues.

2 participants