Sha256: 4e1cb894042875b94b3f01da54946f9c48296de844e48b28fd762f745177900e

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_record'

module Motel
  module Manager

    mattr_accessor :nonexistent_tenant_page
    mattr_accessor :admission_criteria
    mattr_accessor :default_tenant
    mattr_accessor :current_tenant

    class << self

      def tenants_source_configurations(config)
        source_type = config[:source] || 'default'
        source_class = "Motel::Sources::#{source_type.to_s.camelize}".constantize

        source_instance = source_class.new(config)

        ActiveRecord::Base.connection_handler.tenants_source = source_instance
      end

      def tenants
        tenants_source.tenants
      end

      def tenant(name)
        tenants_source.tenant(name)
      end

      def tenant?(name)
        active_tenants.include?(name) || tenants_source.tenant?(name)
      end

      def add_tenant(name, spec)
        tenants_source.add_tenant(name, spec)
        tenant?(name)
      end

      def update_tenant(name, spec)
        ActiveRecord::Base.remove_connection(name)
        tenants_source.update_tenant(name, spec)
        tenant(name)
      end

      def delete_tenant(name)
        ActiveRecord::Base.remove_connection(name)
        tenants_source.delete_tenant(name)
        !tenant?(name)
      end

      def active_tenants
        ActiveRecord::Base.connection_handler.active_tenants
      end

      def determines_tenant
        ENV['TENANT'] || current_tenant || default_tenant
      end

      def tenants_source
        ActiveRecord::Base.connection_handler.tenants_source
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
motel-activerecord-2.0.3 lib/motel/manager.rb
motel-activerecord-2.0.2 lib/motel/manager.rb
motel-activerecord-2.0.1 lib/motel/manager.rb
motel-activerecord-2.0.0 lib/motel/manager.rb
motel-activerecord-1.0.1 lib/motel/manager.rb
motel-activerecord-1.0.0 lib/motel/manager.rb