Module: ActsAsTenant

Defined in:
lib/acts_as_tenant.rb,
lib/acts_as_tenant/errors.rb,
lib/acts_as_tenant/version.rb,
lib/acts_as_tenant/configuration.rb,
lib/acts_as_tenant/tenant_helper.rb,
lib/acts_as_tenant/model_extensions.rb,
lib/acts_as_tenant/active_job_extensions.rb,
lib/acts_as_tenant/controller_extensions.rb,
lib/acts_as_tenant/test_tenant_middleware.rb,
lib/acts_as_tenant/controller_extensions/filter.rb,
lib/acts_as_tenant/controller_extensions/subdomain.rb,
lib/acts_as_tenant/controller_extensions/subdomain_or_domain.rb

Defined Under Namespace

Modules: ActiveJobExtensions, ControllerExtensions, Errors, ModelExtensions, Sidekiq, TenantHelper Classes: Configuration, Current, Error, TestTenantMiddleware

Constant Summary collapse

VERSION =
"1.0.1"
@@configuration =
nil
@@tenant_klass =
nil
@@models_with_global_records =
[]
@@mutable_tenant =
false

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_tenantObject



91
92
93
# File 'lib/acts_as_tenant.rb', line 91

def self.default_tenant
  @default_tenant unless unscoped
end

Class Method Details

.add_global_record_model(model) ⇒ Object



47
48
49
# File 'lib/acts_as_tenant.rb', line 47

def self.add_global_record_model model
  @@models_with_global_records.push(model)
end

.configurationObject



31
32
33
# File 'lib/acts_as_tenant.rb', line 31

def self.configuration
  @@configuration || configure
end

.configure {|configuration| ... } ⇒ Object

Yields:



25
26
27
28
29
# File 'lib/acts_as_tenant.rb', line 25

def self.configure
  @@configuration = Configuration.new
  yield configuration if block_given?
  configuration
end

.current_tenantObject



67
68
69
# File 'lib/acts_as_tenant.rb', line 67

def self.current_tenant
  Current.current_tenant || test_tenant || default_tenant
end

.current_tenant=(tenant) ⇒ Object



63
64
65
# File 'lib/acts_as_tenant.rb', line 63

def self.current_tenant=(tenant)
  Current.current_tenant = tenant
end

.fkeyObject



51
52
53
# File 'lib/acts_as_tenant.rb', line 51

def self.fkey
  "#{@@tenant_klass}_id"
end

.models_with_global_recordsObject



43
44
45
# File 'lib/acts_as_tenant.rb', line 43

def self.models_with_global_records
  @@models_with_global_records
end

.mutable_tenant!(toggle) ⇒ Object



95
96
97
# File 'lib/acts_as_tenant.rb', line 95

def self.mutable_tenant!(toggle)
  @@mutable_tenant = toggle
end

.mutable_tenant?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/acts_as_tenant.rb', line 99

def self.mutable_tenant?
  @@mutable_tenant
end

.pkeyObject



55
56
57
# File 'lib/acts_as_tenant.rb', line 55

def self.pkey
  ActsAsTenant.configuration.pkey
end

.polymorphic_typeObject



59
60
61
# File 'lib/acts_as_tenant.rb', line 59

def self.polymorphic_type
  "#{@@tenant_klass}_type"
end

.set_tenant_klass(klass) ⇒ Object



35
36
37
# File 'lib/acts_as_tenant.rb', line 35

def self.set_tenant_klass(klass)
  @@tenant_klass = klass
end

.should_require_tenant?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
# File 'lib/acts_as_tenant.rb', line 143

def self.should_require_tenant?
  if configuration.require_tenant.respond_to?(:call)
    !!configuration.require_tenant.call
  else
    !!configuration.require_tenant
  end
end

.tenant_klassObject



39
40
41
# File 'lib/acts_as_tenant.rb', line 39

def self.tenant_klass
  @@tenant_klass
end

.test_tenantObject



75
76
77
# File 'lib/acts_as_tenant.rb', line 75

def self.test_tenant
  Thread.current[:test_tenant]
end

.test_tenant=(tenant) ⇒ Object



71
72
73
# File 'lib/acts_as_tenant.rb', line 71

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

.unscopedObject



83
84
85
# File 'lib/acts_as_tenant.rb', line 83

def self.unscoped
  Current.acts_as_tenant_unscoped
end

.unscoped=(unscoped) ⇒ Object



79
80
81
# File 'lib/acts_as_tenant.rb', line 79

def self.unscoped=(unscoped)
  Current.acts_as_tenant_unscoped = unscoped
end

.unscoped?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/acts_as_tenant.rb', line 87

def self.unscoped?
  !!unscoped
end

.with_mutable_tenant(&block) ⇒ Object



136
137
138
139
140
141
# File 'lib/acts_as_tenant.rb', line 136

def self.with_mutable_tenant(&block)
  ActsAsTenant.mutable_tenant!(true)
  without_tenant(&block)
ensure
  ActsAsTenant.mutable_tenant!(false)
end

.with_tenant(tenant, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/acts_as_tenant.rb', line 103

def self.with_tenant(tenant, &block)
  if block.nil?
    raise ArgumentError, "block required"
  end

  old_tenant = current_tenant
  self.current_tenant = tenant
  value = block.call
  value
ensure
  self.current_tenant = old_tenant
end

.without_tenant(&block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/acts_as_tenant.rb', line 116

def self.without_tenant(&block)
  if block.nil?
    raise ArgumentError, "block required"
  end

  old_tenant = current_tenant
  old_test_tenant = test_tenant
  old_unscoped = unscoped

  self.current_tenant = nil
  self.test_tenant = nil
  self.unscoped = true
  value = block.call
  value
ensure
  self.current_tenant = old_tenant
  self.test_tenant = old_test_tenant
  self.unscoped = old_unscoped
end