fix multisite to work with rails 4

This commit is contained in:
Sam 2013-11-05 11:11:34 +11:00
parent 1e21fc8c9a
commit 3bdd2eeeba

View File

@ -2,6 +2,10 @@ module RailsMultisite
class ConnectionManagement
CONFIG_FILE = 'config/multisite.yml'
def self.rails4?
!!(Rails.version =~ /^4/)
end
def self.establish_connection(opts)
if opts[:db] == "default" && (!defined?(@@default_spec) || !@@default_spec)
# don't do anything .. handled implicitly
@ -18,7 +22,11 @@ module RailsMultisite
handler = @@default_connection_handler
end
ActiveRecord::Base.connection_handler = handler
ActiveRecord::Base.connection_handler.establish_connection("ActiveRecord::Base", spec)
if rails4?
ActiveRecord::Base.connection_handler.establish_connection(ActiveRecord::Base, spec)
else
ActiveRecord::Base.connection_handler.establish_connection("ActiveRecord::Base", spec)
end
end
end
@ -68,6 +76,7 @@ module RailsMultisite
end
def self.load_settings!
spec_klass = rails4? ? ActiveRecord::ConnectionAdapters::ConnectionSpecification : ActiveRecord::Base::ConnectionSpecification
configs = YAML::load(File.open(self.config_filename))
configs.each do |k,v|
raise ArgumentError.new("Please do not name any db default!") if k == "default"
@ -75,7 +84,7 @@ module RailsMultisite
end
@@db_spec_cache = Hash[*configs.map do |k, data|
[k, ActiveRecord::Base::ConnectionSpecification::Resolver.new(k, configs).spec]
[k, spec_klass::Resolver.new(k, configs).spec]
end.flatten]
@@host_spec_cache = {}
@ -86,7 +95,7 @@ module RailsMultisite
end
end
@@default_spec = ActiveRecord::Base::ConnectionSpecification::Resolver.new(Rails.env, ActiveRecord::Base.configurations).spec
@@default_spec = spec_klass::Resolver.new(Rails.env, ActiveRecord::Base.configurations).spec
ActiveRecord::Base.configurations[Rails.env]["host_names"].each do |host|
@@host_spec_cache[host] = @@default_spec
end
@ -98,7 +107,9 @@ module RailsMultisite
#
@@default_connection_handler = ActiveRecord::Base.connection_handler
ActiveRecord::Base.send :include, NewConnectionHandler if ActiveRecord::VERSION::MAJOR == 3
ActiveRecord::Base.connection_handler = @@default_connection_handler
@@connection_handlers = {}
end