Module: PgRls

Extended by:
Forwardable
Defined in:
lib/pg_rls.rb,
lib/pg_rls/tenant.rb,
lib/pg_rls/railtie.rb,
lib/pg_rls/version.rb,
lib/generators/pg_rls.rb,
lib/pg_rls/middleware.rb,
lib/pg_rls/solo/tenant.rb,
lib/pg_rls/multi_tenancy.rb,
lib/generators/pg_rls/base.rb,
lib/pg_rls/database/prepared.rb,
lib/pg_rls/schema/statements.rb,
lib/pg_rls/secure_connection.rb,
lib/pg_rls/middleware/sidekiq.rb,
lib/pg_rls/schema/up_statements.rb,
lib/pg_rls/schema/down_statements.rb,
lib/pg_rls/schema/solo/statements.rb,
lib/pg_rls/errors/tenant_not_found.rb,
lib/pg_rls/middleware/sidekiq/client.rb,
lib/pg_rls/middleware/sidekiq/server.rb,
lib/pg_rls/schema/solo/up_statements.rb,
lib/generators/pg_rls/pg_rls_generator.rb,
lib/generators/pg_rls/install_generator.rb,
lib/pg_rls/middleware/set_reset_connection.rb,
lib/generators/pg_rls/active_record/active_record_generator.rb

Overview

:nocov:

Defined Under Namespace

Modules: Base, Database, Errors, Generators, Middleware, MultiTenancy, Schema, SecureConnection, Solo, Tenant Classes: Error, Railtie

Constant Summary collapse

SECURE_USERNAME =
'app_user'
WRITER_METHODS =
%i[table_name class_name search_methods establish_default_connection].freeze
READER_METHODS =
%i[
  connection_class database_configuration execute table_name class_name search_methods establish_default_connection
].freeze
DELEGATORS_METHODS =
%i[
  connection_class database_configuration execute table_name search_methods
  class_name all_tenants main_model establish_default_connection
].freeze
VERSION =
'0.0.2.6.10'
@@as_db_admin =
false
@@table_name =
'companies'
@@class_name =
'Company'
@@username =
'app_user'
@@password =
'password'
@@solo_mode =
false
@@test_inline_tenant =
false
@@session_key =
'_hub_sessions'
@@session_prefix =
'_session_id:2::'
@@search_methods =
%i[subdomain id tenant_id]

Class Method Summary collapse

Class Method Details

.admin_execute(query = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/pg_rls.rb', line 62

def admin_execute(query = nil)
  self.as_db_admin = true
  establish_new_connection
  return yield if block_given?

  execute(query)
ensure
  self.as_db_admin = false
  establish_new_connection
end

.all_tenantsObject



86
87
88
89
90
91
92
93
# File 'lib/pg_rls.rb', line 86

def all_tenants
  main_model.all.each do |tenant|
    allowed_search_fields = search_methods.map(&:to_s).intersection(main_model.column_names)
    Tenant.switch tenant.send(allowed_search_fields.first)

    yield(tenant) if block_given?
  end
end

.connection_classObject



52
53
54
# File 'lib/pg_rls.rb', line 52

def connection_class
  @connection_class ||= ActiveRecord::Base
end

.current_connection_usernameObject



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

def current_connection_username
  connection_class.connection_db_config.configuration_hash[:username]
end

.database_admin_configurationObject



109
110
111
112
113
114
115
# File 'lib/pg_rls.rb', line 109

def database_admin_configuration
  environment_db_configuration = database_connection_file[Rails.env]

  return environment_db_configuration if environment_db_configuration['username'].present?

  environment_db_configuration.first.last
end

.database_configurationObject



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pg_rls.rb', line 117

def database_configuration
  return database_admin_configuration if default_connection?

  current_configuration = database_default_configuration.deep_dup
  current_configuration.tap do |config|
    config[:username] = PgRls.username
    config[:password] = PgRls.password
  end

  current_configuration.freeze
end

.database_connection_fileObject



46
47
48
49
50
# File 'lib/pg_rls.rb', line 46

def database_connection_file
  file = File.read(Rails.root.join('config/database.yml'))

  YAML.safe_load(ERB.new(file).result, aliases: true)
end

.database_default_configurationObject



103
104
105
106
107
# File 'lib/pg_rls.rb', line 103

def database_default_configuration
  connection_class.connection.pool.db_config.configuration_hash
rescue ActiveRecord::NoDatabaseError
  connection_class.connection_db_config.configuration_hash
end

.default_connection?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/pg_rls.rb', line 78

def default_connection?
  as_db_admin
end

.establish_default_connection=(value) ⇒ Object



73
74
75
76
# File 'lib/pg_rls.rb', line 73

def establish_default_connection=(value)
  ENV['AS_DB_ADMIN'] = value.to_s
  @default_connection = value
end

.establish_new_connectionObject



56
57
58
59
60
# File 'lib/pg_rls.rb', line 56

def establish_new_connection
  ActiveRecord::Base.connection.disconnect! if ActiveRecord::Base.connection_pool.connected?

  connection_class.establish_connection(**database_configuration)
end

.execute(query) ⇒ Object



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

def execute(query)
  ActiveRecord::Migration.execute(query)
end

.main_modelObject



82
83
84
# File 'lib/pg_rls.rb', line 82

def main_model
  class_name.to_s.camelize.constantize
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (PgRls)

    the object that the method was called on



38
39
40
# File 'lib/pg_rls.rb', line 38

def setup
  yield self
end

.solo_mode?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/pg_rls.rb', line 42

def solo_mode?
  solo_mode
end