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

tenantable_belongs_to method #340

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

Conversation

ptarud
Copy link

@ptarud ptarud commented Aug 18, 2024

Add a tenantable_belongs_to class method that automatically validates that the parent association must has the same tenant than the record is defining the tenantable_belongs_to association.

@ptarud ptarud force-pushed the tenantable_belongs_to branch from 32db5d1 to d143bbf Compare August 18, 2024 05:18
…validates the tenant of the parent must be the same tenant of the record is defining the association
@ptarud ptarud force-pushed the tenantable_belongs_to branch from d143bbf to afed16a Compare August 20, 2024 01:59
@ptarud
Copy link
Author

ptarud commented Aug 29, 2024

@excid3 Hi, I hope you are very well. Can you review this PR please ?

@mrosso10
Copy link

mrosso10 commented Sep 6, 2024

Hi @ptarud, what would be the difference with the already existing validation?

# - validate that associations belong to the tenant, currently only for belongs_to

@mrosso10
Copy link

mrosso10 commented Sep 6, 2024

Dismiss my previous comment, I saw the difference, and actually found this useful for admin panels where there is no current_tenant.

Thanks!

@ptarud
Copy link
Author

ptarud commented Sep 6, 2024

When there is tenant setted and you change the tenant programmatically doing ActsAsTenant.current_tenant = other_tenant
and then for example you have a Parent class instanciated and then you created a Child class (that belongs_to :parent) you can assign the parent into that other child and the tenants will does not match.

Take a look of this peace of code:

ActsAsTenant.current_tenant = Tenant.first
t1_company = FactoryBot.create(:company)
ActsAsTenant.current_tenant = Tenant.second
t2_branch = FactoryBot.create(:branch, company: t1_company)

t1_company.inspect
=> "#<Company id: 1, tenant_id: 1, fantasy_name: \"Fantasy Name 1\", deleted: false, created_at: \"2024-09-06 15:58:45.439919000 +0000\", updated_at: \"2024-09-06 15:58:45.439919000 +0000\">"

t2_branch.inspect
=> "#<Branch id: 1, tenant_id: 2, company_id: 1, name: \"Branch 1\", time_zone: \"Etc/UTC\", deleted: false, created_at: \"2024-09-06 16:00:01.057926000 +0000\", updated_at: \"2024-09-06 16:00:01.057926000 +0000\">"

I think is useful having a functionality for checking this behaviour.

mrosso10 added a commit to mrosso10/acts_as_tenant that referenced this pull request Sep 6, 2024
@mrosso10
Copy link

mrosso10 commented Sep 11, 2024

Hi @ptarud, what do you think about optionally auto-assigning the tenant id from the associated record?

Just implemented it here, feel free to cherry pick:
mrosso10@5e21cda

mrosso10 referenced this pull request in mrosso10/acts_as_tenant Sep 11, 2024
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

Successfully merging this pull request may close these issues.

2 participants