Class: Tenant

Inherits:
Object
  • Object
show all
Defined in:
lib/multitenancy/tenant.rb

Constant Summary collapse

UnknownTenantError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject



13
14
15
# File 'lib/multitenancy/tenant.rb', line 13

def current
  Thread.current[:tenant]
end

.current=(value) ⇒ Object



17
18
19
# File 'lib/multitenancy/tenant.rb', line 17

def current=(value)
  Thread.current[:tenant] = value
end

.current_idObject



21
22
23
24
# File 'lib/multitenancy/tenant.rb', line 21

def current_id
  return nil unless current
  current.id
end

.find_by(field, value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/multitenancy/tenant.rb', line 30

def find_by(field, value)
  return current if current && current.send(field) == value

  table  = Arel::Table.new(:tenants)
  query  = table.project([table[:id], table[:subdomain], table[:name]]).where(table[field].eq(value))
  record = Multitenancy.connection.select_one(query)

  self.current = nil
  self.current = new(record) if record
end

.find_by!(field, value) ⇒ Object



41
42
43
# File 'lib/multitenancy/tenant.rb', line 41

def find_by!(field, value)
  find_by(field, value) or raise UnknownTenantError, "Unknown Tenant #{field}: #{value}"
end

.load(subdomain) ⇒ Object



26
27
28
# File 'lib/multitenancy/tenant.rb', line 26

def load(subdomain)
  find_by :subdomain, subdomain
end

Instance Method Details

#uri(domain = Multitenancy.domain) ⇒ Object



46
47
48
# File 'lib/multitenancy/tenant.rb', line 46

def uri(domain = Multitenancy.domain)
  URI("https://#{ subdomain }.#{ domain }")
end