Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue with polymorphic tenants where the polymorphic tenant id would not be set correctly.
In the
before_validation
callback defined inlib/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}
arenil
, which is not consistent with the previous implementation. This PR removes theif
conditionals.In the same callback on line
139
there is a bug:should read
This is fixed in this PR.
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 theif
conditionals mentioned above. To solve this problem, this PR wraps creating the second model in anActsAsTenant.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 anArticle
scoped to tenant A, switch to tenant B, and save a newPolymorphicTenantComment
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 fromdefault_scope
that gets executed as soon as you do.new
on an ActiveRecord), and there's thoseif
conditionals that would not override them if they're set.