ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.56k stars 264 forks source link

I want to set config.require_tenant to false when the path is /admin, but it doesn't work. #299

Open hatsu38 opened 1 year ago

hatsu38 commented 1 year ago

I am using ActsAsTenant. Recently, I wanted to change require_tenant dynamically because I plan to create an admin page. So I looked at the README and rewrote acts_as_tenant.rb using lambda.

cat config/initializers/acts_as_tenant.rb

ActsAsTenant.configure do |config|
  config.require_tenant = lambda do
    puts "*" * 30
    p $request
    p $request_env
    puts "*" * 30
    if $request_env.present?
      return false if $request_env["REQUEST_PATH"].start_with?("/admin/")
    end
    true
  end
end

But $request_env is always nil even though the controller is working, so $request_env.present? is still false

******************************
nil
nil
******************************

Is there any way to set require_tenant to false only for the /admin path?

drale2k commented 1 year ago

Same issue here - $request_env always nil. Did you figure this out?

excid3 commented 1 year ago

I believe this is just an example where you'd have to set a variable to access the request. @cmer made that PR to the Gemfile and can probably give some tips on that. https://github.com/ErwinM/acts_as_tenant/pull/286

For example, you might want to use CurrentAttributes and set Current.request in a before_action so you can access it inside the lambda.

We may need to update the readme to show a complete example like that.

hatsu38 commented 1 year ago

Thank you for telling me. I misunderstood that I could use a variable called $request_env, but now I understand that I need to set the variable.

As you said, I'm glad the readme has a better example.