module MultiTenant::ActsAsTenant::TenantSetters

Class methods applied to the tenant model.

class Client < ActiveRecord::Base
  acts_as_tenant using: :code
end

Client.current
=> # the current client set by the middleware, or nil

# Manually set the current client, where 'acme' is in the 'code' col in the db
Client.current = 'acme'

# Manually set the current client to an AR record
Client.current

Public Instance Methods

current=(record_or_identifier)
Alias for: current_tenant=
current_tenant=(record_or_identifier) click to toggle source

Set the current tenant record. You may either pass an ActiveRecord Client record, OR the value of the `:using` option you passed to `acts_as_tenant`. Thread-safe.

@param record_or_identifier the record or the identifier in the 'tenant_identifier' column.

# File lib/multi_tenant/acts_as_tenant.rb, line 94
def current_tenant=(record_or_identifier)
  self.current_tenants = Array(record_or_identifier)
end
Also aliased as: current=
current_tenants=(records_or_identifiers) click to toggle source

Set the array of current tenant records. You may either pass an ActiveRecord Client record, OR the value of the `:using` option you passed to `acts_as_tenant`. Thread-safe.

@param records_or_identifiers array of the records or identifiers in the 'tenant_identifier' column.

# File lib/multi_tenant/acts_as_tenant.rb, line 105
def current_tenants=(records_or_identifiers)
  records, identifiers = Array(records_or_identifiers.uniq).partition { |x|
    x.class.respond_to?(:table_name) && x.class.table_name == self.table_name
  }
  tenants = if identifiers.any?
              queried_records = where({tenant_identifier => identifiers}).to_a
              if queried_records.size < identifiers.size and raise_on_tenant_not_found
                raise ::MultiTenant::TenantsNotFound.new(self, identifiers, queried_records)
              end
              records + queried_records
            else
              records
            end
  Thread.current.thread_variable_set tenant_thread_var, tenants
end