Module: Roomer::Utils

Included in:
Roomer
Defined in:
lib/roomer/utils.rb

Instance Method Summary collapse

Instance Method Details

#current_tenantSymbol

Fetches the current tenant

Returns:

  • (Symbol)

    the current tenant key in the thread



52
53
54
55
# File 'lib/roomer/utils.rb', line 52

def current_tenant
  key = :"roomer_current_tenant"
  Thread.current[key]
end

#current_tenant=(val) ⇒ Symbol

Sets current tenant from ApplicationController into a Thread local variable. Works only with thread-safe Rails as long as it gets set on every request

Returns:

  • (Symbol)

    the current tenant key in the thread



41
42
43
44
45
46
47
48
# File 'lib/roomer/utils.rb', line 41

def current_tenant=(val)
  key = :"roomer_current_tenant"
  unless  Thread.current[key].try(:url_identifier) == val.url_identifier
    Thread.current[key] = val
    ensure_tenant_model_reset
  end
  Thread.current[key]
end

#ensure_tenant_model_resetObject

Reset cached data in tenanted models



65
66
67
# File 'lib/roomer/utils.rb', line 65

def ensure_tenant_model_reset
  reset_models
end

#full_shared_schema_migration_pathString

Constructs the full path to the shared schema directory Example: /Users/Greg/Projects/roomer/db/migrate/global

Returns:

  • (String)

    full path to the shared schema directory



26
27
28
# File 'lib/roomer/utils.rb', line 26

def full_shared_schema_migration_path
  "#{Rails.root}/#{shared_migrations_directory}"
end

#full_tenants_table_nameString

Consutructs the full name for the tenants table with schema Example: ‘global.tenant’

Returns:

  • (String)

    full name of the tenant table



19
20
21
# File 'lib/roomer/utils.rb', line 19

def full_tenants_table_name
  "#{shared_schema_name}#{schema_seperator}#{tenants_table}"
end

#migrations_directoryString

Rails DB Migrations Directory

Returns:

  • (String)

    full path to the current migrations directory



6
7
8
# File 'lib/roomer/utils.rb', line 6

def migrations_directory
  ActiveRecord::Migrator.migrations_path
end

#model_directoryString

Directory where the models are stored

Returns:

  • (String)

    path of the directory where the models are stored



12
13
14
# File 'lib/roomer/utils.rb', line 12

def model_directory
  File.join("app","models")
end

#reset_current_tenantNil

Reset current tenant

Returns:

  • (Nil)


59
60
61
62
# File 'lib/roomer/utils.rb', line 59

def reset_current_tenant
  key = :"roomer_current_tenant"
  Thread.current[key] = nil
end

#tenant_modelObject

Returns tenant model as a constant Example: Tenant

Returns:

  • Object



33
34
35
# File 'lib/roomer/utils.rb', line 33

def tenant_model
  Roomer.tenants_table.to_s.classify.constantize
end