Sha256: 69709a02e6099127da03e353e1bfbd9cbe9be60aebb4ef561b02ec3b53f8221a

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Motel
  module MultiTenant
    extend ActiveSupport::Concern

    included do

      self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new

    end

    module ClassMethods

      def establish_connection(config)
        resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(Motel::Manager.tenants)
        spec = resolver.spec(config)
        connection_handler.establish_connection (ENV['TENANT'] || self.name), spec
      end

      def connection_pool
        connection_handler.retrieve_connection_pool(current_tenant)
      end

      def retrieve_connection
        connection_handler.retrieve_connection(current_tenant)
      end

      def connected?
        connection_handler.connected?(current_tenant)
      end

      def remove_connection(tenant_name = current_tenant)
        connection_handler.remove_connection(tenant_name)
      end

      def arel_engine
        ActiveRecord::Base
      end

      def current_tenant
        Motel::Manager.determines_tenant or raise Motel::NoCurrentTenantError
      end

    end
  end

end

ActiveSupport.on_load(:active_record) do
  include Motel::MultiTenant
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
motel-activerecord-2.0.1 lib/motel/multi_tenant.rb
motel-activerecord-2.0.0 lib/motel/multi_tenant.rb