Active Admin and the Apartment Gem

2019-05-19 05:37发布

问题:

I am new to Active Admin but from what I have seen so far I think this is quite easy to implement.

I have an app with the apartment gem to add multi-tenancy.

I am adding active admin to my app. Apartment uses PostgreSQL schemas to segregate data. So for example by default your models in Apartment have a 'public' tenant unless you call something like Apartment::Tenant.switch!('abc').

In my app my User and Company model are in the public tenant and everything else is in tenants. So out of the box Active Admin works fine except that the tenant models show no records - as they should.

I did some tinkering and manually added Apartment::Tenant.switch!('abc') one of my AA model files and that worked perfect. Here is an ideal solution:

  • when the AdminUser logs in the tenant gets set to a default (say the first tenant)
  • On each tenanted model there could be a select menu that submits a param (?tenant=abc) and then the tenant is changed
  • The active tenant is persisted in perhaps the AdminUser session store so you can work in the same tenant data until you need to switch.

I think I can do this myself quite easily but I wanted to see if there was any Active Admin specific issues I would need to address like:

  • Does AA have an equivalent of a application controller? It would be nice to keep the tenant switching logic in there vs the main one.
  • The alternate AA Devise AdminUser has a separate session variable store available right?

Any suggestions would be appreciated - I will post my final solution / code back to this post once I sort it out.

回答1:

Does AA have an equivalent of a application controller? It would be nice to keep the tenant switching logic in there vs the main one.

Indeed, there is an ActiveAdmin::BaseController, but the gem authors don't talk about using it for customization, not sure why. Seems like a fine place for the logic you're talking about.

I've never needed to modify it before, but here's a blog article from someone who did.

The alternate AA Devise AdminUser has a separate session variable store available right?

Hmmm. Devise uses Warden for session management, which supports multiple user 'scopes' logged in simultaneously as well as separate session data, and if memory serves from when I've dug around in the code Warden puts the separate session data in a different key in the same cookie. Not sure if this is what you meant, but I verified that it definitely does not use a different cookie for users vs admins in my current ActiveAdmin-using Rails project.

Not a definitive answer, but moving the ball!