lib/apartment/database.rb in apartment-0.11.0 vs lib/apartment/database.rb in apartment-0.11.1

- old
+ new

@@ -1,49 +1,57 @@ require 'active_support/core_ext/module/delegation' module Apartment + + # The main entry point to Apartment functions module Database - class << self + extend self - # pass these methods to our adapter - delegate :create, :current_database, :process, :process_excluded_models, :reset, :seed, :switch, :to => :adapter + delegate :create, :current_database, :process, :process_excluded_models, :reset, :seed, :switch, :to => :adapter - # Call init to establish a connection to the public schema on all excluded models - # This must be done before creating any new schemas or switching - def init - process_excluded_models - end - - def adapter - @adapter ||= begin - adapter_method = "#{config[:adapter]}_adapter" - - begin - require "apartment/adapters/#{adapter_method}" - rescue LoadError => e - raise "The adapter `#{config[:adapter]}` is not yet supported" - end + # Initialize Apartment config options such as excluded_models + # + def init + process_excluded_models + end + + # Fetch the proper multi-tenant adapter based on Rails config + # + # @return {subclass of Apartment::AbstractAdapter} + # + def adapter + @adapter ||= begin + adapter_method = "#{config[:adapter]}_adapter" + + begin + require "apartment/adapters/#{adapter_method}" + rescue LoadError => e + raise "The adapter `#{config[:adapter]}` is not yet supported" + end - unless respond_to?(adapter_method) - raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" - end - - send(adapter_method, config) + unless respond_to?(adapter_method) + raise AdapterNotFound, "database configuration specifies nonexistent #{config[:adapter]} adapter" end - end - - def reload! - @adapter = nil - @config = nil - end - private - - def config - @config ||= Rails.configuration.database_configuration[Rails.env].symbolize_keys + send(adapter_method, config) end end - - end + + # Reset config and adapter so they are regenerated + # + def reload! + @adapter = nil + @config = nil + end + + private + + # Fetch the rails database configuration + # + def config + @config ||= Rails.configuration.database_configuration[Rails.env].symbolize_keys + end + + end end \ No newline at end of file