ErwinM / acts_as_tenant

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

Add support for setting the current tenant in an ActionCable context #280

Open scarhand opened 2 years ago

scarhand commented 2 years ago

Allows you to use set_current_tenant_through_filter, set_current_tenant_by_subdomain_or_domain and set_current_tenant_by_subdomain in ActionCable channels, in a similar vein as ActionControllers.

E.g.:

class SpecSubdomainOrDomainChannel < ApplicationCable::Channel
  set_current_tenant_by_subdomain_or_domain

  def subscribed
    reject if params[:room_id].nil?
  end

  def whoami
    transmit current_tenant.name
  end
end

Haven't made any changes to the README, as I wasn't sure where to put it :)

excid3 commented 2 years ago

@scarhand Looks like ActiveSupport.on_load(:action_cable_channel) wasn't added until Rails 6, which is the cause for the failures on Rails 5.2.

Rails 5.2 is EOL on June 1st, 2022, so we can't really drop support for it yet I feel like.

Maybe we can do a different method for Rails 5, like a config.to_prepare block or something?

scarhand commented 2 years ago

Looks like the PR is about a month too early then ;) I'll see if config.to_prepare can be used, or if there's something else that works with Rails 5(.2).

excid3 commented 2 years ago

If there's nothing easy, we can wait on Rails 5.2 to be EOL and update the required versions here soon.

ziadsawalha commented 7 months ago

@excid3 Rails 5.2 is now EOL. Is this PR now possible to merge?

Or does it need to work with class Connection < ActionCable::Connection::Base now?