lib/apartment.rb in ros-apartment-2.3.0 vs lib/apartment.rb in ros-apartment-2.4.0
- old
+ new
@@ -1,20 +1,25 @@
+# frozen_string_literal: true
+
require 'apartment/railtie' if defined?(Rails)
require 'active_support/core_ext/object/blank'
require 'forwardable'
require 'active_record'
require 'apartment/tenant'
module Apartment
-
class << self
-
extend Forwardable
- ACCESSOR_METHODS = [:use_schemas, :use_sql, :seed_after_create, :prepend_environment, :append_environment, :with_multi_server_setup]
- WRITER_METHODS = [:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length, :db_migrate_tenants, :seed_data_file, :parallel_migration_threads, :pg_excluded_names]
+ ACCESSOR_METHODS = %i[use_schemas use_sql seed_after_create prepend_environment
+ append_environment with_multi_server_setup].freeze
+ WRITER_METHODS = %i[tenant_names database_schema_file excluded_models
+ default_schema persistent_schemas connection_class
+ tld_length db_migrate_tenants seed_data_file
+ parallel_migration_threads pg_excluded_names].freeze
+
attr_accessor(*ACCESSOR_METHODS)
attr_writer(*WRITER_METHODS)
def_delegators :connection_class, :connection, :connection_config, :establish_connection
@@ -47,18 +52,18 @@
def excluded_models
@excluded_models || []
end
def default_schema
- @default_schema || "public" # TODO 'public' is postgres specific
+ @default_schema || 'public' # TODO: 'public' is postgres specific
end
def parallel_migration_threads
@parallel_migration_threads || 0
end
- alias :default_tenant :default_schema
- alias :default_tenant= :default_schema=
+ alias default_tenant default_schema
+ alias default_tenant= default_schema=
def persistent_schemas
@persistent_schemas || []
end
@@ -73,23 +78,24 @@
end
def seed_data_file
return @seed_data_file if defined?(@seed_data_file)
- @seed_data_file = "#{Rails.root}/db/seeds.rb"
+ @seed_data_file = Rails.root.join('db', 'seeds.rb')
end
def pg_excluded_names
@pg_excluded_names || []
end
# Reset all the config for Apartment
def reset
- (ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
+ (ACCESSOR_METHODS + WRITER_METHODS).each { |method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
end
def extract_tenant_config
return {} unless @tenant_names
+
values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
unless values.is_a? Hash
values = values.each_with_object({}) do |tenant, hash|
hash[tenant] = connection_config
end