Module: MultiTenantSupport

Defined in:
lib/multi-tenant-support.rb,
lib/multi_tenant_support/errors.rb,
lib/multi_tenant_support/current.rb,
lib/multi_tenant_support/railtie.rb,
lib/multi_tenant_support/sidekiq.rb,
lib/multi_tenant_support/version.rb,
lib/multi_tenant_support/active_job.rb,
lib/multi_tenant_support/config/app.rb,
lib/multi_tenant_support/test/system.rb,
lib/multi_tenant_support/config/model.rb,
lib/multi_tenant_support/test/capybara.rb,
lib/multi_tenant_support/config/console.rb,
lib/multi_tenant_support/test/integration.rb,
lib/multi_tenant_support/config/controller.rb,
lib/multi_tenant_support/find_tenant_account.rb,
lib/multi_tenant_support/concern/model_concern.rb,
lib/multi_tenant_support/concern/controller_concern.rb,
lib/generators/multi_tenant_support/migration_generator.rb,
lib/generators/multi_tenant_support/initializer_generator.rb

Defined Under Namespace

Modules: ActiveJob, Config, ConfiguredJob, ControllerConcern, Generators, ModelConcern, Sidekiq, Test, ViewHelper Classes: Current, Error, FindTenantAccount, ImmutableTenantError, InvalidTenantAccess, MissingTenantError, NilTenantError, Railtie

Constant Summary collapse

PROTECTED =

Scoped and proteced

1
PROTECTED_EXCEPT_READ =

Scoped and protected except read across tenant

2
UNPROTECTED =

Scoped but unprotected

3
VERSION =
'1.5.0'

Class Method Summary collapse

Class Method Details

.allow_read_across_tenantObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/multi-tenant-support.rb', line 74

def allow_read_across_tenant
  raise 'Cannot read across tenant, try wrap in without_current_tenant' if current_tenant

  if block_given?
    Current.set(protection_state: PROTECTED_EXCEPT_READ) do
      yield
    end
  else
    Current.protection_state = PROTECTED_EXCEPT_READ
  end
end

.allow_read_across_tenant?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/multi-tenant-support.rb', line 54

def allow_read_across_tenant?
  current_tenant.nil? && [PROTECTED_EXCEPT_READ, UNPROTECTED].include?(Current.protection_state)
end

.app {|@app| ... } ⇒ Object

Yields:



19
20
21
22
23
24
# File 'lib/multi_tenant_support/config/app.rb', line 19

def app
  @app ||= Config::App.new
  return @app unless block_given?

  yield @app
end

.configure(&block) ⇒ Object



17
18
19
# File 'lib/multi-tenant-support.rb', line 17

def configure(&block)
  instance_eval(&block)
end

.console {|@console| ... } ⇒ Object

Yields:



14
15
16
17
18
19
# File 'lib/multi_tenant_support/config/console.rb', line 14

def console
  @console ||= Config::Console.new
  return @console unless block_given?

  yield @console
end

.controller {|@controller| ... } ⇒ Object

Yields:



15
16
17
18
19
20
# File 'lib/multi_tenant_support/config/controller.rb', line 15

def controller
  @controller ||= Config::Controller.new
  return @controller unless block_given?

  yield @controller
end

.current_tenantObject



21
22
23
# File 'lib/multi-tenant-support.rb', line 21

def current_tenant
  Current.
end

.current_tenant_account_methodObject



22
23
24
# File 'lib/multi_tenant_support/config/controller.rb', line 22

def 
  controller.
end

.current_tenant_idObject



25
26
27
# File 'lib/multi-tenant-support.rb', line 25

def current_tenant_id
  Current.&.send(model.)
end

.full_protected?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/multi-tenant-support.rb', line 50

def full_protected?
  current_tenant || Current.protection_state == PROTECTED
end

.model {|@model| ... } ⇒ Object

Yields:



30
31
32
33
34
35
# File 'lib/multi_tenant_support/config/model.rb', line 30

def model
  @model ||= Config::Model.new
  return @model unless block_given?

  yield @model
end

.set_current_tenant(tenant) ⇒ Object



29
30
31
32
# File 'lib/multi-tenant-support.rb', line 29

def set_current_tenant(tenant)
  Current. = tenant
  Current.protection_state = PROTECTED
end

.turn_off_protectionObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/multi-tenant-support.rb', line 62

def turn_off_protection
  raise 'Cannot turn off protection, try wrap in without_current_tenant' if current_tenant

  if block_given?
    Current.set(protection_state: UNPROTECTED) do
      yield
    end
  else
    Current.protection_state = UNPROTECTED
  end
end

.turn_on_full_protectionObject



86
87
88
89
90
91
92
93
94
# File 'lib/multi-tenant-support.rb', line 86

def turn_on_full_protection
  if block_given?
    Current.set(protection_state: PROTECTED) do
      yield
    end
  else
    Current.protection_state = PROTECTED
  end
end

.under_tenant(tenant_account, &block) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/multi-tenant-support.rb', line 34

def under_tenant(, &block)
  raise ArgumentError, "block is missing" if block.nil?

  Current.set(tenant_account: , protection_state: PROTECTED) do
    yield
  end
end

.unprotected?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/multi-tenant-support.rb', line 58

def unprotected?
  current_tenant.nil? && Current.protection_state == UNPROTECTED
end

.without_current_tenant(&block) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
# File 'lib/multi-tenant-support.rb', line 42

def without_current_tenant(&block)
  raise ArgumentError, "block is missing" if block.nil?

  Current.set(tenant_account: nil) do
    yield
  end
end