Module: AtomicTenant::Tenantable

Extended by:
ActiveSupport::Concern
Defined in:
lib/atomic_tenant/tenantable.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.private_tenanted_modelsObject (readonly)

Returns the value of attribute private_tenanted_models.



8
9
10
# File 'lib/atomic_tenant/tenantable.rb', line 8

def private_tenanted_models
  @private_tenanted_models
end

.public_tenanted_modelsObject (readonly)

Returns the value of attribute public_tenanted_models.



8
9
10
# File 'lib/atomic_tenant/tenantable.rb', line 8

def public_tenanted_models
  @public_tenanted_models
end

Class Method Details

.register_private_tenanted_model(model) ⇒ Object



15
16
17
18
# File 'lib/atomic_tenant/tenantable.rb', line 15

def register_private_tenanted_model(model)
  @private_tenanted_models.add(model)
  @public_tenanted_models.delete(model)
end

.register_public_tenanted_model(model) ⇒ Object



10
11
12
13
# File 'lib/atomic_tenant/tenantable.rb', line 10

def register_public_tenanted_model(model)
  @public_tenanted_models.add(model)
  @private_tenanted_models.delete(model)
end

.verify_tenanted(model) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/atomic_tenant/tenantable.rb', line 20

def verify_tenanted(model)
  query = <<~SQL
    SELECT relrowsecurity
    FROM pg_class
    WHERE relname = $1;
  SQL

  result = ActiveRecord::Base.connection.exec_query(
    query,
    'SQL',
    [
      ActiveRecord::Relation::QueryAttribute.new(
        'relname',
        model.table_name,
        ActiveRecord::Type::String.new
      )
    ]
  )

  return if result.first['relrowsecurity']

  raise "Model #{model.name} is not public but does not have row level security. Did you forget to add row level security in your migration?"
end