Skip to content

Commit

Permalink
ARel only requires the connection from the AR class. Simply return th…
Browse files Browse the repository at this point in the history
…e AR class rather than jump through hoops and store ivars
  • Loading branch information
tenderlove committed Feb 3, 2011
1 parent 1a15fda commit d65e3b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
10 changes: 2 additions & 8 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -747,7 +747,7 @@ def reset_column_information
undefine_attribute_methods undefine_attribute_methods
reset_column_cache reset_column_cache
@column_names = @content_columns = @dynamic_methods_hash = @inheritance_column = nil @column_names = @content_columns = @dynamic_methods_hash = @inheritance_column = nil
@arel_engine = @relation = nil @relation = nil
end end


def reset_column_cache # :nodoc: def reset_column_cache # :nodoc:
Expand Down Expand Up @@ -856,13 +856,7 @@ def arel_table
end end


def arel_engine def arel_engine
@arel_engine ||= begin self
if self == ActiveRecord::Base
ActiveRecord::Base
else
connection_handler.connection_pools[name] ? self : superclass.arel_engine
end
end
end end


# Returns a scope for this class without taking into account the default_scope. # Returns a scope for this class without taking into account the default_scope.
Expand Down
Expand Up @@ -50,34 +50,34 @@ def connection
# may be returned on an error. # may be returned on an error.
def self.establish_connection(spec = nil) def self.establish_connection(spec = nil)
case spec case spec
when nil when nil
raise AdapterNotSpecified unless defined?(Rails.env) raise AdapterNotSpecified unless defined?(Rails.env)
establish_connection(Rails.env) establish_connection(Rails.env)
when ConnectionSpecification when ConnectionSpecification
self.connection_handler.establish_connection(name, spec) self.connection_handler.establish_connection(name, spec)
when Symbol, String when Symbol, String
if configuration = configurations[spec.to_s] if configuration = configurations[spec.to_s]
establish_connection(configuration) establish_connection(configuration)
else
raise AdapterNotSpecified, "#{spec} database is not configured"
end
else else
spec = spec.symbolize_keys raise AdapterNotSpecified, "#{spec} database is not configured"
unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end end
else
spec = spec.symbolize_keys
unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end


begin begin
require "active_record/connection_adapters/#{spec[:adapter]}_adapter" require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
rescue LoadError => e rescue LoadError => e
raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e})" raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{e})"
end end


adapter_method = "#{spec[:adapter]}_connection" adapter_method = "#{spec[:adapter]}_connection"
unless respond_to?(adapter_method) unless respond_to?(adapter_method)
raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter" raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
end end


remove_connection remove_connection
establish_connection(ConnectionSpecification.new(spec, adapter_method)) establish_connection(ConnectionSpecification.new(spec, adapter_method))
end end
end end


Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/multiple_db_test.rb
Expand Up @@ -84,8 +84,8 @@ def test_transactions_across_databases
assert_equal "Ruby Developer", Entrant.find(1).name assert_equal "Ruby Developer", Entrant.find(1).name
end end


def test_arel_table_engines def test_connections
assert_not_equal Entrant.arel_engine, Course.arel_engine assert_not_equal Entrant.connection, Course.connection
assert_equal Entrant.arel_engine, Bird.arel_engine assert_equal Entrant.connection, Bird.connection
end end
end end

0 comments on commit d65e3b4

Please sign in to comment.